mirror of
https://github.com/yiisoft/yii2.git
synced 2026-03-21 06:33:43 +01:00
Fixes #2079:
- i18n now falls back to `en` from `en-US` if message translation isn't found - View now falls back to `en` from `en-US` if file not found - Default `sourceLanguage` and `language` are now `en`
This commit is contained in:
@@ -50,14 +50,51 @@ class GettextMessageSource extends MessageSource
|
||||
|
||||
/**
|
||||
* Loads the message translation for the specified language and category.
|
||||
* Child classes should override this method to return the message translations of
|
||||
* the specified language and category.
|
||||
* If translation for specific locale code such as `en-US` isn't found it
|
||||
* tries more generic `en`.
|
||||
*
|
||||
* @param string $category the message category
|
||||
* @param string $language the target language
|
||||
* @return array the loaded messages. The keys are original messages, and the values
|
||||
* are translated messages.
|
||||
*/
|
||||
protected function loadMessages($category, $language)
|
||||
{
|
||||
$messageFile = $this->getMessageFilePath($category, $language);
|
||||
$messages = $this->loadMessagesFromFile($messageFile);
|
||||
|
||||
$fallbackLanguage = substr($language, 0, 2);
|
||||
if ($fallbackLanguage != $language) {
|
||||
$fallbackMessageFile = $this->getMessageFilePath($category, $fallbackLanguage);
|
||||
$fallbackMessages = $this->loadMessagesFromFile($fallbackMessageFile);
|
||||
|
||||
if ($messages === null && $fallbackMessages === null && $fallbackLanguage != $this->sourceLanguage) {
|
||||
Yii::error("The message file for category '$category' does not exist: $messageFile Fallback file does not exist as well: $fallbackMessageFile", __METHOD__);
|
||||
} else if (empty($messages)) {
|
||||
return $fallbackMessages;
|
||||
} else if (!empty($fallbackMessages)) {
|
||||
foreach ($messages as $key => $value) {
|
||||
if (empty($value) && !empty($fallbackMessages[$key])) {
|
||||
$messages[$key] = $fallbackMessages[$key];
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if ($messages === null) {
|
||||
Yii::error("The message file for category '$category' does not exist: $messageFile", __METHOD__);
|
||||
}
|
||||
}
|
||||
return (array)$messages;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns message file path for the specified language and category.
|
||||
*
|
||||
* @param string $category the message category
|
||||
* @param string $language the target language
|
||||
* @return string path to message file
|
||||
*/
|
||||
protected function getMessageFilePath($category, $language)
|
||||
{
|
||||
$messageFile = Yii::getAlias($this->basePath) . '/' . $language . '/' . $this->catalog;
|
||||
if ($this->useMoFile) {
|
||||
@@ -65,7 +102,17 @@ class GettextMessageSource extends MessageSource
|
||||
} else {
|
||||
$messageFile .= static::PO_FILE_EXT;
|
||||
}
|
||||
return $messageFile;
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads the message translation for the specified language and category or returns null if file doesn't exist.
|
||||
*
|
||||
* @param $messageFile string path to message file
|
||||
* @return array|null array of messages or null if file not found
|
||||
*/
|
||||
protected function loadMessagesFromFile($messageFile)
|
||||
{
|
||||
if (is_file($messageFile)) {
|
||||
if ($this->useMoFile) {
|
||||
$gettextFile = new GettextMoFile(['useBigEndian' => $this->useBigEndian]);
|
||||
@@ -78,8 +125,7 @@ class GettextMessageSource extends MessageSource
|
||||
}
|
||||
return $messages;
|
||||
} else {
|
||||
Yii::error("The message file for category '$category' does not exist: $messageFile", __METHOD__);
|
||||
return [];
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user