TIP: Use Markdown or, <pre> for multi line code blocks / <code> for inline code.
These forums are read-only and for archival purposes only!
Please join our new forums at discourse.kohanaframework.org
I18n language assumed as default
  • Hello!

    I 'm new here and new to Kohana (developing my first project with it right now).

    So, I was developing a multilanguage site, and, naturally, was writing the phrases in site's original language (not English), so, for example:

    <?
     echo __("Cita valoda"); //Different language
    ?>
    

    In bootstrap I have set the default lang:

    /**
     * Set the default language
     */
    I18n::lang('lv-lv');
    

    Then, when I switch the language to 'en-us', it somehow didn't work - nothing was translated.

    Then I dug in the code and found out, that the default language is kind of hardcoded:

        function __($string, array $values = NULL, $lang = 'en-us')
        {
            if ($lang !== I18n::$lang)
            {
                // The message and target languages are different
                // Get the translation for this message
                $string = I18n::get($string);
            }
    
            return empty($values) ? $string : strtr($string, $values);
        }
    

    and by that, it is always assumed that the language used in __() function is 'en-us'.

    I got it to work by using different lang name, like 'en'.

    Don't know how to call this problem, but maybe it's something to look forward in future to fix, so that the language I specify by default in bootstrap is the language I will use in __().

    So, fellow coders, what do you think about this?

  • The problem is that everything in system is written in english, and it uses __() so if your original code is in a language other than english, you need to send the second parameter to __(). If you don't want to use the second parameter for everything you write, you need to write the original version in english and Kohana will translate it to the language you set in the bootstrap.

  • In bootstrap.php, set the default language (the one you will use in __()) with I18n::lang('lv-lv'). Then you have to set your i18n files for other languages.

    Have a look at http://kerkness.ca/wiki/doku.php?id=routing:multi-language_with_a_route

    For my part I code with french sentences by default and it works fine.

  • The problem is that everything in system is written in english

    It's actually a bigger problem then that. The fact is kohana thinks it's being smart by translating silly things like error pages. Who writes code in other languages and wants code (or code related messages) translated (I'm not a native english speaker). Let's assume you are outright insane and output stack traces as your "user friendly error messages", how is having english and randomly translated words from english a good idea? Where do you get the idea that having a word in english, a word in language X and potentially another word (such as variables in code) in some other language is good practice.

    In my exception handlers it's always a pain since I have to reset the language to en-us so I don't have to worry about kohana spaming me trying to translate things I don't care to be translated.

    And besides, as mentioned already, I could be making a site that's mainly not english (might not have any english anywhere even as a option), maybe the site just has Korean/Japanese/Chinese as language options; kohana should not force me to write the base version in english. There should be no default set outside of some default in configuration file like bootstrap.php; since there are at least 7 versions of english out there too.

  • When I wrote the original i18n __() methods for 2.4, it didn't force you to use English. However if you changed the default language, system messages wouldn't be translated, since they are written in english.

  • @Akki, you are right, we will be switching exceptions to use strtr instead of __() because we don't want those translated.

    @j987 you are also right, we actually had this in http://dev.kohanaframework.org/issues/3310 but it was incorrectly applied somewhere down the road.

  • All (translated) messages generated by core should indicate that they are 'en-us'. I've opened an issue for this.

  • Hi and thanks for your responses! I'm glad, that I'm not the only one with this problem :) So, waiting for one of these issues to be fixed and released!

    Good luck to the dev team!

  • Uhm, I also encountered the problem where things weren't translated as I expected.

    Then I found this thread and the the fix that j987 posted:

    function __($string, array $values = NULL, $source = NULL)
        {
            if ( ! $source)
            {
                // Use default source language
                $source = I18n::$source;
            }
    
            if ($source !== I18n::$lang)
            {
                // The message and target languages are different
                // Get the translation for this message
                $string = I18n::get($string);
            }
    
            return empty($values) ? $string : strtr($string, $values);
        }
    

    But in my opinion, this function does this: If you want to translate something, and argument $source is set, but isn't the same as the global language:

    if ($source !== I18n::$lang)
    

    then we get the translation for this message by:

    l18n::get($string)
    

    But the function get() does this:

    public static function get($string, $lang = NULL)
        {
            if ( ! $lang)
            {
                // Use the global target language
                $lang = I18n::$lang;
            }
    
            // Load the translation table for this language
            $table = I18n::load($lang);
    
            // Return the translated string if it exists
            return isset($table[$string]) ? $table[$string] : $string;
        }
    

    So if you don't give get() a second parameter, it just uses the global language setting again.

    Must the function __() not pass it's third argument as second parameter for get() ? Like so:

    if ($source !== I18n::$lang)
            {
                // The message and target languages are different
                // Get the translation for this message
                $string = I18n::get($string, $source);
            }
    

    If I'm wrong or just don't understand how Kohana works then please tell me.

  • __() function's third argument is a source language (the one you are translating from). get() function's second parameter is is a target language (the one you are translating to).

    You can/should define source language in __() when you only have few strings passed to __() in different source language than the main source language that you normally pass strings to __().

  • Oké then I probaly didn't understand the translate mechanism very well, thank you. :)

  • I use I18n:lang() em Controller::before() and index mark __('application.controller.action.identificator'); You can change all indexes of language files for your language. or ... For automatic translator use the module google translate in github.

  • @cbandy - I believe that is the correct solution, by specifying en-us inside each of the core messages..

    Not translating them doesn't seem ideal..

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion