How should I pass the translated text to my object in my multilingual application?
        Posted  
        
            by boatingcow
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by boatingcow
        
        
        
        Published on 2010-03-30T14:14:56Z
        Indexed on 
            2010/03/30
            14:33 UTC
        
        
        Read the original article
        Hit count: 313
        
Up until now, I have maintained a 'dictionary' table in my database, for example:
+-----------+---------------------------------------+-----------------------------------------------+--------+
| phrase    | en                                    | fr                                            | etc... |
+-----------+---------------------------------------+-----------------------------------------------+--------+
| generated | Generated in %1$01.2f seconds at %2$s | Créée en %1$01.2f secondes à %2$s aujourd'hui | ...    |
| submit    | Submit...                             | Envoyer...                                    | ...    |
+-----------+---------------------------------------+-----------------------------------------------+--------+
I'll then select all rows from the database for the column that matches the locale we're interested in (or read the cache from a file to speed db lookup) and dump the dictionary into an array called $lng.
Then I'll have HTML helper objects like this in my view:
$html->input(array('type' => 'submit', 'value' => $lng['submit'], etc...));
...
$html->div(array('value' => sprintf($lng['generated'], $generated, date('H:i')), etc...));
The translations can appear in PDF, XLS and AJAX responses too.
The problem with my approach so far is that I now have loads of global $lng; in every class where there is a function that spits out UI code..
How do other people get the translation into the object? Is it one scenario where globals aren't actually that bad? Would it be madness to create a class with accessors when the dictionary terms are all static?
© Stack Overflow or respective owner