Skip to content
This repository has been archived by the owner on Jan 30, 2020. It is now read-only.

Commit

Permalink
[zen-18] Add fallback locale
Browse files Browse the repository at this point in the history
  • Loading branch information
DASPRiD committed Jun 24, 2012
1 parent 0a9e36a commit 34adb69
Showing 1 changed file with 72 additions and 18 deletions.
90 changes: 72 additions & 18 deletions src/Translator/Translator.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,13 @@ class Translator
*/
protected $locale;

/**
* Locale to use as fallback if there is no translation.
*
* @var string
*/
protected $fallbackLocale;

/**
* Translation cache.
*
Expand Down Expand Up @@ -92,7 +99,7 @@ public function setLocale($locale)
}

/**
* Get default locale.
* Get the default locale.
*
* @return string
*/
Expand All @@ -106,13 +113,29 @@ public function getLocale()
}

/**
* Returns the set cache
* Set the fallback locale.
*
* @return CacheAdapter The set cache
* @param string $locale
* @return Translator
*/
public function getCache()
public function setFallbackLocale($locale)
{
return $this->cache;
$this->locale = $locale;
return $this;
}

/**
* Get the fallback locale.
*
* @return string
*/
public function getFallbackLocale()
{
if ($this->locale === null) {
$this->locale = Locale::getDefault();
}

return $this->locale;
}

/**
Expand All @@ -127,6 +150,16 @@ public function setCache(CacheAdapter $cache = null)
return $this;
}

/**
* Returns the set cache
*
* @return CacheAdapter The set cache
*/
public function getCache()
{
return $this->cache;
}

/**
* Retreive or set the plugin broker.
*
Expand Down Expand Up @@ -159,9 +192,16 @@ public function pluginBroker(Broker $broker = null)
*/
public function translate($message, $textDomain = 'default', $locale = null)
{
$translation = $this->getTranslatedMessage($message, $textDomain, $locale);
$locale = ($locale ?: $this->getLocale());
$translation = $this->getTranslatedMessage($message, $locale, $textDomain);

return ($translation === null || $translation === '' ? $message : $translation);
if ($translation !== null && $translation !== '') {
return $translation;
} elseif (null !== ($fallbackLocale = $this->getFallbackLocale()) && $locale !== $fallbackLocale) {
return $this->translate($message, $textDomain, $fallbackLocale);
}

return $message;
}

/**
Expand All @@ -174,12 +214,21 @@ public function translate($message, $textDomain = 'default', $locale = null)
* @param type $locale
* @return string
*/
public function translatePlural($singular, $plural, $number,
$textDomain = 'default', $locale = null
public function translatePlural(
$singular,
$plural,
$number,
$textDomain = 'default',
$locale = null
) {
$translation = $this->getTranslatedMessage($message, $textDomain, $locale);
$locale = ($locale ?: $this->getLocale());
$translation = $this->getTranslatedMessage($message, $locale, $textDomain);

if ($translation === null || $translation === '') {
if (null !== ($fallbackLocale = $this->getFallbackLocale()) && $locale !== $fallbackLocale) {
return $this->translatePlural($singular, $plural, $number, $textDomain, $fallbackLocale);
}

return ($number != 1 ? $singular : $plural);
}

Expand All @@ -198,19 +247,19 @@ public function translatePlural($singular, $plural, $number,
* Get a translated message.
*
* @param string $message
* @param string $textDomain
* @param string $locale
* @param string $textDomain
* @return string|null
*/
protected function getTranslatedMessage($message, $textDomain = 'default',
$locale = null
protected function getTranslatedMessage(
$message,
$locale = null,
$textDomain = 'default'
) {
if ($message === '') {
return '';
}

$locale = ($locale ?: $this->getLocale());

if (!isset($this->messages[$textDomain][$locale])) {
$this->loadMessages($textDomain, $locale);
}
Expand All @@ -231,8 +280,11 @@ protected function getTranslatedMessage($message, $textDomain = 'default',
* @param string $locale
* @return Translator
*/
public function addTranslationFile($type, $filename,
$textDomain = 'default', $locale = null
public function addTranslationFile(
$type,
$filename,
$textDomain = 'default',
$locale = null
) {
$locale = ($locale ?: '*');

Expand All @@ -257,7 +309,9 @@ public function addTranslationFile($type, $filename,
* @param string $textDomain
* @return Translator
*/
public function addTranslationPattern($type, $baseDir, $pattern,
public function addTranslationPattern($type,
$baseDir,
$pattern,
$textDomain = 'default'
) {
if (!isset($this->patterns[$textDomain])) {
Expand Down

0 comments on commit 34adb69

Please sign in to comment.