yiisoft / yii

Yii PHP Framework 1.1.x
http://www.yiiframework.com
BSD 3-Clause "New" or "Revised" License
4.85k stars 2.28k forks source link

load yii core messages for more locales #698

Closed xwz closed 8 years ago

xwz commented 12 years ago

Currently the yii core messages are loaded when the application language match exactly the translated message file names.

For example, when language="no" it will load messages/no/yii.php, however, when language="nb_NO" it is unable to load messages/no/yii.php.

Proposal:

Register a subclass of CPhpMessageSource for coreMessages application component that provides a mapping from more complete locale identifiers (such as "nb_no" => "no") to respective translated core messages.

class YiiCoreMessages extends CPhpMessageSource
{
    private static $MAPPING=array(
        'nb_NO' => 'no',
        // ... other mappings
    );

    protected function getMessageFile($category,$language)
    {
        if(($category==='yii'||$category==='zii')&&isset(self::$MAPPING[$language]))
            $language=self::$MAPPING[$language];
        return parent::getMessageFile($category,$language);
    }
}
mdomba commented 12 years ago

is the mapping needed?

as all the "parent" languages are those before the _, can this be extracted from the $language?

xwz commented 12 years ago

Yes, because some files are based on language, others on the territory part of the locale identifier. Some files use both.

On 10/05/2012, at 7:42 PM, Maurizio Dombareply@reply.github.com wrote:

is the mapping needed?

as all the "parent" languages are those before the _, can this be extracted from the $language?


Reply to this email directly or view it on GitHub: https://github.com/yiisoft/yii/issues/698#issuecomment-5621464

samdark commented 12 years ago

It possible can solve incosistencies we have with jQueryUI locale names.

xwz commented 12 years ago

It possible can solve incosistencies we have with jQueryUI locale names.

Can you elaborate?

qiangxue commented 12 years ago

This problem needs a more generic solution. Basically we need some sort of fallback mechanism while at the same time not sacrificing the performance too much.

samdark commented 12 years ago

@xwz in order to use localized jQueryUI widgets you need to specify locale. The issue is that locale names aren't the same as in Yii itself.

luislobo commented 12 years ago

In my case, I solve this using this function in my Controller class:

static public function fallbackLanguage( $language )
{
    $newLang = '';
    $changeToLanguage = strtolower( (string) $language );

    // find out language
    if( !in_array( $changeToLanguage, Controller::$languages ) )
    {
        foreach( Controller::$languages as $lang )
        {
            if( substr( $changeToLanguage, 0, 2 ) === $lang )
            {
                $newLang = $lang;
            }
        }
        if( $newLang === '' )
        {
            // fallback to english
            $changeToLanguage = Controller::$defaultLanguage;
        }
        else
        {
            $changeToLanguage = $newLang;
        }
    }
    return $changeToLanguage;
}

This function is called in the Controller constructor. In my case, our logged user has a property for their language, so it makes no sense putting my code of the constructor for you here, but may be it's a good starting point

luislobo commented 12 years ago

btw, I have a list of "valid" languages in Controller::$languages I support...

static $defaultLanguage = 'en';
static $languages = array(
    'af',
    //'ar',
    'da',
    'de',
    'en',
    'es',
    'et',
    'fr',
    'ga',
    //'hi',
    'it',
    //'jp',
    'pt',
    //'ru',
    'sv',
    'tr',
    //'zn',
);
static $languageNames = array(
    'af' => 'Afrikaans',
    //'ar' => 'العربية',
    'da' => 'Dansk',
    'de' => 'Deutsch',
    'en' => 'English',
    'es' => 'Español',
    'et' => 'Eesti',
    'fr' => 'Français',
    'ga' => 'Gaeilge',
    //'hi' => 'हिन्दी',
    'it' => 'Italiano',
    //'jp' => '日本',
    'pt' => 'Português',
    //'ru' => 'Россию',
    'sv' => 'Svenska',
    'tr' => 'Türkçe',
    //'zh' => '中文',
);
xwz commented 12 years ago

@luislobo I think that's separate issue. This is more about allowing users to use a specific locale and having yii core message still work relatively correct.

luislobo commented 12 years ago

Yes, I just contributed with a fallback algorithm, if it helps

schmunk42 commented 11 years ago

For message files I could solve it like this: http://www.yiiframework.com/wiki/504/how-to-provide-a-fallback-or-mapping-for-translation-messages/

Note: Has no effect on locales and views!

@qiangxue Would be nice if Yii2 could address this somehow. Or would aliases even be capable of that?

realtebo commented 11 years ago

I think it's not a good idea to do substr, because a lot of js library need "it_IT" format, probably the fallback should be the inverse: when 'it' -> 'it-IT" !

luislobo commented 11 years ago

Just on top of my head... if you just want to translate several languages pointing to one only single translation source, you could create symbolic links...