PHPackages                             mmdm/sim-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. mmdm/sim-i18n

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

mmdm/sim-i18n
=============

A simple way for multiple language management

v1.2.0(5y ago)135MITPHPPHP &gt;=7.2

Since Aug 20Pushed 5y ago1 watchersCompare

[ Source](https://github.com/mmdm95/sim-i18n)[ Packagist](https://packagist.org/packages/mmdm/sim-i18n)[ RSS](/packages/mmdm-sim-i18n/feed)WikiDiscussions master Synced 3w ago

READMEChangelogDependenciesVersions (7)Used By (0)

Simplicity I18n
===============

[](#simplicity-i18n)

A library for internationalization.

Install
-------

[](#install)

**composer**

```
composer require mmdm/sim-i18n
```

Or you can simply download zip file from github and extract it, then put file to your project library and use it like other libraries.

Just add line below to autoload files:

```
require_once 'path_to_library/autoloader.php';
```

and you are good to go.

How to use
----------

[](#how-to-use)

```
// to instantiate a translate object
$translate = new translate();

// then you should set directory and file of language
$translate->setTranslateDir('path_to_languages_directory');
$translate->setLocale(ISOLanguageCodes::LANGUAGE_ENGLISH);

// then use methods
$translate->translate($translate_key);
```

#### Description

[](#description)

It provides two kind of translate class:

- Simple instantiatable translate class
- Singleton instantiatable translate class

Both have same method but in different usage

1. If you use DIC (Dependency Injection Container) or can pass translate instance to other part of your application, then use simple one.
2. Otherwise use singleton for convenient.

**First one is recommended**

For language, it recommend *ISO 639-1* language codes that can access to them through `ISOLanguageCodes` class.

Test beside testing purposes, also have example purposes. See how to use methods there.

Available functions
-------------------

[](#available-functions)

#### Translate

[](#translate)

#### `createLanguageFile(string $language, string $directory)`

[](#createlanguagefilestring-language-string-directory)

This method will create an empty language file in $directory.

Note: $language should be just the file name like en, fr etc. Note: If directory or file is exists, it do nothing. Note: Please use `ISOLanguageCodes` class for better results.

```
// to create a language file
// de -> German
$translate->createLanguageFile('de', 'path/to/languages/directory');

// this is better
$translate->createLanguageFile(ISOLanguageCodes::LANGUAGE_GERMAN, 'path/to/languages/directory');
```

#### `setLocale(string $language)`

[](#setlocalestring-language)

Set locale language to get translates from that file

Note: This is actually the language file name(Just file name not directory name).

Default is *en*

```
// set locale to french
$translate->setLocale('fr');

// this is better
$translate->setLocale(ISOLanguageCodes::LANGUAGE_FRENCH);
```

#### `getLanguage(): string`

[](#getlanguage-string)

Get translate language that has been set from `setLocale` method.

Note: If you use *ISO 639-1* standard, you can set html language with this method easily.

```
$language = $translate->getLanguage();
```

#### `setTranslateDir(string $directory)`

[](#settranslatedirstring-directory)

Set language directory

Note: Just specify directory not file.

```
$translate->setTranslateDir('path_to_languages_directory');
```

#### `getTranslateFromFile(string $filename, bool $fresh = false): array`

[](#gettranslatefromfilestring-filename-bool-fresh--false-array)

Get all translates from a language file.

Basically after reading a language file, it will be cached by library but if you need to read it another time, pass *true* as $fresh variable value.

#### `itIsRTL()`

[](#itisrtl)

You can specify it is a *rtl* language that is using. Purpose of this method is to have specific reaction according to *rtl* in your view.

```
$translate->itIsRTL();
```

#### `isRTL(): bool`

[](#isrtl-bool)

Return *true* if language is *rtl* otherwise return *false*.

Note: By default it will check in `ISOLanguageCodes` class in rtl languages to check it is a rtl language or not. Note: If you set it is a rtl language through `itIsRTL` method, it will be rtl in any condition even if it's not.

```
$is_rtl = $translate->isRTL();
```

#### `translate(string $key, $fileOrValue = null, array $value = [])`

[](#translatestring-key-fileorvalue--null-array-value--)

This is the main method to translate to language you specified.

$key is the key that sets in language file.

In *$fileOrValue* parameter you can pass translate filename (without specify directory) or directory + filename by adding `file:` prefix to string or if you are happy with previous setting, and need to pass values to translate string, pass it here instead of third parameter.

**Note:** If `$key` could not translate, `$key` will return.

#### `translateChoice(string $key, int $count, $fileOrValue = null, array $value = [])`

[](#translatechoicestring-key-int-count-fileorvalue--null-array-value--)

Exactly like translate method but you can pluralization on it.

##### Example 1

[](#example-1)

If your file has structure like this:

```
return [
    'current-lang' => 'Current language is: ',
    'greeting' => 'Hello dear {user}',
    'labels' => [
        'test' => 'This is a test.'
    ]
]

```

you can access a translate like:

```
$current_lang = $translate->translate('current-lang');

// output:
// Current language is:
```

You can use placeholder for your translates and replace that placeholder by passing and array of key(as placeholder) and value(as value of placeholder) through $value parameter.

```
$greeting = $translate->translate('greeting', ['user' => 'MMDM']);

// output:
// Hello dear MMDM
```

If you need to access a multidimensional array inside you translate file, you can pass $key as a dot separated string to get what you want:

```
$test_label = $translate->translate('labels.test');

// output:
// This is a test.
```

##### Example 2

[](#example-2)

If you have multiple files with some translations like:

`file number 1`:

```
return [
    'a1' => 'hello',
    'b1' => 'hi',
    'c1' => 'hi {user}'
]

```

`file number 2`:

```
return [
    'a2' => 'hello again',
    'b2' => 'hi again',
    'c2' => 'hi again {user}'
]

```

Now to use translation you can do following things:

```
// simple usage when you configured directory and filename before
$translate->translate('a1');

// change them in runtime
// [this is not so much convenient!]
$translate
    ->setTranslateDir('direcoty_to_file_1_or_2')
    ->setLocale('en_or_other_languages')
    ->translate('a1_or_a2');

// above code with parameter
$translate
    ->setTranslateDir('direcoty_to_file_1_or_2')
    ->setLocale('en_or_other_languages')
    ->translate('c1_or_c2', ['user' => 'MMDM']);

//===================================================

// a convenient way to use above code
// with just change locale filename
$translate->translate('a1_or_a2', 'en_or_other_languages');

// above code with parameter
$translate->translate('c1_or_c2', 'en_or_other_languages', ['user' => 'MMDM']);

// a convenient way to use above code
// with changing directory + filename
$translate->translate('a1_or_a2', 'file:directory/en_or_other_languages');
```

##### Example 3

[](#example-3)

Assume we have below translation file:

```
