Localization

The object is responsible for the multilanguage of the interface of the /Aleksmir/Core/Lang/Lang class. The translation of phrases in the template is performed via auxiliary function lang().

How is the interface language determined

The interface language is determined by the end of the URL string. For example, if the URL looks like this:

https://core.aleksmir.ru/core/doc/00/en or https://core.aleksmir.ru/en,

the interface language is "English", because the last element of the address bar is 'en'. But this will only work if the language code "en" is specified in parameter 'core/lang/list'. For example, if the parameter specifies such languages:

'lang' => [
  'list' => [
    'ru' => 'Russian',
    'en' => 'English'
  ]
],

then you can use the endings in the address bar 'en'. Moreover, the language, specified first in the list, it is considered the main one. The main language has the advantage of, that the prefix at the end of the address bar for it, in this case, does not need to be specified. The ability to specify the main language code in the address bar, in this case 'ru', blocked so that the same page cannot be accessed from two different addresses. Therefore, for the main language, we do not specify the ending at all.

How the lang() function works

Auxiliary lang function($section, $words) is available globally. Used in the template and in the source code for:

- phrase definitions in the interface element in the current site language ($section - site section, $words-phrase code);
- URL conversions based on the current language ($section-URL string before conversion, $words = ")
- transformations of the path to the template (separator - dot) taking into account the current language ($section - the path to the template before the conversion, $words = '')
- conversion of the URL of the address bar to another language ($section = ", $words-code of another language);
- getting the current language code ($section =", $words = ")

How to define a phrase using the lang() function

The lang() function gets the translation of the interface phrase from the route()->lang->words array. The route()->lang->words array is filled in when the 'lang' object of the \Aleksmir\Core\Lang\Lang () class is initialized. For example, consider the situation when 'ru' is the main language, 'en' - optional. In this case, the selected language for the site is English.

The 'lang' object reads phrases from the /view/default/core/lang/en file.json and stores them in a named array $core - >lang - >words.

Then it scans all the folders in the /view/default directory for the presence of other modules; for example, we have written our own module in the /view/default/my_module folder. Then the phrases from the /view/default/my_module/lang/en.json file are additionally read. If the file and phrases exist, they are loaded into the same array core ()->lang->words, at the same time, phrases with repeated codes from the file /view/default/core/lang/en.json they are overwritten with new phrases.

Then, the /product and /company folders are viewed in the same way. With, the /product folder is viewed first, then the /company folder. If there are files in them /lang/en.json, then their phrases are loaded into the array core ()->lang->words. Inside the /product and /company folders, not all of them are viewed, but only those that are set in the program settings .:

core/user/enter_skin - sets the theme code; default is 'default', in this case, the beginning of the path takes the form /view/default
core/user/prod_code - product code; default is 'my_product', in this case, the beginning of the path takes the form /product/my_product;
core/user/prod_code - the company code; by default, 'my_enter', in this case, the beginning of the path takes the form /company/my_product.

Using the lang() function in templates

Let's look at an example of using the lang() function in the /core/main/layout template.blade.php:

@section('nav')
  <p>
    {{ lang('main.layout', 'head_title') }} <br />
    {{ lang('main.layout', 'head_motto') }}
  </p>
@endsection

Here we see the code of the 'main.layout' section. The section code can be arbitrary, but for better readability, the section code will be abbreviated the name of the template file. The phrase code is also arbitrary, it must be unique within a section, but it can be repeated in another section.

Now let's look at the contents of the ru.json file:

{
  "main.layout": {
    "head_title": "Заголовок моего сайта",
    "head_motto": "Слоган для моего сайта"
}

After compiling the template, the blade template engine will generate the following code:

<p>
  Заголовок моего сайта<br />
  Слоган моего сайта
</p>

For the English version, the dictionary file en. json it will look like this:

{
  "main.layout": {
    "title": "My site title",
    "motto": "My site's slogan"
}

Dictionaries can be used not only in templates, but also in class methods. In this case, the section code will represent includes the full name of the namespace, for example 'aleksmir/core/view':

"aleksmir/core/view": {
"404": "Page not found!"
}

Implementation of multilingual page content

If the main language is 'ru', then the main page in Russian will have the address:

https://core.aleksmir.ru,

the main page in English will have the address:

https://core.aleksmir.ru/en

That is, the page addresses in different languages differ. This is done in order to, so that search engines do not have doubts and disagreements about the language of the page content. The content of the other pages of the site is implemented in the same way.

The language code is also added in the main template in the <html> tag:

<html lang="{{ lang() }}">

Methods of the \Aleksmir\Core\Lang\Lang class object

A reference to an object of the Aleksmir/Core/Lang/Lang class is available at any time via the a ()->core- > lang property. Methods of the Aleksmir/Core/Lang/Lang class:

__construct() - constructor; when initializing an object loads the dictionary from the dictionary files;
init() - initialization; when called, reloads the dictionary from the dictionary files;
translate($section, $words) - returns the phrase $words from the $section section;
defineLang() - defines the language code from the address bar;
getURL($lang) - converts the address bar to the specified language $lang.