1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others
|
---|
2 | package org.openstreetmap.josm.tools;
|
---|
3 |
|
---|
4 | import java.util.Locale;
|
---|
5 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
6 |
|
---|
7 | public 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 | if (code.equals("en")) return "";
|
---|
26 | } else {
|
---|
27 | System.err.println(tr("Warning: failed to derive wiki language prefix from JOSM locale code ''{0}''. Using default code ''en''.", code));
|
---|
28 | return "";
|
---|
29 | }
|
---|
30 | return code.substring(0,1).toUpperCase() + code.substring(1) + ":";
|
---|
31 | }
|
---|
32 |
|
---|
33 | /**
|
---|
34 | * Replies the wiki language prefix for the current locale.
|
---|
35 | *
|
---|
36 | * @return the wiki language prefix
|
---|
37 | * @see Locale#getDefault()
|
---|
38 | * @see #getWikiLanguagePrefix(Locale)
|
---|
39 | */
|
---|
40 | static public String getWikiLanguagePrefix() {
|
---|
41 | return getWikiLanguagePrefix(Locale.getDefault());
|
---|
42 | }
|
---|
43 |
|
---|
44 | /**
|
---|
45 | * Replies the JOSM locale code for the default locale.
|
---|
46 | *
|
---|
47 | * @return the JOSM locale code for the default locale
|
---|
48 | * @see #getJOSMLocaleCode(Locale)
|
---|
49 | */
|
---|
50 | static public String getJOSMLocaleCode() {
|
---|
51 | return getJOSMLocaleCode(Locale.getDefault());
|
---|
52 | }
|
---|
53 |
|
---|
54 | /**
|
---|
55 | * Replies the locale code used by JOSM for a given locale.
|
---|
56 | *
|
---|
57 | * In most cases JOSM uses the 2-character ISO 639 language code ({@see Locale#getLanguage()}
|
---|
58 | * to identify the locale of a localized resource, but in some cases it may use the
|
---|
59 | * programmatic name for locales, as replied by {@see Locale#toString()}.
|
---|
60 | *
|
---|
61 | * @param locale the locale. Replies "en" if null.
|
---|
62 | * @return the JOSM code for the given locale
|
---|
63 | */
|
---|
64 | static public String getJOSMLocaleCode(Locale locale) {
|
---|
65 | if (locale == null) return "en";
|
---|
66 | String full = locale.toString();
|
---|
67 | if (full.equals("iw_IL"))
|
---|
68 | return "he";
|
---|
69 | /* list of non-single codes supported by josm */
|
---|
70 | else if (full.equals("en_GB") || full.equals("pt_BR") || full.equals("en_AU") || full.equals("zh_TW") || full.equals("zh_CN"))
|
---|
71 | return full;
|
---|
72 |
|
---|
73 | return locale.getLanguage();
|
---|
74 | }
|
---|
75 |
|
---|
76 | static public String getLanguageCodeXML()
|
---|
77 | {
|
---|
78 | return getJOSMLocaleCode()+".";
|
---|
79 | }
|
---|
80 | static public String getLanguageCodeManifest()
|
---|
81 | {
|
---|
82 | return getJOSMLocaleCode()+"_";
|
---|
83 | }
|
---|
84 | }
|
---|