This discussion is archived
2 Replies Latest reply: Nov 2, 2010 2:50 PM by 802737 RSS

In search of the links Country to Languages and Language to Countries.

802737 Newbie
Currently Being Moderated
Hello,

At the beginning, I was happy. With some users coming from France (fr_FR) or from the US (en_US), it was easy to do offer them some preselections in their forms, such as: "France", "United States" for any addresses they wished to type, or even EUR or USD if they had to select some currencies.

But then, a Vietnamese came.
His browser mentions only "*vi*" and this is normal. This is the standard.
But, I would have loved having vi_VN instead! Because, if from vi I can extract Vietnamese easily (the tongue), I can't extract the country (Viet Nam) or the currency (VND).

Java offers two lists:
The one with all the languages,
The other with all the countries.

When a user comes and you only know his language, is there a way to find his associated country?
Making the (dangerous?) assumption that if the locale mentions only the language, it means that the country speaking that language would be unique?

Do I have a way to generate from vi: vi_VN?
Next, with Africans (af) langage, as another example, af_<something>?


Things, are really tricky for me (as you can see).
Am I misleading?


Regards,

Grunt.
  • 1. Re: In search of the links Country to Languages and Language to Countries.
    800025 Explorer
    Currently Being Moderated
    Hi Grunt,

    The following example is a very rough guess that may need some refinements, but is may help as a start:
    import java.util.*;
    
    public class SearchCountryLocale {
    
        public static void main(String[] args) {
         System.out.println(findCountryLocale("vi"));
        }
    
        public static Locale findCountryLocale(String language) {
         Locale[] locales = Locale.getAvailableLocales();
         Set<Locale> candidates = new HashSet<Locale>();
         for (Locale locale : locales) {
             if (language.equals(locale.getLanguage())
                  && locale.getCountry().length() > 0
                  && locale.getVariant().length() == 0) {
              candidates.add(locale);
             }
         }
         if (candidates.size() == 1) {
             for (Locale locale : candidates) {
              return locale;
             }
         }
         throw new RuntimeException("No country locale found for " + language);
        }
    
    }
    Piet
  • 2. Re: In search of the links Country to Languages and Language to Countries.
    802737 Newbie
    Currently Being Moderated
    No, I am sorry. This won't help.

    Your algorithm attempt to parse availables locales,
    but it allows itself to end in sentences like:
    No country speak a language.
    or no language is spoken in a country.
    who are for the end user, a non-sense.

    => in Vietnam (VN) people speak {vietnamese (vi)}
    => and Vietnamese (vi) is spoken in {Vietnam(VN)}

    The Locale class alone doesn't offer the solution.

    Grunt

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points