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

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

see #8853 remove tabs, trailing spaces, windows line ends, strange characters

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