function i18n($params, $cache = false) { if (!is_array($params)) { $params = explode('/', trim($params, '/')); } $this->init(); $cacheFile = implode(DS, $params); $params = $this->normalize($params); $jsFile = $this->parseFile($params); $lang = $this->parseLang($params); $L10n = new L10n(); if (!$L10n->map($lang)) { $lang = null; } $L10n->get($lang); $sourceJsFile = $this->paths['source'] . $jsFile; if (file_exists($sourceJsFile)) { ob_start(); include $sourceJsFile; $js = ob_get_clean(); if ($cache) { $this->write($cacheFile, $js); } return $js; } return false; }
/** * Used by the translation functions in basics.php * Can also be used like I18n::translate(); but only if the App::import('I18n'); has been used to load the class. * * @param string $singular String to translate * @param string $plural Plural string (if any) * @param string $domain Domain * @param string $category Category * @param integer $count Count * @return string translated strings. * @access public */ public static function translate($singular, $plural = null, $domain = null, $category = 6, $count = null) { self::init(); if (strpos($singular, "\r\n") !== false) { $singular = str_replace("\r\n", "\n", $singular); } if ($plural !== null && strpos($plural, "\r\n") !== false) { $plural = str_replace("\r\n", "\n", $plural); } if (is_numeric($category)) { self::$category = self::$__categories[$category]; } $language = Configure::read('Config.language'); if (!empty($_SESSION['Config']['language'])) { $language = $_SESSION['Config']['language']; } if (self::$__lang && self::$__lang !== $language || !self::$__lang) { self::$__lang = self::$L10n->get($language); } if (is_null($domain)) { $domain = 'default'; } self::$domain = $domain . '_' . self::$L10n->locale; if (empty(self::$__domains)) { self::$__domains = Cache::read(self::$domain, '_cake_core_'); } if (!isset(self::$__domains[self::$category][self::$__lang][$domain])) { self::__bindTextDomain($domain); self::$__cache = true; } if (!isset($count)) { $plurals = 0; } elseif (!empty(self::$__domains[self::$category][self::$__lang][$domain]["%plural-c"]) && self::$__noLocale === false) { $header = self::$__domains[self::$category][self::$__lang][$domain]["%plural-c"]; $plurals = self::__pluralGuess($header, $count); } else { if ($count != 1) { $plurals = 1; } else { $plurals = 0; } } if (!empty(self::$__domains[self::$category][self::$__lang][$domain][$singular])) { if (($trans = self::$__domains[self::$category][self::$__lang][$domain][$singular]) || $plurals && ($trans = self::$__domains[self::$category][self::$__lang][$domain][$plural])) { if (is_array($trans)) { if (isset($trans[$plurals])) { $trans = $trans[$plurals]; } } if (strlen($trans)) { $singular = $trans; return $singular; } } } if (!empty($plurals)) { return $plural; } return $singular; }
/** * testGet method * * @access public * @return void */ function testGet() { $l10n =& new L10n(); // Catalog Entry $l10n->get('en'); $this->assertEqual($l10n->language, 'English'); $this->assertEqual($l10n->languagePath, array('eng', 'eng')); $this->assertEqual($l10n->locale, 'eng'); // Map Entry $l10n->get('eng'); $this->assertEqual($l10n->language, 'English'); $this->assertEqual($l10n->languagePath, array('eng', 'eng')); $this->assertEqual($l10n->locale, 'eng'); // Catalog Entry $l10n->get('en-ca'); $this->assertEqual($l10n->language, 'English (Canadian)'); $this->assertEqual($l10n->languagePath, array('en_ca', 'eng')); $this->assertEqual($l10n->locale, 'en_ca'); // Default Entry define('DEFAULT_LANGUAGE', 'en-us'); $l10n->get('use_default'); $this->assertEqual($l10n->language, 'English (United States)'); $this->assertEqual($l10n->languagePath, array('en_us', 'eng')); $this->assertEqual($l10n->locale, 'en_us'); $l10n->get('es'); $l10n->get(''); $this->assertEqual($l10n->lang, 'en-us'); // Using $this->default $l10n = new L10n(); $l10n->get('use_default'); $this->assertEqual($l10n->language, 'English (United States)'); $this->assertEqual($l10n->languagePath, array('en_us', 'eng', 'eng')); $this->assertEqual($l10n->locale, 'en_us'); }
public static function get() { if (!self::$inst) { $lang = L10n::get('messages')->getLanguage(); self::$inst = new self($lang); } return self::$inst; }
/** * testGetAutoLanguage method * * @access public * @return void */ function testGetAutoLanguage() { $__SERVER = $_SERVER; $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'inexistent,en-ca'; $l10n = new L10n(); $l10n->get(); $this->assertEqual($l10n->language, 'English (Canadian)'); $this->assertEqual($l10n->languagePath, array('en_ca', 'eng', 'eng')); $this->assertEqual($l10n->locale, 'en_ca'); $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'es_mx'; $l10n->get(); $this->assertEqual($l10n->language, 'Spanish (Mexican)'); $this->assertEqual($l10n->languagePath, array('es_mx', 'spa', 'eng')); $this->assertEqual($l10n->locale, 'es_mx'); $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'en_xy,en_ca'; $l10n->get(); $this->assertEqual($l10n->language, 'English'); $this->assertEqual($l10n->languagePath, array('eng', 'eng', 'eng')); $this->assertEqual($l10n->locale, 'eng'); $_SERVER = $__SERVER; }
/** * testGetAutoLanguage method * * @return void */ public function testGetAutoLanguage() { $serverBackup = $_SERVER; $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'inexistent,en-ca'; $localize = new L10n(); $localize->get(); $this->assertEquals('English (Canadian)', $localize->language); $this->assertEquals(array('en_ca', 'eng', 'eng'), $localize->languagePath); $this->assertEquals('en_ca', $localize->locale); $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'es_mx'; $localize->get(); $this->assertEquals('Spanish (Mexican)', $localize->language); $this->assertEquals(array('es_mx', 'spa', 'eng'), $localize->languagePath); $this->assertEquals('es_mx', $localize->locale); $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'en_xy,en_ca'; $localize->get(); $this->assertEquals('English', $localize->language); $this->assertEquals(array('eng', 'eng', 'eng'), $localize->languagePath); $this->assertEquals('eng', $localize->locale); $_SERVER = $serverBackup; }
/** * testGet method with deprecated constant DEFAULT_LANGUAGE * * @return void */ public function testGetWithDeprecatedConstant() { $this->skipIf(defined('DEFAULT_LANGUAGE'), 'Cannot re-define already defined constant.'); define('DEFAULT_LANGUAGE', 'en-us'); $localize = new L10n(); $lang = $localize->get('use_default'); $this->assertEquals('en-us', $lang); $this->assertEquals('English (United States)', $localize->language); $this->assertEquals(array('en_us', 'eng'), $localize->languagePath); $this->assertEquals('en_us', $localize->locale); $localize = new L10n(); $lang = $localize->get(); $this->assertEquals('en-us', $lang); $this->assertEquals('English (United States)', $localize->language); $this->assertEquals(array('en_us', 'eng'), $localize->languagePath); $this->assertEquals('en_us', $localize->locale); }
/** * testGet method * * @access public * @return void */ function testGet() { $l10n =& new L10n(); // Catalog Entry $l10n->get('en'); $result = $l10n->language; $expected = 'English'; $this->assertEqual($result, $expected); $result = $l10n->languagePath; $expected = array('eng', 'eng'); $this->assertEqual($result, $expected); $result = $l10n->locale; $expected = 'eng'; $this->assertEqual($result, $expected); // Map Entry $l10n->get('eng'); $result = $l10n->language; $expected = 'English'; $this->assertEqual($result, $expected); $result = $l10n->languagePath; $expected = array('eng', 'eng'); $this->assertEqual($result, $expected); $result = $l10n->locale; $expected = 'eng'; $this->assertEqual($result, $expected); // Catalog Entry $l10n->get('en-ca'); $result = $l10n->language; $expected = 'English (Canadian)'; $this->assertEqual($result, $expected); $result = $l10n->languagePath; $expected = array('en_ca', 'eng'); $this->assertEqual($result, $expected); $result = $l10n->locale; $expected = 'en_ca'; $this->assertEqual($result, $expected); // Default Entry define('DEFAULT_LANGUAGE', 'en-us'); $l10n->get('use_default'); $result = $l10n->language; $expected = 'English (United States)'; $this->assertEqual($result, $expected); $result = $l10n->languagePath; $expected = array('en_us', 'eng'); $this->assertEqual($result, $expected); $result = $l10n->locale; $expected = 'en_us'; $this->assertEqual($result, $expected); // Using $this->default $l10n = new L10n(); $l10n->get('use_default'); $result = $l10n->language; $expected = 'English (United States)'; $this->assertEqual($result, $expected); $result = $l10n->languagePath; $expected = array('en_us', 'eng', 'eng'); $this->assertEqual($result, $expected); $result = $l10n->locale; $expected = 'en_us'; $this->assertEqual($result, $expected); }
/** * testGetAutoLanguage method * * @access public * @return void */ function testGetAutoLanguage() { $__SERVER = $_SERVER; $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'inexistent,en-ca'; $l10n = new L10n(); $l10n->get(); $result = $l10n->language; $expected = 'English (Canadian)'; $this->assertEqual($result, $expected); $result = $l10n->languagePath; $expected = array('en_ca', 'eng', 'eng'); $this->assertEqual($result, $expected); $result = $l10n->locale; $expected = 'en_ca'; $this->assertEqual($result, $expected); $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'es_mx'; $l10n->get(); $result = $l10n->language; $expected = 'Spanish (Mexican)'; $this->assertEqual($result, $expected); $result = $l10n->languagePath; $expected = array('es_mx', 'spa', 'eng'); $this->assertEqual($result, $expected); $result = $l10n->locale; $expected = 'es_mx'; $this->assertEqual($result, $expected); $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'en_xy,en_ca'; $l10n->get(); $result = $l10n->language; $expected = 'English'; $this->assertEqual($result, $expected); $result = $l10n->languagePath; $expected = array('eng', 'eng', 'eng'); $this->assertEqual($result, $expected); $result = $l10n->locale; $expected = 'eng'; $this->assertEqual($result, $expected); $_SERVER = $__SERVER; }