Modules

I18n
extends Kohana_I18n

Internationalization (i18n) class. Provides language loading and translation methods without dependencies on gettext.

Typically this class would never be used directly, but used via the __() function, which loads the message and replaces parameters:

// Display a translated message
echo __('Hello, world');

// With parameter replacement
echo __('Hello, :user', array(':user' => $username));
package
Kohana
category
Base
author
Kohana Team
copyright
© 2008-2012 Kohana Team
license
http://kohanaframework.org/license

Class declared in SYSPATH/classes/I18n.php on line 3.

Constants

  • None

Properties

Methods

Properties

public static string $lang

target language: en-us, es-es, zh-cn, etc

string(5) "en-us"

public static string $source

source language: en-us, es-es, zh-cn, etc

string(5) "en-us"

protected static array $_cache

cache of loaded languages

array(0) 

Methods

public static get( string $string [, string $lang = NULL ] ) (defined in Kohana_I18n)

Returns translation of a string. If no translation exists, the original string will be returned. No parameters are replaced.

$hello = I18n::get('Hello friends, my name is :name');

Parameters

  • string $string required - Text to translate
  • string $lang = NULL - Target language

Return Values

  • string

Source Code

public static function get($string, $lang = NULL)
{
	if ( ! $lang)
	{
		// Use the global target language
		$lang = I18n::$lang;
	}

	// Load the translation table for this language
	$table = I18n::load($lang);

	// Return the translated string if it exists
	return isset($table[$string]) ? $table[$string] : $string;
}

public static lang( [ string $lang = NULL ] ) (defined in Kohana_I18n)

Get and set the target language.

// Get the current language
$lang = I18n::lang();

// Change the current language to Spanish
I18n::lang('es-es');

Parameters

  • string $lang = NULL - New language setting

Tags

  • Since - 3.0.2

Return Values

  • string

Source Code

public static function lang($lang = NULL)
{
	if ($lang)
	{
		// Normalize the language
		I18n::$lang = strtolower(str_replace(array(' ', '_'), '-', $lang));
	}

	return I18n::$lang;
}

public static load( string $lang ) (defined in Kohana_I18n)

Returns the translation table for a given language.

// Get all defined Spanish messages
$messages = I18n::load('es-es');

Parameters

  • string $lang required - Language to load

Return Values

  • array

Source Code

public static function load($lang)
{
	if (isset(I18n::$_cache[$lang]))
	{
		return I18n::$_cache[$lang];
	}

	// New translation table
	$table = array();

	// Split the language: language, region, locale, etc
	$parts = explode('-', $lang);

	do
	{
		// Create a path for this set of parts
		$path = implode(DIRECTORY_SEPARATOR, $parts);

		if ($files = Kohana::find_file('i18n', $path, NULL, TRUE))
		{
			$t = array();
			foreach ($files as $file)
			{
				// Merge the language strings into the sub table
				$t = array_merge($t, Kohana::load($file));
			}

			// Append the sub table, preventing less specific language
			// files from overloading more specific files
			$table += $t;
		}

		// Remove the last part
		array_pop($parts);
	}
	while ($parts);

	// Cache the translation table locally
	return I18n::$_cache[$lang] = $table;
}