tags:

views:

59

answers:

1

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 unsure if there are any cases where the format changes for particular languages.

+4  A: 

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: http://tools.ietf.org/html/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();
}
Miklos