Lang Class
The Lang class allows you to set language variables using language files in your application.
The default language, which is en
, is set in app/config/config.php.
Use the Config set method to change the value.
Config::set('language', 'cy');
You can also define a fallback language in your configuration, which is either a language code, or an array of
language codes. The fallback is used whenever you load a language file, and the file does not exist for the
specified language.
Multi-language support
The Lang class supports multiple languages, which can be used concurrently. By default the currently set language
is used to load files and retrieve language strings, but you can switch this dynamically using the method described
above, or by specifying the required language code in one of the Lang class method calls.
Lang File Types
You can use different file layouts to store your language files. The layout type is determined by the file extension:
-
PHP. The default type. A PHP file should return an array structure.
return array('key' => 'value');
-
INI. See this page for a definition of the ini-file layout.
[group]
key=value
-
YAML. See this page for a definition of the yaml-file layout.
group:
key: value
-
JSON. See this page for a definition of the json-file layout.
{
"group" :
{
"key": "value"
}
}
-
DB. Use the following table structure:
CREATE TABLE IF NOT EXISTS `lang` (
`identifier` char(100) NOT NULL,
`language` char(10) NOT NULL,
`lang` longtext NOT NULL,
`hash` char(13) NOT NULL,
PRIMARY KEY (`identifier`, `language`)
)
By default it will use the table name 'lang'. You can override this by defining your table name in
app/config/config.php
, using the lang.table_name key.
If you don't specify a file type, Lang::load() will default to the 'php' type.
load($file, $group = null, $language = null, $overwrite = false, $reload = false)
The load method allows you to load a language file.
Static |
Yes |
Parameters |
Param |
Default |
Description |
$file |
required |
The path of the desired file. You can prefix this with a namespace to load a config file from a loaded package or module. |
$group |
null
|
Sets a language group to be used. |
$language |
null
|
Sets a specific language to load. |
$overwrite |
false |
(Optional) If set to true the loaded language items will be merged with the items already loaded in a non-recursive manner, with will overwrite array values in a multidimensional array, rather then merging them. |
$reload |
false |
(Optional) If set to true a reload of the requested language file is forced, erasing cached language items related to the lang file to be loaded. |
|
Returns |
void |
Example |
// Example of a language file:
return array(
'hello' => 'Hello :name',
'something'=> 'something :name!',
'test'=> array('hello' => 'Hello', 'something' => 'Plop') // Group
);
// Loads example.php.
// Note: If no language is set in the config, it will fallback to English.
Lang::load('example');
// Will load the given file into the 'test' group.
Lang::load('example', 'test');
// Outputs Plop
$this->output = Lang::get('test.test.something');
// Will load the example language file from the module 'foo' into the 'bar' group.
Lang::load('foo::example', 'bar');
// Will load the example language file in italian.
// If that doesn't exist, it will load the configured language
Lang::load('foo::example', 'bar', 'it');
|
get($line, $params = array(), $default = null, $language = null)
The get method allows you to get a specific line from the language file.
Static |
Yes |
Parameters |
Param |
Default |
Description |
$line |
required |
The desired line's identifier. |
$parameters |
array()
|
Sets an array of parameter that might be passed to the line. |
$default |
null
|
Value to return if $line could not be found. |
$language |
null
|
Language code for which this line should be retrieved. If not given the current active language will be used. |
|
Returns |
mixed. String if found, or false if not found |
Example |
// Given this lang file
return array('hello' => 'Hello :name');
// Outputs Hello world
$this->output = Lang::get('hello', array('name' => 'world'));
// Outputs Plop
$this->output = Lang::get('test.something');
|
set($line, $value, $group = null, $language = null)
The set method allows you to set a specific line to the language file.
Static |
Yes |
Parameters |
Param |
Default |
Description |
$line |
required |
The desired line's identifier. |
$value |
array()
|
Sets the value of the line. |
$group |
null
|
Sets a language group to be used. |
$language |
null
|
Language code for which this line should be set. If not given the current active language will be used. |
|
Returns |
void |
Example |
// Returns true
Lang::set('hello', 'Ahoy!');
// Using groups
Lang:set('hello', 'Ahoy!', 'test');
// Will also work as above
Lang::load('test', 'test');
Lang::set('hello', 'Ahoy!');
|
save($file, $lang, $language = null)
The save method saves a language file into the system. It searches through the language directories for the requested file
in the requested language. If no existing file is found, the language file is created in the APPPATH lang directory.
Static |
Yes |
Parameters |
Param |
Default |
Description |
$file |
required |
The path to the config file, relative to the config directory. Do not include the file extension (".php" is assumed). You can prefix this with a namespace to load a config file from a loaded package or module. |
$lang |
required |
If this is a string, it specifies a group name to save. If it is an array, it is assumed to contain the language strings to be saved. |
$language |
null |
The language code to save the file too. If not given, it will be saved to whatever the current set language code is. |
|
Returns |
true if the language file was saved, false if an error occurred |
Example |
// This loads the "custom" language file in a group named "foo".
Lang::load('custom', 'foo');
// update some language item
Lang::set('foo.key', $value);
// save the updated language group 'foo' (note: it will save everything in that group!)
Lang::save('custom', 'foo');
// save the updated language group 'bar' to the language file 'custom' in the module 'foo'
Lang::save('foo::custom', 'bar');
|
delete($item, $group = null, $language = null)
The delete method allows you to delete a specific line from the loaded language file.
Static |
Yes |
Parameters |
Param |
Default |
Description |
$item |
required |
The desired line's identifier. |
$group |
null
|
Sets a language group to be used. |
$language |
null
|
Language code for which this line should be deleted. If not given the current active language will be used. |
|
Returns |
void |
Example |
// delete the 'hello' language key
Lang::delete('hello');
|
Note that this deletes the language item stored in the language class, it does NOT delete it from the language file on disk!
get_lang()
The get_lang method allows you to retrieve currently active language. Typically it will be same as Config::get('language').
Static |
Yes |
Parameters |
None |
Returns |
String with currently active language. |
Example |
// will return currently active language, most likely 'en'
Lang::get_lang();
|
set_lang($language, $reload = false)
The set_lang method allows you to set the new active language, and optionally
it attempts to reload all language file already loaded for that language.
This will call Config::set('language') to set
the new language globally. Typically it will be used to set or switch to a user configurable language.
Static |
Yes |
Parameters |
Param |
Default |
Description |
$language |
required |
Language code to set. |
$reload |
false |
(Optional) If set to true all already loaded language files will be reloaded for the new language set. |
|
Returns |
Bool indicating success or failure. |
Example |
// switch to French, and reload all loaded language files
Lang::set_lang('fr', true);
|
Procedural helpers
__($string, $params = array())
The __ function is an alias for Lang::get.
Parameters |
Param |
Default |
Description |
$string |
required |
The desired line's identifier. |
$parameters |
array()
|
Sets an array of parameter that might be passed to the line. |
|
Returns |
string, result from Lang::get |
Example |
// Outputs Hello world
$this->output = __('hello', array('name' => 'world');
// Outputs Plop
$this->output = __('test.something');
|