-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
made Lingo entities work with variable injections
- Loading branch information
Showing
3 changed files
with
72 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?php | ||
/** | ||
* Created by PhpStorm. | ||
* User: emilberg | ||
* Date: 2018-10-27 | ||
* Time: 16:16 | ||
*/ | ||
|
||
namespace NorthCreationAgency\SilverStripeLingo; | ||
|
||
|
||
use SilverStripe\i18n\Data\Intl\IntlLocales; | ||
|
||
class LingoTranslator | ||
{ | ||
|
||
private static $intl_locale; | ||
|
||
/** | ||
* @param $entity | ||
* @return mixed|null | ||
*/ | ||
private function getLingoValue($localeOrLang, $entity) | ||
{ | ||
$cacheKey = LingoCache::get_cache_key($entity, $localeOrLang); | ||
|
||
//see if entity is in cache | ||
if(LingoCache::has_value($cacheKey)){ | ||
return LingoCache::get_value($cacheKey); | ||
} | ||
|
||
$lingo = Lingo::get()->filter(array( | ||
'Locale' => $localeOrLang, | ||
'Entity' => $entity | ||
))->first(); | ||
|
||
if(!$lingo){ | ||
return null; | ||
} | ||
|
||
LingoCache::set_value($cacheKey, $lingo->Value); | ||
|
||
return $lingo->Value; | ||
|
||
} | ||
|
||
public function trans($id, array $parameters = array(), $locale = null) | ||
{ | ||
if(self::$intl_locale === null){ | ||
self::$intl_locale = new IntlLocales(); | ||
} | ||
|
||
//get lang | ||
$lang = self::$intl_locale->langFromLocale($locale); | ||
//see if we got a value for lingo with lang value as "locale" | ||
$value = $this->getLingoValue($lang, $id); | ||
|
||
//see if we got a value thats defined with full locale | ||
if(!$value){ | ||
$value = $this->getLingoValue($locale, $id); | ||
} | ||
|
||
return strtr($value, $parameters); | ||
} | ||
|
||
} |