PHPackages                             samsonos/php\_i18n - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Localization &amp; i18n](/categories/localization)
4. /
5. samsonos/php\_i18n

ActiveLibrary[Localization &amp; i18n](/categories/localization)

samsonos/php\_i18n
==================

SamsonPHP internalization module

0.1.1(9y ago)34.9k4[11 issues](https://github.com/samsonos/php_i18n/issues)2Open Software License (OSL) v 3.0PHP

Since May 23Pushed 9y ago2 watchersCompare

[ Source](https://github.com/samsonos/php_i18n)[ Packagist](https://packagist.org/packages/samsonos/php_i18n)[ Docs](http://samsonphp.com/)[ RSS](/packages/samsonos-php-i18n/feed)WikiDiscussions master Synced 3d ago

READMEChangelog (2)Dependencies (3)Versions (3)Used By (2)

Internalization(i18n) module for [SamsonPHP](http://samsonphp.com) framework
============================================================================

[](#internalizationi18n-module-for-samsonphp-framework)

> Module gives uses special global functions to give localization features for a web-application

Module default locale is empty string `' '`, its value is stored at `samsonos\php\core\SamsonLocale::DEF` constant. This gives ability to use any locale as default without specifying it.

Setting supported locales
-------------------------

[](#setting-supported-locales)

For adding localization support to your web-application special global function must be called:

` setlocales($locale1, $locale2, ...)`

Where parameters are:

- `$locale1 = $locale2 = 'ru'|'en'|'ko'`, and other supported locales by `samsonos\php\core\SamsonLocale`

Usually this function is called in web-application starter script `index.php` before `s()`, for example `index.php`:

```
// SamsonPHP Configuration
require('config.php');

// Add russian and french locales
setlocales('ru', 'fr');

// Run
s()->composer()->start();
```

Switching locales
-----------------

[](#switching-locales)

To switch system to another **supported** locale(defined by `setlocales()`) you should add locale prefix to url, for example if we have domain `http://example.com` we must redirect user to `http://example.com/ru` for switching system to `russian` localized version. This url suffix `/ru` is ignored by all SamsonPHP components and actually is invisible in URL dispatcher `samsonos\php\core\URL` and in all other components. The only module who handles this suffix is `samsonos\php\i18n`(this module).

> If you specify not supported locale suffix (you did not specified it in `setlocales()` before SamsonPHP is started), URL dispatcher `samsonos\php\core\URL` will handle it as regular module controller request.

Using localized views
---------------------

[](#using-localized-views)

When you switch to other supported locale then default locale, automatic system view rendering mechanism is changed. First of all system searches necessary view in *localized view path*, using default settings, system view path is `app/view/`. *localized view path* in this case is `app/view/$locale/`, where `$locale = 'ru'|'en'|'ko'`.

So if you want to have separate view files for different locales you can create two view files with the same names, for example we have english view file located at `app/view/test.php`:

```
This is ENGLISH view file
```

And russian view file located at `app/view/ru/test.php`:

```
This is RUSSIAN view file
```

Also we have regular simple controller file located at `app/view/controller/test.php`:

```
function test()
{
    m()->view('test')->title('Localization test');
}
```

And default view template file located at `app/view/index.php` who renders current module:

```

```

And if you try to visit URL `htttp://example.com/ru/test` you will get `app/view/ru/test.php` rendered instead of `app/view/test.php`.

> The logic is: If system does not find localized view it will use regular view.

Dictionary localization
-----------------------

[](#dictionary-localization)

If we don't want to create separate views for every supported locale, as they match each other in HTML markup and only differs in some words you can use **dictionary** localization logic.

For using global **dictionary** for your web-application , using default settings, you have to create special dictionary file located in `app/view/i18n/dictionary.php` with generic special function defined:

```
function dictionary()
{
    return array(
        $locale => array(
            $key => $value,
            ...
        ),
        'ru' => array(
            'Translate me' => 'Переведи меня',
        )
    );
}
```

Where parameters are:

- `$locale = 'ru'|'en'|'ko'` and other supported locales by samsonos\\core\\SamsonLocale
- `$key = 'Translate me'` any string value to outputted and translated. This key must match in all locales for translation
- `$value = 'Переведи меня'`(translation to russian) string 'Translate me' in current locale array

We use function to avoid bugs when compressing web-application to a snapshot via [samsonos/php\_compressor](http://github.com/samsonos/php_compressor)

### Using dictionary in views

[](#using-dictionary-in-views)

To use dictionary created above in a view view file you should use special global function `t([key], [return])`

Where parameters are:

- `$key = 'Translate me'` - Any string value to outputted and translated. This key must match in all locales for translation
- `$return = true|false` - If true - the translation will be returned otherwise echoed

So if we have view file(or template view file) `app/view/test.php` localized string output will be:

```
