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 using dot notation with sub-arays
  • The purpose is pretty simple, allow the programmer to fill the i18n lanaguage files the same way with both dot/sub-array notations.

    I believe the array notation is prefered from some developpers, I think it's easier to see the groups using this way than a lot of big keys with dot separators.

    Here is my code:

    // View: echo __('categories.dog_available_reproduction.no'); echo __('categories.dog_available_reproduction.yes');

    // i18n/en.php return array( 'categories' => array( 'dog_available_reproduction' => array( 'no' => 'Non', ) ), 'categories.dog_available_reproduction.yes' => 'Yes', );

    // classes/I18n.php <?php defined('SYSPATH') OR die('No direct script access.');

    /** * Custom I18n class. * Allows to use the same notation than the config file with dot separator to find in sub-arrays. * * @package I18n * @author Vadorequest */ class I18n extends Kohana_I18n {

     * Returns translation of a string. If no translation exists, the original
     * string will be returned. No parameters are replaced.
     * @example $hello = I18n::get('Hello friends, my name is :name');
     * @param string $string
     * @param string $lang
     * @return string
    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
            // Simple way, by full key.
            return $table[$string];
        // Complex way where the points are looking into sub arrays.
        if($value = Arr::path($table, $string)){
            return $value;
        // If no key found, return the key.
        return $string;


    What do you think? The footprint is minimalist and should not break the compatibility.

  • And please explain how make the code beautiful.., markdown doesn't works...

  • some html tags work, use pre tag for code, more info.

    About fix: when a large amount of data it will require a lot of server resources.

    I use a reduction in the names of keys, plus - get a short entry, minus - the excess server loading when using the default language.

    // My short version:
    echo __('mail_success');
    echo __('user_created');
    // Basic version:
    echo __('E-mail sent successfully!');
    echo __('New user successfully created!');

    Also you can use combined i18n file:

    // 18n/ru.php:
    // ...
    $main = include_once '18n/ru/main.php';
    $forum = include_once '18n/ru/forum.php';
    // ...
    return $main + $forum;

    Much more interesting it would make the separation language file into several logical parts:

    __('user', 'New user successfully created!'); // load '../18n/ru/user.php'
    __('admin.mail', 'E-mail sent successfully!'); // load '../18n/ru/admin/mail.php'
    // or shorter:

    plus create generation of language files for each section based on the i18n tags used in it: site.com/catalog/ load ../18n/ru/catalog.php, site.com/forum/ load ../18n/ru/forum.php. The use of such "cached" files will be faster, than loading a "common" language file: files much smaller and thus load faster and take up less memory, faster search values.

  • Don't mix i18n with Kohana::message. I18n keys meant to be human readable sentences/strings.

Howdy, Stranger!

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

In this Discussion