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

Last change on this file since 5915 was 5915, checked in by stoecker, 11 years ago

use 3 step wiki loading fallback, cleanup handling of language fallbacks

  • Property svn:eol-style set to native
File size: 4.0 KB
Line 
1// License: GPL. Copyright 2007 by Immanuel Scholz and others
2package org.openstreetmap.josm.tools;
3
4import static org.openstreetmap.josm.tools.I18n.marktr;
5
6import org.openstreetmap.josm.Main;
7
8import java.util.Locale;
9
10public class LanguageInfo {
11 /** Type of the locale to use */
12 public enum LocaleType {
13 /** The current default language */
14 DEFAULT,
15 /** The current default language, but not english */
16 DEFAULTNOTENGLISH,
17 /** The base language (i.e. pt for pt_BR) */
18 BASELANGUAGE,
19 /** The standard english texts */
20 ENGLISH
21 };
22
23 /**
24 * Replies the wiki language prefix for the given locale. The wiki language
25 * prefix has the form 'Xy:' where 'Xy' is a ISO 639 language code in title
26 * case (or Xy_AB: for sub languages).
27 *
28 * @param type the type
29 * @return the wiki language prefix or {@code null} for {@link LocaleType#BASELANGUAGE}, when
30 * base language is identical to default or english
31 * @since 8636
32 */
33 static public String getWikiLanguagePrefix(LocaleType type) {
34 if(type == LocaleType.ENGLISH)
35 return "";
36
37 String code = getJOSMLocaleCode();
38 if(type == LocaleType.BASELANGUAGE) {
39 if(code.matches("[^_]+_[^_]+")) {
40 code = code.substring(0,2);
41 if(code == "en")
42 return null;
43 } else {
44 return null;
45 }
46 } else if(type == LocaleType.DEFAULTNOTENGLISH && code == "en")
47 return null;
48 return code.substring(0,1).toUpperCase() + code.substring(1) + ":";
49 }
50
51 /**
52 * Replies the wiki language prefix for the current locale.
53 *
54 * @return the wiki language prefix
55 * @see Locale#getDefault()
56 * @see #getWikiLanguagePrefix(LocaleType)
57 */
58 static public String getWikiLanguagePrefix() {
59 return getWikiLanguagePrefix(LocaleType.DEFAULT);
60 }
61
62 /**
63 * Replies the JOSM locale code for the default locale.
64 *
65 * @return the JOSM locale code for the default locale
66 * @see #getJOSMLocaleCode(Locale)
67 */
68 static public String getJOSMLocaleCode() {
69 return getJOSMLocaleCode(Locale.getDefault());
70 }
71
72 /**
73 * Replies the locale code used by JOSM for a given locale.
74 *
75 * In most cases JOSM uses the 2-character ISO 639 language code ({@link Locale#getLanguage()}
76 * to identify the locale of a localized resource, but in some cases it may use the
77 * programmatic name for locales, as replied by {@link Locale#toString()}.
78 *
79 * @param locale the locale. Replies "en" if null.
80 * @return the JOSM code for the given locale
81 */
82 static public String getJOSMLocaleCode(Locale locale) {
83 if (locale == null) return "en";
84 String full = locale.toString();
85 if (full.equals("iw_IL"))
86 return "he";
87 else if (full.equals("in"))
88 return "id";
89 else if (I18n.hasCode(full)) /* catch all non-single codes */
90 return full;
91
92 /* return single code */
93 return locale.getLanguage();
94 }
95
96 /**
97 * Replies the locale code used by Java for a given locale.
98 *
99 * In most cases JOSM and Java uses the same codes, but for some exceptions this is needed.
100 *
101 * @param localeName the locale code.
102 * @return the resulting locale
103 */
104 static public Locale getLocale(String localeName) {
105 if (localeName.equals("he")) {
106 localeName = "iw_IL";
107 }
108 else if (localeName.equals("id")) {
109 localeName = "in";
110 }
111 Locale l;
112 int i = localeName.indexOf('_');
113 if (i > 0) {
114 l = new Locale(localeName.substring(0, i), localeName.substring(i + 1));
115 } else {
116 l = new Locale(localeName);
117 }
118 return l;
119 }
120
121 static public String getLanguageCodeXML()
122 {
123 return getJOSMLocaleCode()+".";
124 }
125 static public String getLanguageCodeManifest()
126 {
127 return getJOSMLocaleCode()+"_";
128 }
129}
Note: See TracBrowser for help on using the repository browser.