Modules

Gleez_I18n
extends Kohana_I18n

Add plural support to i18n

package
Gleez\I18n
author
Sandeep Sangamreddi - Gleez
copyright
© 2013 Gleez Technologies
license
http://gleezcms.org/license

Class declared in MODPATH/gleez/classes/gleez/i18n.php on line 10.

Constants

  • None

Properties

Properties

public static string $lang

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

string(2) "en"

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_plural( ) (defined in Gleez_I18n)

This method is borrowed from the s7ncms code:

Source Code

public static function get_plural($string, $count)
{
	// Load the translation table
	$table = I18n::load(I18n::$lang);

	$key = Gleez_I18n::get_plural_key(I18n::$lang, $count);

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

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;
}

private static get_plural_key( ) (defined in Gleez_I18n)

This method is borrowed from the Gallery3 code: http://apps.sourceforge.net/trac/gallery/browser/gallery3/trunk/core/libraries/I18n.php?rev=20787#L217

Gallery - a web based photo album viewer and editor Copyright (C) 2000-2009 Bharat Mediratta

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.

Source Code

private static function get_plural_key($lang, $count) {

	// Data from CLDR 1.6 (http://unicode.org/cldr/data/common/supplemental/plurals.xml).
	// Docs: http://www.unicode.org/cldr/data/charts/supplemental/language_plural_rules.html
	switch ($lang) {
		case 'az':
		case 'fa':
		case 'hu':
		case 'ja':
		case 'ko':
		case 'my':
		case 'to':
		case 'tr':
		case 'vi':
		case 'yo':
		case 'zh':
		case 'bo':
		case 'dz':
		case 'id':
		case 'jv':
		case 'ka':
		case 'km':
		case 'kn':
		case 'ms':
		case 'th':
			return 'other';

		case 'ar':
			if ($count == 0) {
				return 'zero';
			} else if ($count == 1) {
				return 'one';
			} else if ($count == 2) {
				return 'two';
			} else if (is_int($count) AND ($i = $count % 100) >= 3 AND $i <= 10) {
				return 'few';
			} else if (is_int($count) AND ($i = $count % 100) >= 11 AND $i <= 99) {
				return 'many';
			} else {
				return 'other';
			}

		case 'pt':
		case 'am':
		case 'bh':
		case 'fil':
		case 'tl':
		case 'guw':
		case 'hi':
		case 'ln':
		case 'mg':
		case 'nso':
		case 'ti':
		case 'wa':
			if ($count == 0 OR $count == 1) {
				return 'one';
			} else {
				return 'other';
			}

		case 'fr':
			if ($count >= 0 and $count < 2) {
				return 'one';
			} else {
				return 'other';
			}

		case 'lv':
			if ($count == 0) {
				return 'zero';
			} else if ($count % 10 == 1 AND $count % 100 != 11) {
				return 'one';
			} else {
				return 'other';
			}

		case 'ga':
		case 'se':
		case 'sma':
		case 'smi':
		case 'smj':
		case 'smn':
		case 'sms':
			if ($count == 1) {
				return 'one';
			} else if ($count == 2) {
				return 'two';
			} else {
				return 'other';
			}

		case 'ro':
		case 'mo':
			if ($count == 1) {
				return 'one';
			} else if (is_int($count) AND $count == 0 AND ($i = $count % 100) >= 1 AND $i <= 19) {
				return 'few';
			} else {
				return 'other';
			}

		case 'lt':
			if (is_int($count) AND $count % 10 == 1 AND $count % 100 != 11) {
				return 'one';
			} else if (is_int($count) AND ($i = $count % 10) >= 2 AND $i <= 9 AND ($i = $count % 100) < 11 AND $i > 19) {
				return 'few';
			} else {
				return 'other';
			}

		case 'hr':
		case 'ru':
		case 'sr':
		case 'uk':
		case 'be':
		case 'bs':
		case 'sh':
			if (is_int($count) AND $count % 10 == 1 AND $count % 100 != 11) {
				return 'one';
			} else if (is_int($count) AND ($i = $count % 10) >= 2 AND $i <= 4 AND ($i = $count % 100) < 12 AND $i > 14) {
				return 'few';
			} else if (is_int($count) AND ($count % 10 == 0 OR (($i = $count % 10) >= 5 AND $i <= 9) OR (($i = $count % 100) >= 11 AND $i <= 14))) {
				return 'many';
			} else {
				return 'other';
			}

		case 'cs':
		case 'sk':
			if ($count == 1) {
				return 'one';
			} else if (is_int($count) AND $count >= 2 AND $count <= 4) {
				return 'few';
			} else {
				return 'other';
			}

		case 'pl':
			if ($count == 1) {
				return 'one';
			} else if (is_int($count) AND ($i = $count % 10) >= 2 AND $i <= 4 &&
			($i = $count % 100) < 12 AND $i > 14 AND ($i = $count % 100) < 22 AND $i > 24) {
				return 'few';
			} else {
				return 'other';
			}

		case 'sl':
			if ($count % 100 == 1) {
				return 'one';
			} else if ($count % 100 == 2) {
				return 'two';
			} else if (is_int($count) AND ($i = $count % 100) >= 3 AND $i <= 4) {
				return 'few';
			} else {
				return 'other';
			}

		case 'mt':
			if ($count == 1) {
				return 'one';
			} else if ($count == 0 OR is_int($count) AND ($i = $count % 100) >= 2 AND $i <= 10) {
				return 'few';
			} else if (is_int($count) AND ($i = $count % 100) >= 11 AND $i <= 19) {
				return 'many';
			} else {
				return 'other';
			}

		case 'mk':
			if ($count % 10 == 1) {
				return 'one';
			} else {
				return 'other';
			}

		case 'cy':
			if ($count == 1) {
				return 'one';
			} else if ($count == 2) {
				return 'two';
			} else if ($count == 8 OR $count == 11) {
				return 'many';
			} else {
				return 'other';
			}

		default: // en, de, etc.
			return $count == 1 ? 'one' : 'other';
	}
}
Documentation comments powered by Disqus