Handle message translation

1) Initialize application
$i18n = I18n::getInstance(NAME, APPLICATION PATH);
retrieve next instances with $i18n = I18n::getInstance(NAME);

2) Retrieve locales
$en = $i18n->init(LOCALE)->get();
or: $i18n->init(LOCALE)->alias('L');

3) Retrieve text
$en::KEY
or: L::KEY

For better performance :

  • When a language file is modified, call $i18n->generate(); (do NOT call at each page !)
  • Initialize application with I18n::getInstance(NAME, APPLICATION PATH, I18n::MODE_USE_GENERATED)
package salt\utils

 Methods

Set an alias for a locale class

alias(string $alias, string $locale = NULL

Parameters

$alias

string

name of the class (with namespace) to use

$locale

string

locale to alias, if not provided, use the locale of the last init() call

Check every locale files.

check(boolean $display = FALSE) : array<mixed,mixed>

Display keys missing or not defined in default locale for each locale files

DO NOT CALL this method at each page ! You have to call it only once, after a language file was modified.

Parameters

$display

boolean

TRUE for display check report

Returns

array<mixed,mixed>Report as :
locale => array(
'count' => number of entries,
'missing' => entries missing in locale but present in default,
'orphan' => entries in locale but missing in default
)
First locale is default locale

Generate class files for all available locales.

generate(boolean $display = FALSE) : array<mixed,string>

DO NOT CALL this method at each page ! You have to call it only once, after a language file was modified
If you don't want to do this, use I18n::MODE_REGENERATE_ON_THE_FLY in getInstance() instead.

Parameters

$display

boolean

TRUE for display report after generate

Returns

array<mixed,string>list of initialized locales

Retrieve class for an initialized locale

get(string $locale = NULL) : \stdClass

Parameters

$locale

string

locale to retrieve or NULL for use the last initialized locale

Returns

\stdClassa class for use the specified locale

Return selected locale after init() call

getCurrentLocale() : string

Returns

stringcurrent locale

Create or retrieve an I18n instance.

getInstance(string $name, string $rootPath = NULL, integer $mode = self::MODE_REGENERATE_ON_THE_FLY) : static
Static

Parameters

$name

string

name of the I18n instance to retrieve

$rootPath

string

absolute path of an application. Can be used to quickly initialize cache and lang paths.

$mode

integer

set it to a self::MODE_* value for change how locales classes are loaded

Returns

static

Initialize the first available locale in the list

init(string|array<mixed,string> $locales) : static

Parameters

$locales

stringarray<mixed,string>

locale to load, in preference order. Load the first available locale.
Generic locale has not required.
init('en_us') is equivalent to init(array('en_us', 'en'))

Exceptions

\salt\SaltException

Returns

staticcurrent instance

Check the generation mode

isGenerationMode(integer $mode) : boolean

Parameters

$mode

integer

a generation mode

Returns

booleanTRUE if current generation mode is $mode

Set the path to save generated class files

setCachePath(string $path) : \salt\I18n

Parameters

$path

string

absolute path that will contains PHP classes

Returns

\salt\I18ncurrent instance

Set the path to seach yaml files

setLangPath(string $path) : \salt\I18n

Parameters

$path

string

absolute path that contains yaml files

Returns

\salt\I18ncurrent instance

Create a new instance for an application

__construct(string $name, string $path, integer $mode) 

Parameters

$name

string

Application name

$path

string

absolute path of the application

$mode

integer

set it to a self::MODE_* value for change how locales classes are loaded

Extract all keys from multi dimensionnal array

array_keys_recursive(array<mixed,mixed> $array, string $separator, string $prefix = '') : array<mixed,string>
Static

Parameters

$array

array<mixed,mixed>

multi dimensionnal array

$separator

string

the separator for merge sub keys

$prefix

string

internal prefix for recursivity

Returns

array<mixed,string>key list. If a value is an array, all array keys will be added with a prefix : the current key and $separator.
Example :
[1 => a, 2 => [ b => A, c => B ]] with '_' as separator will return [1, 2_b, 2_c]

Build PHP code for $data

buildData(array<mixed,mixed> $data, string $keyPrefix = '', array<mixed,string> $allKeys = array()) : string
Static

Parameters

$data

array<mixed,mixed>

array as key=>value, value can be an array

$keyPrefix

string

prefix of all keys, do not use on first call

$allKeys

array<mixed,string>

all computed keys, internal calls only

Returns

stringPHP code with all leaf of $data as constant and all node of $data as static function that return an array

Throw an exception if any locale is already initialized

checkNoneLocaleInitialized() 

Exceptions

\salt\SaltException if method init() was called

Retrieve all compatibles locales

compatibleLocales(string $locale) : array<mixed,string>

Parameters

$locale

string

locale like xx-yy

Returns

array<mixed,string>array of locales, for example [xx-yy, xx]

Create a PHP class source

convertToClass(string $source, string $namespace, string $className, string $parent, array<mixed,mixed> $data, string $generationInfos) : string
Static

Parameters

$source

string

original filename that contains raw data (yaml localization file)

$namespace

string

namespace of the class

$className

string

name of the class

$parent

string

name of the parent class (of same namespace)

$data

array<mixed,mixed>

array as key=>value, value can be an array

$generationInfos

string

additionnal information on generation

Returns

stringPHP code of the class

Generate and write a class to disk

generateClass(string $source, string $destination, string $namespace, string $locale, string $parent) 

Parameters

$source

string

locale file

$destination

string

destination file

$namespace

string

namespace of the class

$locale

string

locale to generate

$parent

string

parent locale

Generate a .htaccess file in $dir

generateHtaccess(string $dir) 

Parameters

$dir

string

directory

Initialize a locale

initLocale(string $locale, boolean $forceGenerate = FALSE) : static

Parameters

$locale

string

locale to initialize

$forceGenerate

boolean

TRUE for generate class file for MODE_USE_GENERATED mode

Returns

staticcurrent I18n or NULL if locale does not exists

Normalize a locale key

normalizeKey(string $key) : string
Static

Parameters

$key

string

key in locale file

Returns

stringnormalized key

Normalize locale to [_a-z]*

normalizeLocale(string $locale) : string

Parameters

$locale

string

raw locale like en-US

Returns

stringnormalized locale like en_us

Retrieve locale to create with all parents

retrieveClassesToCreate(string $locale, boolean $parent = FALSE) : array<mixed,mixed>

Parameters

$locale

string

locale

$parent

boolean

TRUE if is parent locale

Returns

array<mixed,mixed>array locale => yamlFilePath. yamlFilePath is NULL if the file don't exist

Retrieve root of a locale

rootLocale(string $locale) : string

Parameters

$locale

string

locale like xx_yy

Returns

stringroot locale like xx

 Properties

 

absolute path of generated class files

$_saltCachePath : string

Default

NULL
 

how to retrieve locales classes. see self::MODE_*

$_saltGenerationMode : integer

Default

self::MODE_REGENERATE_ON_THE_FLY
 

list of initialized locales as : locale => className or FALSE if not exists

$_saltInitializedLocales : array<mixed,string>

Default

array()
 

Instances indexed by application name

$_saltInstances : array<mixed,\salt\I18n>

Default

array()
Static
 

absolute path of yaml files

$_saltLangPath : string

Default

NULL
 

current locale

$_saltLocale : string

Default

self::DEFAULT_LOCALE
 

application name

$_saltName : string

Default

NULL

 Constants

 

Debug only, set to TRUE for enclose every string with # *

DEBUG = FALSE 
 

default path for write generated class files

DEFAULT_CACHE_PATH = 'cache' 
 

default path for read yaml files

DEFAULT_LANG_PATH = 'lang' 
 

default language

DEFAULT_LOCALE = 'en' 
 

localization file extension

EXTENSION = 'yml' 
 

With this mode, the initLocale() method will check locale file last modification and regenerate locale class if needed. We call filemtime() and file_exists()

MODE_REGENERATE_ON_THE_FLY = 1 
 

With this mode, the initLocale() method will include class file generated by a previous call of generate() method, or failed if the class does not exists.

MODE_USE_GENERATED = 2 
 

location of Spyc main file in SALT

SPYC_PATH = 'vendor/spyc-0.5.1/Spyc.php'