Php – CakePHP helpers and i18n


I have some questions about cakePHP, I spent long time Googling for a solution and as I didn't find any I'm not sure I'm taking the right approach.

So, I have a menu/sitemap which is part static (xml file – i10n) and part dynamic (database i18n)

I've been asked to cache the menu so that:

  1. A component creates a cache xml file of the whole menu for each language if it doesn't exist

  2. An helper creates a cache html formatted from the xml file created from the component

  3. The layout uses the helper to add the menu in the page


  • How do I get the list of available languages from the helper and from the component?

  • Is this

    $this->L10n = new L10n(); $this->L10n->lang

    the correct way to get the actual language?

  • If I import the helpers/component in the app_controller instead of typing them in each controller

    class AppController extends Controller {
        var $components = array('menu', 'otherComponent');
        var $helpers = array('menuCache');
        function beforeFilter(){

I get a call to undefined object $html for the echo $html->charset(); in the layout

can't understand why…

Best Solution

You can use Configure::read('Config.language'). A part of the CakePHP cookbook states:

The current locale is the current value of Configure::read('Config.language'). The value of Config.language is assigned in the L10n Class - unless it is already set.

I18n, the class responsible for translation using __(), uses Config.language so, unless you override it in bootstrap.php, that variable contains the selected language. Actually, even if you override it, it will still contain the used language (there might be inconsistencies because I10n is not really aware of the change but I never ran into any).

To get a list of languages, you can use L10n::catalog(). I'm not sure it's what you're after, however, since it lists all languages CakePHP knows about, not only the languages that actually have a translation in app/locale.

Related Question