I think the multi-language feature could be better, if it also worked like this:
Yes, the idea is to have the Fallback string directly in the template. I'm working on a large project and having to map all the text on the site to variables is really time consuming. Having the string in whatever default language you have directly in the template was a huge time saver for me.
in the template: {{ __('my text here with spaces') }}
DICT/en.php is the same, but supports spaces (or one could use _en.php for backwards compatibility)
Then, I have this in index.php
$f3->LANGUAGE = $f3->ml->current; //previously loaded the Multilang::instance()
$f3->TEXT = include($f3->LOCALES."_".$f3->LANGUAGE.".php");
function __( $text) {
global $f3;
if (isset($f3->TEXT[$text])) { //for simplicity, the default language file has an empty value
if ($f3->TEXT[$text]) {
return $f3->TEXT[$text];
} else {
return $text;//display the original text
}
} else {
return '_'.$text.'_';//default is whatever is in the template, but add underscores to show that a translation is missing
}
}
$f3->PREFIX = 'DICT.';
$f3->DICT = [];
Template::instance()->filter('dict', function($str) use($f3) {
return isset($f3->DICT[ $str ]) ? $f3->DICT[ $str ] : $str;
});
<strong>{{ 'Welcome home!' | dict }}</strong>
<?php
$f3 = require('base.php');
$f3->DEBUG = 5;
$f3->UI = './';
$f3->DICT = [];
$f3->PREFIX = 'DICT.';
$f3->LANGUAGE = 'fr-FR,fr';
$f3->LOCALES = 'dict/';
$tpl = Template::instance();
$tpl->filter('dict', function($str) use($f3) {
return $f3->get('DICT.'.$str) ?: $str;
});
echo $tpl->render('foo.html');
<!DOCTYPE html>
<meta charset="{{ @ENCODING }}"/>
<ul>
<li>Key not translated: {{ 'Welcome home!' | dict }}</li>
<li>Key translated: {{ 'Be careful!' | dict }}</li>
</ul>
<?php
return [
'Be careful!' => 'Faites attention !',
];
Yes, the idea is to have the Fallback string directly in the template. I'm working on a large project and having to map all the text on the site to variables is really time consuming. Having the string in whatever default language you have directly in the template was a huge time saver for me.
Also can you please explain me what $f3->DICT = [] does.