diff --git a/README.md b/README.md index a4e9508..819250f 100644 --- a/README.md +++ b/README.md @@ -2,11 +2,9 @@ Lingo lets developers define keys in yml-files in the same style as the normal lang-files but that can be viewed and translated from within the SilverStripe admin. -## TODO -* ~~Make Lingo translations work with variables~~ ## Requirements -* SilverStripe 4.2+ +* SilverStripe 4 (tested with 4.2) ## Installation diff --git a/src/messages/LingoMessageProvider.php b/src/messages/LingoMessageProvider.php index 03c03f0..85822d3 100644 --- a/src/messages/LingoMessageProvider.php +++ b/src/messages/LingoMessageProvider.php @@ -14,39 +14,8 @@ class LingoMessageProvider extends SymfonyMessageProvider { - private static $intl_locale; - - /** - * @param $entity - * @return mixed|null - */ - private function getLingoValue($entity, $locale) - { - $cacheKey = LingoCache::get_cache_key($entity, $locale); - - //see if entity is in cache - if(LingoCache::has_value($cacheKey)){ - return LingoCache::get_value($cacheKey); - } - - if(self::$intl_locale === null){ - self::$intl_locale = new IntlLocales(); - } - - $lang = self::$intl_locale->langFromLocale($locale); - $lingo = Lingo::get()->filter(array( - 'Locale' => $lang, - 'Entity' => $entity - ))->first(); - - if(!$lingo){ - return null; - } - - LingoCache::set_value($cacheKey, $lingo->Value); - - return $lingo->Value; - + private function getLingoTranslator(){ + return new LingoTranslator(); } public function translate($entity, $default, $injection) @@ -63,7 +32,9 @@ public function translate($entity, $default, $injection) //See if we have a Lingo translation if none is found if ($entity === $result) { - $result = $this->getLingoValue($entity, $locale); + //$result = $this->getLingoValue($entity, $locale); + + $result = $this->getLingoTranslator()->trans($entity, $arguments, $locale); if($result){ return $result; diff --git a/src/model/LingoTranslator.php b/src/model/LingoTranslator.php new file mode 100644 index 0000000..172d414 --- /dev/null +++ b/src/model/LingoTranslator.php @@ -0,0 +1,66 @@ +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); + } + +} \ No newline at end of file