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

Generate class files for all available locales.

generate() 

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.

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

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

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

Build PHP code for $data

buildData(array<mixed,mixed> $data, string $keyPrefix = '') : 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

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) : static

Parameters

$locale

string

locale to initialize

Returns

staticcurrent I18n or NULL if locale does not exists

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 regenerate class files each time

DEBUG_FORCE_GENERATION = 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' 
 

Internal mode, used by generate() method

MODE_FORCE_REGENERATE = -1 
 

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'