Lang クラス

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:

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
パラメータ
パラメータ 規定値 説明
$file 必須 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.
返り値 void
// 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
パラメータ
パラメータ 規定値 説明
$line 必須 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.
返り値 mixed. String if found, or false if not found
// 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
パラメータ
パラメータ 規定値 説明
$line 必須 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.
返り値 void
// 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
パラメータ
パラメータ 規定値 説明
$file 必須 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 必須 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.
返り値 true if the language file was saved, false if an error occurred
// 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 set 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!

Procedural helpers

__($string, $params = array())

The __ function is an alias for Lang::get.

パラメータ
パラメータ 規定値 説明
$string 必須 The desired line's identifier.
$parameters
array()
Sets an array of parameter that might be passed to the line.
返り値 string, result from Lang::get
// Outputs Hello world
$this->output = __('hello', array('name' => 'world');

// Outputs Plop
$this->output = __('test.something');