Working with Fat Free Framework template engine and translations using dictionary files.
In my template I have {{@_someNonTranslatedWord}} keys.
Currently FF just leaves the space emtpy when this key is missing. Is there a way to make FF display the raw key instead of leaving the space empty?
I'd rather see something like:
"My {{@_someNonTranslatedWord}} is missing"
instead of
"My is missing"
So I can easily identify missing keys and add them. Possibly even auto-translate them when they're missing.
Actually it is possible with a little trick. You can use the framework var PREFIX
to add a prefix to all language keys..
so when you set PREFIX = ll.
in your config file, all keys in your language files are put into the array ll
(ll = local language).
Now, in your template, you'll use {{ @ll.welcome_headline }}
and you have all your keys separated nicely and don't conflict with the rest of your variables. And since you know, that all language keys are now within in ll
array, you can exchange that array with an object, that implements ArrayAccess and adds some logic when accessing those keys.. here is a little helper I just wrote right now to test that behaviour, which works pretty well:
namespace Service;
class Dictionary extends \Magic {
protected
$lex = [],
$prev_key,
$f3;
function __construct(array $lexicon, $prev_key = NULL) {
$this->lex = $lexicon;
$this->prev_key = $prev_key;
$this->f3 = \Base::instance();
}
function exists($key) {
$val=$this->f3->ref($key,FALSE,$this->lex);
return isset($val);
}
function set($key,$val) {
$ref=&$this->f3->ref($key, TRUE, $this->lex);
$ref=$val;
return $ref;
}
function &get($key) {
$val=$this->f3->ref($key,FALSE, $this->lex);
$current_key = ($this->prev_key?$this->prev_key.'.':'').$key;
if (is_array($val)) {
$val = new self($val, $current_key);
} else {
if (!$val) {
// do something when language key is missing
var_dump('missing language key: '.$current_key);
} else {
// track which language key was used
var_dump('language key used: '.$current_key);
}
}
return $val;
}
function clear($key) {
// no clear
}
}
To use that puppy, simply create the object, feed the lexicon entries in, and exchange the existing stupid array:
$dict = new Service\Dictionary($fw->get('ll'));
$fw->set('ll', $dict);
Do that before rendering the template, or using language keys in general. Everytime you access an existing or missing key, you can do something special in the get
method.. maybe also translate them on the fly if you want to...
good luck ;)