Download the PHP package mmdm/sim-i18n without Composer
On this page you can find all versions of the php package mmdm/sim-i18n. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download mmdm/sim-i18n
More information about mmdm/sim-i18n
Files in mmdm/sim-i18n
Package sim-i18n
Short Description A simple way for multiple language management
License MIT
Informations about the package sim-i18n
Simplicity I18n
A library for internationalization.
Install
composer
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:
and you are good to go.
How to use
Description
It provides two kind of translate class:
- Simple instantiatable translate class
- Singleton instantiatable translate class
Both have same method but in different usage
- If you use DIC (Dependency Injection Container) or can pass translate instance to other part of your application, then use simple one.
- 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
Translate
createLanguageFile(string $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.
setLocale(string $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
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.
setTranslateDir(string $directory)
Set language directory
Note: Just specify directory not file.
getTranslateFromFile(string $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()
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.
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.
translate(string $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 = [])
Exactly like translate method but you can pluralization on it.
Example 1
If your file has structure like this:
you can access a translate like:
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.
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:
Example 2
If you have multiple files with some translations like:
file number 1
:
file number 2
:
Now to use translation you can do following things:
Example 3
Assume we have below translation file:
now to get it according to number of items, call
translateChoice
method.
as you can see we did not pass count
to translate but it could
be used internally.
Also if you pass count as third or forth parameter, it'll be replaced by actual count.
ISOLanguageCodes
static isInLanguageCode(string $code): bool
Check if language is in ISO 639-1 languages or not.
static isRtlLanguage(string $code): bool
Check if a language is rtl or not.
Rtl language list for now are:
- Arabic
- Azerbaijani
- Divehi
- Hebrew
- Kurdish
- Pashto
- PersianFarsi
- Sindhi
- Urdu
- Uyghur
- Yiddish
License
Under MIT license.