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');

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)

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 configuration 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.
返り値 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())

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.
返り値 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)

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.
返り値 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');

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');