Adding doc comments to translation functions in basics.php

Refactoring I18n class to use one locale directory for an application.
Translations will be located in app/locales/[LANGUAGE]/LC_MESSAGES/[CONTROLLER].mo or [CONTROLLER].po
If not found app/locales/[LANGUAGE]/LC_MESSAGES/default.mo or default.po
All core translations will be located in cake/locales/[LANGUAGE]/LC_MESSAGES/

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@4122 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
phpnut 2006-12-22 20:04:15 +00:00
parent 4c586b7893
commit d23de28349
3 changed files with 76 additions and 64 deletions

View file

@ -1039,21 +1039,20 @@
} }
/** /**
* *
* Returns a translated string if one is found, * Returns a translated string if one is found, or the submitted message if not found.
* or the submitted message if not found.
* *
* @param unknown_type $msg * @param string $msg
* @param unknown_type $return * @param boolean $return
* @return unknown * @return translated string if $return is false string will be echoed
*/ */
function __($msg, $return = null) { function __($msg, $return = false) {
if(!class_exists('I18n')) { if(!class_exists('I18n')) {
uses('i18n'); uses('i18n');
} }
$calledFrom = debug_backtrace(); $calledFrom = debug_backtrace();
$dir = dirname($calledFrom[0]['file']); $dir = dirname($calledFrom[0]['file']);
if(is_null($return)) { if($return === false) {
echo I18n::translate($msg, null, null, null, null, $dir); echo I18n::translate($msg, null, null, null, null, $dir);
} else { } else {
return I18n::translate($msg, null, null, null, null, $dir); return I18n::translate($msg, null, null, null, null, $dir);
@ -1064,20 +1063,20 @@
* Returns correct plural form of message identified by $msg1 and $msg2 for count $count. * Returns correct plural form of message identified by $msg1 and $msg2 for count $count.
* Some languages have more than one form for plural messages dependent on the count. * Some languages have more than one form for plural messages dependent on the count.
* *
* @param unknown_type $msg1 * @param string $msg1
* @param unknown_type $msg2 * @param string $msg2
* @param unknown_type $count * @param integer $count
* @param unknown_type $return * @param boolean $return
* @return unknown * @return plural form of translated string if $return is false string will be echoed
*/ */
function __n($msg1, $msg2, $count, $return = null) { function __n($msg1, $msg2, $count, $return = false) {
if(!class_exists('I18n')) { if(!class_exists('I18n')) {
uses('i18n'); uses('i18n');
} }
$calledFrom = debug_backtrace(); $calledFrom = debug_backtrace();
$dir = dirname($calledFrom[0]['file']); $dir = dirname($calledFrom[0]['file']);
if(is_null($return)) { if($return === false) {
echo I18n::translate($msg1, $msg2, null, null, $count, $dir); echo I18n::translate($msg1, $msg2, null, null, $count, $dir);
} else { } else {
return I18n::translate($msg1, $msg2, null, null, $count, $dir); return I18n::translate($msg1, $msg2, null, null, $count, $dir);
@ -1089,21 +1088,21 @@
* Returns correct plural form of message identified by $msg1 and $msg2 for count $count * Returns correct plural form of message identified by $msg1 and $msg2 for count $count
* from domain $domain * from domain $domain
* *
* @param unknown_type $domain * @param string $domain
* @param unknown_type $msg1 * @param string $msg1
* @param unknown_type $msg2 * @param string $msg2
* @param unknown_type $count * @param integer $count
* @param unknown_type $return * @param boolean $return
* @return unknown * @return plural form of translated string if $return is false string will be echoed
*/ */
function __dn($domain, $msg1, $msg2, $count, $return = null) { function __dn($domain, $msg1, $msg2, $count, $return = false) {
if(!class_exists('I18n')) { if(!class_exists('I18n')) {
uses('i18n'); uses('i18n');
} }
$calledFrom = debug_backtrace(); $calledFrom = debug_backtrace();
$dir = dirname($calledFrom[0]['file']); $dir = dirname($calledFrom[0]['file']);
if(is_null($return)) { if($return === false) {
echo I18n::translate($msg1, $msg2, $domain, null, $count, $dir);; echo I18n::translate($msg1, $msg2, $domain, null, $count, $dir);;
} else { } else {
return I18n::translate($msg1, $msg2, $domain, null, $count, $dir); return I18n::translate($msg1, $msg2, $domain, null, $count, $dir);
@ -1128,22 +1127,22 @@
* LC_MESSAGES 5 * LC_MESSAGES 5
* LC_ALL 6 * LC_ALL 6
* *
* @param unknown_type $domain * @param string $domain
* @param unknown_type $msg1 * @param string $msg1
* @param unknown_type $msg2 * @param string $msg2
* @param unknown_type $count * @param integer $count
* @param unknown_type $category * @param string $category
* @param unknown_type $return * @param boolean $return
* @return unknown * @return plural form of translated string if $return is false string will be echoed
*/ */
function __dcn($domain, $msg1, $msg2, $count, $category, $return = null) { function __dcn($domain, $msg1, $msg2, $count, $category, $return = false) {
if(!class_exists('I18n')) { if(!class_exists('I18n')) {
uses('i18n'); uses('i18n');
} }
$calledFrom = debug_backtrace(); $calledFrom = debug_backtrace();
$dir = dirname($calledFrom[0]['file']); $dir = dirname($calledFrom[0]['file']);
if(is_null($return)) { if($return === false) {
echo I18n::translate($msg1, $msg2, $domain, $category, $count, $dir); echo I18n::translate($msg1, $msg2, $domain, $category, $count, $dir);
} else { } else {
return I18n::translate($msg1, $msg2, $domain, $category, $count, $dir); return I18n::translate($msg1, $msg2, $domain, $category, $count, $dir);
@ -1166,20 +1165,20 @@
* LC_MESSAGES 5 * LC_MESSAGES 5
* LC_ALL 6 * LC_ALL 6
* *
* @param unknown_type $domain * @param string $domain
* @param unknown_type $msg * @param string $msg
* @param unknown_type $category * @param string $category
* @param unknown_type $return * @param boolean $return
* @return unknown * @return translated string if $return is false string will be echoed
*/ */
function __dc($domain, $msg, $category, $return = null) { function __dc($domain, $msg, $category, $return = false) {
if(!class_exists('I18n')) { if(!class_exists('I18n')) {
uses('i18n'); uses('i18n');
} }
$calledFrom = debug_backtrace(); $calledFrom = debug_backtrace();
$dir = dirname($calledFrom[0]['file']); $dir = dirname($calledFrom[0]['file']);
if(is_null($return)) { if($return === false) {
echo I18n::translate($msg, null, $domain, $category, null, $dir); echo I18n::translate($msg, null, $domain, $category, null, $dir);
} else { } else {
return I18n::translate($msg, null, $domain, $category, null, $dir); return I18n::translate($msg, null, $domain, $category, null, $dir);
@ -1194,14 +1193,14 @@
* @param unknown_type $return * @param unknown_type $return
* @return unknown * @return unknown
*/ */
function __d($domain, $msg, $return = null) { function __d($domain, $msg, $return = false) {
if(!class_exists('I18n')) { if(!class_exists('I18n')) {
uses('i18n'); uses('i18n');
} }
$calledFrom = debug_backtrace(); $calledFrom = debug_backtrace();
$dir = dirname($calledFrom[0]['file']); $dir = dirname($calledFrom[0]['file']);
if(is_null($return)) { if($return === false) {
echo I18n::translate($msg, null, $domain, null, null, $dir); echo I18n::translate($msg, null, $domain, null, null, $dir);
} else { } else {
return I18n::translate($msg, null, $domain, null, null, $dir); return I18n::translate($msg, null, $domain, null, null, $dir);

View file

@ -95,7 +95,7 @@ class I18n extends Object {
$_this =& I18n::getInstance(); $_this =& I18n::getInstance();
$language = Configure::read('Config.language'); $language = Configure::read('Config.language');
if(!empty($_SESSION['Config']['locale'])) { if($language === null && !empty($_SESSION['Config']['locale'])) {
$_this->locale = $_SESSION['Config']['locale']; $_this->locale = $_SESSION['Config']['locale'];
} else{ } else{
$_this->__l10n->get($language); $_this->__l10n->get($language);
@ -291,34 +291,39 @@ class I18n extends Object {
$_this =& I18n::getInstance(); $_this =& I18n::getInstance();
$_this->__noLocal = true; $_this->__noLocal = true;
$searchPath[] = VIEWS . $domain . DS . 'locale'; $searchPath[] = APP . 'locale';
$searchPath[] = CAKE . 'locale'; $searchPath[] = CAKE_CORE_INCLUDE_PATH . DS . 'cake' . DS . 'locale';
foreach (explode(",",$_this->locale) as $d) {
$d = trim($d);
$d = strtok($d, "@.-+=%:; ");
if (strlen($d)) {
$dir[] = $d;
}
if (strpos($d, "_")) {
$dir[] = strtok($d, "_");
}
}
foreach ($searchPath as $directory) { foreach ($searchPath as $directory) {
foreach ($dir as $lang) { foreach ($_this->__l10n->languagePath as $lang) {
$file = $directory . DS . $lang . DS . 'LC_MESSAGES' . DS . $domain; $file = $directory . DS . $lang . DS . 'LC_MESSAGES' . DS . $domain;
$default = APP . 'locale'. DS . $lang . DS . 'LC_MESSAGES' . DS . 'default';
$core = CAKE_CORE_INCLUDE_PATH . DS . 'cake' . DS . 'locale'. DS . $lang . DS . 'LC_MESSAGES' . DS . 'core';
if (file_exists($fn = "$file.mo") && ($f = fopen($fn, "rb"))) { if (file_exists($fn = "$file.mo") && ($f = fopen($fn, "rb"))) {
$_this->__loadMo($f, $domain); $_this->__loadMo($f, $domain);
$_this->__noLocal = null; $_this->__noLocal = null;
break 2; break 2;
} elseif (file_exists($fn = "$default.mo") && ($f = fopen($fn, "rb"))) {
$_this->__loadMo($f, $domain);
$_this->__noLocal = null;
break 2;
} elseif (file_exists($fn = "$file.po") && ($f = fopen($fn, "r"))) { } elseif (file_exists($fn = "$file.po") && ($f = fopen($fn, "r"))) {
$_this->__loadPo($f, $domain); $_this->__loadPo($f, $domain);
$_this->__noLocal = null; $_this->__noLocal = null;
break 2; break 2;
} elseif (file_exists($fn = "$default.po") && ($f = fopen($fn, "r"))) {
$_this->__loadPo($f, $domain);
$_this->__noLocal = null;
break 2;
} elseif (file_exists($fn = "$core.mo") && ($f = fopen($fn, "rb"))) {
$_this->__loadMo($f, $domain);
$_this->__noLocal = null;
break 2;
} elseif (file_exists($fn = "$core.po") && ($f = fopen($fn, "r"))) {
$_this->__loadPo($f, $domain);
$_this->__noLocal = null;
break 2;
} }
} }
} }

View file

@ -69,7 +69,7 @@ class L10n extends Object {
* @var string * @var string
* @access public * @access public
*/ */
var $winLocale = 'eng'; var $default = null;
/** /**
* Enter description here... * Enter description here...
* *
@ -301,6 +301,9 @@ class L10n extends Object {
* *
*/ */
function __construct() { function __construct() {
if (defined('DEFAULT_LANGUAGE')) {
$this->default = DEFAULT_LANGUAGE;
}
parent::__construct(); parent::__construct();
} }
/** /**
@ -325,9 +328,8 @@ class L10n extends Object {
function __setLanguage($language = null) { function __setLanguage($language = null) {
if ((!is_null($language)) && (isset($this->__l10nCatalog[$this->__l10nMap[$language]]))) { if ((!is_null($language)) && (isset($this->__l10nCatalog[$this->__l10nMap[$language]]))) {
$this->language = $this->__l10nCatalog[$this->__l10nMap[$language]]['language']; $this->language = $this->__l10nCatalog[$this->__l10nMap[$language]]['language'];
$this->languagePath = array(0 => $this->__l10nCatalog[$language]['locale'], $this->languagePath = array(0 => $this->__l10nCatalog[$this->__l10nMap[$language]]['locale'],
1 => $this->__l10nCatalog[$language]['localeFallback'], 1 => $this->__l10nCatalog[$this->__l10nMap[$language]]['localeFallback']);
2 => $this->__l10nCatalog[DEFAULT_LANGUAGE]['localeFallback']);
$this->lang = $language; $this->lang = $language;
$this->locale = $this->__l10nCatalog[$this->__l10nMap[$language]]['locale']; $this->locale = $this->__l10nCatalog[$this->__l10nMap[$language]]['locale'];
} elseif (defined('DEFAULT_LANGUAGE')) { } elseif (defined('DEFAULT_LANGUAGE')) {
@ -337,6 +339,9 @@ class L10n extends Object {
$this->lang = DEFAULT_LANGUAGE; $this->lang = DEFAULT_LANGUAGE;
$this->locale = $this->__l10nCatalog[$this->__l10nMap[DEFAULT_LANGUAGE]]['locale']; $this->locale = $this->__l10nCatalog[$this->__l10nMap[DEFAULT_LANGUAGE]]['locale'];
} }
if($this->default) {
$this->languagePath = array(2 => $this->__l10nCatalog[$this->default]['localeFallback']);
}
} }
/** /**
* Enter description here... * Enter description here...
@ -344,14 +349,17 @@ class L10n extends Object {
*/ */
function __autoLanguage() { function __autoLanguage() {
$_detectableLanguages = split ('[,;]', env('HTTP_ACCEPT_LANGUAGE')); $_detectableLanguages = split ('[,;]', env('HTTP_ACCEPT_LANGUAGE'));
foreach ($_detectableLanguages as $langKey => $key) { foreach ($_detectableLanguages as $key => $langKey) {
if (isset($this->__l10nCatalog[$langKey])) { if (isset($this->__l10nCatalog[$langKey])) {
$this->language = $this->__l10nCatalog[$langKey]['language']; $this->language = $this->__l10nCatalog[$langKey]['language'];
$this->languagePath = array(0 => $this->__l10nCatalog[$langKey]['locale'], $this->languagePath = array(0 => $this->__l10nCatalog[$langKey]['locale'],
1 => $this->__l10nCatalog[$langKey]['localeFallback'], 1 => $this->__l10nCatalog[$langKey]['localeFallback']);
2 => $this->__l10nCatalog[DEFAULT_LANGUAGE]['localeFallback']);
$this->lang = $langKey; $this->lang = $langKey;
$this->locale = $this->__l10nCatalog[$langKey]['locale']; $this->locale = $this->__l10nCatalog[$langKey]['locale'];
if($this->default) {
$this->languagePath = array(2 => $this->__l10nCatalog[$this->default]['localeFallback']);
}
break; break;
} }
} }