mirror of
https://github.com/typemill/typemill.git
synced 2025-08-06 14:16:46 +02:00
i18n-light on 1.3.1
This commit is contained in:
53
system/Extensions/TwigLanguageExtension.php
Normal file
53
system/Extensions/TwigLanguageExtension.php
Normal file
@@ -0,0 +1,53 @@
|
||||
<?php
|
||||
|
||||
namespace Typemill\Extensions;
|
||||
|
||||
use Typemill\Models\WriteYaml;
|
||||
|
||||
class TwigLanguageExtension extends \Twig_Extension
|
||||
{
|
||||
protected $labels;
|
||||
|
||||
public function __construct($labels)
|
||||
{
|
||||
$this->labels = $labels;
|
||||
}
|
||||
|
||||
public function getFilters()
|
||||
{
|
||||
return [
|
||||
new \Twig_SimpleFilter('__', [$this,'translate'] )
|
||||
];
|
||||
}
|
||||
|
||||
public function getFunctions()
|
||||
{
|
||||
return [
|
||||
new \Twig_SimpleFunction('__', array($this, 'translate' ))
|
||||
];
|
||||
}
|
||||
|
||||
public function translate( $label )
|
||||
{
|
||||
// replaces spaces, dots and dash with underscores
|
||||
$string = str_replace(" ", "_", $label);
|
||||
$string = str_replace(".", "_", $string);
|
||||
$string = str_replace("-", "_", $string);
|
||||
|
||||
// transforms to uppercase
|
||||
$string = strtoupper( $string );
|
||||
|
||||
//translates the string
|
||||
$translated_label = $this->labels[$string];
|
||||
|
||||
// if the string is not present, set the original string
|
||||
if( empty($translated_label) ){
|
||||
$translated_label = $label;
|
||||
}
|
||||
|
||||
// returns the string in the set language
|
||||
return $translated_label;
|
||||
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user