Message (4)

Error: E_WARNING

Description: Run-time warning.

Message: Trying to access array offset on value of type null

Stack Trace:

#0 At webfiori\framework\ui\ErrorBox Line 614

#1 At webfiori\framework\WebFioriApp Line 193

#2 At webfiori\examples\views\MdPage Line 109

#3 At webfiori\examples\views\MdPage Line 44

#4 At app\ini\routes\PagesRoutes Line

#5 At Router Line 1436

#6 At webfiori\framework\router\Router Line 1540

#7 At webfiori\framework\router\Router Line 1398

#8 At webfiori\framework\router\Router Line 691

#9 At webfiori\framework\router\Router Line 56

#10 At webfiori\Index Line 83

#11 At webfiori\Index Line 87

Tip: To display more details about the error, define the constant "WF_VERBOSE" and set its value to "true" in the class "GlobalConstants".

Internationalization | WebFiori
WebFioriAPI ReferenceLearnDownloadContributeLearn{{result.parent_page}} > {{result.title}}{{result.title}}Classes{{result.class_name}}{{result.summary}}Methods{{result.name}}{{result.summary}}

Internationalization

In this page:

Introduction

One of the things that might concern web developers is to offer their web application or website in more than one display language. One traditional approach is to have each language variant of the website in different path. For example, the developer might have a URL similar to https://example.com/en/home for English version and https://example.com/ar/home fore Arabic variant.

In WebFiori framework, it is possible to have one URL which points to multiple variants. For example, https://example.com/home can point to an English or Arabic variant of the website.

Language Classes

Before having same page in more than one language, developer must define his language variables. Language variables are defined using classes which exist in the folder [APP_DIR]/langs. Each language class will have the name LanguageXX where XX represents ISO 3166-1 alpha-2 country code of the language. Every class must extend the class Language and must be part of the namespace [APP_DIR]\langs.

By default, the framework will create two language classes in the directory [APP_DIR]/langs, the class LanguageAR for Arabic and LanguageEN for English.

Creating New Language Class

First step in creating new language class is to know its ISO 3166-1 alpha-2 code. Using that code, we create new class based on that code. Assuming that we would like to create a language class for UK. In this case, the name of the class will be LanguageUK. The class should be created in [APP_DIR]/langs.

Once the class is created, then the developer must make the class extends the class Language. After extending the class, the developer must specify writing direction of the language and its code in the constructor.

Code

12345678910
namespace app\langs;

use webfiori\framework\Language;

class LanguageUK extends Language {
    public function __construct() {
        parent::__construct('ltr', 'UK');
    }
}

We have just created new language class and it is possible to load it in our web application.

Adding Language Variables

Language variables are created in a way which is similar to having a directory. Inside the directory, you can have files and more directories. The variables represent the files in this case. The method Language::createDirectory() can be used to create a directory and the method Language::set() can be used to create a variable in a directory.

Code

12345678910111213
namespace app\langs;

use webfiori\framework\i18n\Language;

class LanguageUK extends Language {
    public function __construct() {
        parent::__construct('ltr', 'UK');

        $this->createDirectory('pages/home');
        $this->set('pages/home', 'page-title', 'Welcome to My Website');
    }
}

This approach can be used to create a single directory with a single variable. What if we would like to create multiple directories and variables? To achive this, the developer can use the method Language::createAndSet().

Code

123456789101112131415161718192021222324
namespace app\langs;

use webfiori\framework\Language;

class LanguageUK extends Language {
    public function __construct() {
        parent::__construct('ltr', 'UK');

        $this->createAndSet('pages', [
            'home' => [
                'title' => 'Welcome to My WebSite',
                'description' => 'Home page.'
            ],
            'about' => [
                'title' => 'About Me',
                'description' => 'Information about the author of the website.'
            ]
        ]);
        $this->createAndSet('apis', [
            'say-hi' => 'Hi!'
        ]);
    }
}

This code will create a directory with name pages/home with two variables and a directory with name pages/about with also two variables.

Using Language Classes

After creating language classes and setting variables, they must be loaded for use in your APIs or web pages.

Using Language in Web Pages

The class WebPage has a method which can be used to get specific language variable. The method is WebPage::get(). The method accepts one parameter which is the path to language variable.

Code

12345678910111213
namespace app\pages;

use webfiori\framework\ui\WebPage;

class HomePage extends WebPage {
    public function __construct() {
        parent::__construct();

        $this->setTitle($this->get('pages/home/title'));
        $this->setDescription($this->get('pages/home/description'));
    }
}

By default, language of the page will be based on default website language. If ?lang=XX parameter is set in the query string, page language will be based on it.

Using Language in Web Services

Loading a translation in web services is performed directly using the class Language. It is possible to make the service send a response based on a language code by adding a get or post parameter with name lang or fall back to default language. The developer can use the static method Language::getLabel() to get a language variable.

Code

1234567891011121314151617
use webfiori\http\Request;
use webfiori\framework\WebFiori;
use webfiori\http\AbstractWebService;
use webfiori\framework\Language;

class SampleService extends AbstractWebService {
    public function __construct() {
        parent::__construct('say-hello');
        $this->addRequestMethod('get');
    }

    public function processRequest() {

        $this->sendResponse(Language::getLable('apis/say-hi'));
    }
}

Next: Global Constants

Previous: Background Tasks

View this page on GitHubLast modified: 1970-01-01 03:00:00
Powered By: WebFiori Framework, Vue and VuetifyAll Rights Reserved © 2018 - 2024