Skip to content

Commit

Permalink
made Lingo entities work with variable injections
Browse files Browse the repository at this point in the history
  • Loading branch information
emilberg committed Oct 27, 2018
1 parent 88598ee commit afdd023
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 37 deletions.
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
39 changes: 5 additions & 34 deletions src/messages/LingoMessageProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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;
Expand Down
66 changes: 66 additions & 0 deletions src/model/LingoTranslator.php
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);
}

}

0 comments on commit afdd023

Please sign in to comment.