开发者

Converting java language output to Joomla language output

in java if I run :

Locale.getDefault().toString()

I get zh_tw

I am sending this to a joomla site and setting the language like this:

$lang = &JFactory::getLanguage();
$lang->setLanguage( $_GET['lang'] );
$lang->load();

however the site requires the following format zh-TW

It appears that if it is not in that exact format the language will not change. Is there a function somewhere in java or php that will convert the format for me?

I realise that I could write the method myself like this:

    public static String convertLanguageToJoomlaFormat(String lang) {
    String[] parts = lang.split("_");
    if(parts.length ==2)
        return parts[0]+"-"+parts[1].toUpperCase();
    return lang;
}

but am uns开发者_如何学编程ure if there are any cases where the format changes for particular languages.


According to the Java API docs Locale.toString() is a:

Getter for the programmatic name of the entire locale, with the language, country and variant separated by underbars. Language is always lower case, and country is always upper case. If the language is missing, the string will begin with an underbar. If both the language and country fields are missing, this function will return the empty string, even if the variant field is filled in (you can't have a locale with just a variant-- the variant must accompany a valid language or country code). Examples: "en", "de_DE", "_GB", "en_US_WIN", "de__POSIX", "fr__MAC"

Joomla 1.5 claims to use the IETF RFC3066 for locale names: https://www.rfc-editor.org/rfc/rfc3066, but in reality it always uses the language and country code parts of the identifier like [en-GB].

The problem arise when the Java Locale does not have a country part. In this case you may use a 'hack' using the uppercase version of language code as the country code:

String[] tokens = locale.toString().split("_");
if (tokens.length >= 2 && tokens[1].length() == 2) {
    joomlaName = tokens[0]+"-"+tokens[1];
} else {
    joomlaName = tokens[0]+"-"+tokens[0].toUpperCase();
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜