source: josm/trunk/src/org/openstreetmap/josm/tools/LanguageInfo.java@ 3486

Last change on this file since 3486 was 3418, checked in by stoecker, 14 years ago

fixed locale handling a bit

  • Property svn:eol-style set to native
File size: 2.8 KB
Line 
1// License: GPL. Copyright 2007 by Immanuel Scholz and others
2package org.openstreetmap.josm.tools;
3
4import java.util.Locale;
5import static org.openstreetmap.josm.tools.I18n.tr;
6
7public class LanguageInfo {
8
9 /**
10 * Replies the wiki language prefix for the given locale. The wiki language
11 * prefix has the form 'Xy:' where 'Xy' is a ISO 639 language code in title
12 * case.
13 *
14 * @param locale the locale
15 * @return the wiki language prefix
16 */
17 static public String getWikiLanguagePrefix(Locale locale) {
18 String code = getJOSMLocaleCode(locale);
19 if (code.length() == 2) {
20 if (code.equals("en")) return "";
21 } else if (code.equals("zh_TW") || code.equals("zh_CN")) {
22 /* do nothing */
23 } else if (code.matches("[^_]+_[^_]+")) {
24 code = code.substring(0,2);
25 } else {
26 System.err.println(tr("Warning: failed to derive wiki language prefix from JOSM locale code ''{0}''. Using default code ''en''.", code));
27 return "";
28 }
29 return code.substring(0,1).toUpperCase() + code.substring(1) + ":";
30 }
31
32 /**
33 * Replies the wiki language prefix for the current locale.
34 *
35 * @return the wiki language prefix
36 * @see Locale#getDefault()
37 * @see #getWikiLanguagePrefix(Locale)
38 */
39 static public String getWikiLanguagePrefix() {
40 return getWikiLanguagePrefix(Locale.getDefault());
41 }
42
43 /**
44 * Replies the JOSM locale code for the default locale.
45 *
46 * @return the JOSM locale code for the default locale
47 * @see #getJOSMLocaleCode(Locale)
48 */
49 static public String getJOSMLocaleCode() {
50 return getJOSMLocaleCode(Locale.getDefault());
51 }
52
53 /**
54 * Replies the locale code used by JOSM for a given locale.
55 *
56 * In most cases JOSM uses the 2-character ISO 639 language code ({@see Locale#getLanguage()}
57 * to identify the locale of a localized resource, but in some cases it may use the
58 * programmatic name for locales, as replied by {@see Locale#toString()}.
59 *
60 * @param locale the locale. Replies "en" if null.
61 * @return the JOSM code for the given locale
62 */
63 static public String getJOSMLocaleCode(Locale locale) {
64 if (locale == null) return "en";
65 String full = locale.toString();
66 if (full.equals("iw_IL"))
67 return "he";
68 /* list of non-single codes supported by josm */
69 else if (full.equals("en_GB") || full.equals("pt_BR") || full.equals("en_AU") || full.equals("zh_TW") || full.equals("zh_CN"))
70 return full;
71
72 return locale.getLanguage();
73 }
74
75 static public String getLanguageCodeXML()
76 {
77 return getJOSMLocaleCode()+".";
78 }
79 static public String getLanguageCodeManifest()
80 {
81 return getJOSMLocaleCode()+"_";
82 }
83}
Note: See TracBrowser for help on using the repository browser.