bcosca / fatfree

A powerful yet easy-to-use PHP micro-framework designed to help you build dynamic and robust Web applications - fast!
2.66k stars 446 forks source link

Multilingual Support | Internationalization (I18n) | Localization (L10n) Features/Topics/Ideas #228

Closed AndrewEQ closed 11 years ago

AndrewEQ commented 11 years ago

Hi all,

So I've seen key-pair values across the various frameworks and thought it would be ok but recently had a look at gettext...

Features | Topics | Ideas

What do you think about the following features/topics/ideas (please feel free to add on):

Plurals

Once you have a translation, the frequency of change may be seldom, so how can we handle caching of the language files/strings?

Code listings

I'm not suggesting using the following code or solution(s) as their may be a more minimal/elegant solution(s) but just listing the resources to look at for awareness and inspiration:

2 great PHP tutorials on localizing with gettext:

bcosca commented 11 years ago

$f3->set('LOCALES','dict/',86400); will cache your dictionaries for one day.

bcosca commented 11 years ago

I find the preparation just to use gettext() and the argument to this function too verbose.

AndrewEQ commented 11 years ago

Ah, shweet caching.

Yeah, prep is quite a bit and there is the issue of updating all other locales when you change text in your gettext() or _() call (which is your default/fallback locale).

darkflame470 commented 11 years ago

Allowing the programmer to set the fallback language would be a great feature

AndrewEQ commented 11 years ago

What do you think about something like this for plurals? You would have to build in all the rules somewhere though and check them according to the input to use the appropriate translation.

Before usage

    $f3->set('no_comments', 3);

Usage

<div>{{ @comment_line, $no_comments }}</div>
<!-- outputs "There are 3 comments"-->

Lang definition for en_us - english

<?php

return array(
    'comment_line'=>array(1, //rule number followed by translations, checkout the list of plural rules: https://developer.mozilla.org/en-US/docs/Localization_and_Plurals
        'There is %no_comments comment',
        'There are %no_comments comments',
    ),
);

Lang definition for zh_us - chinese

<?php

return array(
    'comment_line'=>array(0, //rule number followed by translations, checkout the list of plural rules: https://developer.mozilla.org/en-US/docs/Localization_and_Plurals
        '有 %no_comments 条评论', 
    ),
);

Lang definition for ru_us - russian - ok, I'm guessing the translations here but according to the rules Mozilla defined, Russian has 3 forms

<?php

return array(
    'comment_line'=>array(7, //rule number followed by translations, checkout the list of plural rules: https://developer.mozilla.org/en-US/docs/Localization_and_Plurals
        'есть %no_comments комментарий',
        'есть %no_comments комментарий',
        'есть %no_comments комментариев',
    ),
);
bcosca commented 11 years ago

I am not inclined to get into a debate about this. gettext() is 90s technology. It requires third-party software for building language translations. Positional arguments are a common problem. Translation data is also opaque to the application, which makes debugging in two layers more difficult. This doesn't seem to be in consonance with the framework's philosophy of being simple and lightweight all the way. On the other hand, F3 implements (partially) the MessageFormat class of the newer ICU project. If pluralization is the only issue, we'll add that to the lined-up feature requests.

bcosca commented 11 years ago

FALLBACK variable implemented. Now in dev branch.

bcosca commented 11 years ago

ICU-style pluralization implemented.

$f3->set('foo',
    '{0, plural,'.
        'zero {There\'s nothing on the table.},'.
        'one {A book is on the table.},'.
        'other {There are # books on the table.}'.
    '}'
);
echo $f3->get('foo',0); // {{ @foo | 0 | format }}
echo $f3->get('foo',1); // {{ @foo | 1 | format }}
echo $f3->get('foo',2); // {{ @foo | 2 | format }}
echo $f3->get('foo',9); // {{ @foo | 9 | format }}
AndrewEQ commented 11 years ago

Awesome stuff! Wasn't implying that we should add gettext support but plurals support is much appreciated! :)