This class contains language tokens which are valid for the xml:lang attribute. As specified by W3C, the values of the attribute are language identifiers as defined by IETF BCP 47, Tags for the Identification of Languages. @author BaseX Team, BSD License @author Dimitar Popov @author Jens Erat @se
| 22 | * >http://www.iana.org/assignments/language-subtag-registry</a> |
| 23 | */ |
| 24 | public final class Language implements Comparable<Language> { |
| 25 | /** Available languages, indexed by language code. */ |
| 26 | static final HashMap<String, Language> ALL = new HashMap<>(); |
| 27 | /** Available languages, indexed by their display. */ |
| 28 | private static final HashMap<String, Language> DISP = new HashMap<>(); |
| 29 | /** Locale. */ |
| 30 | private final Locale locale; |
| 31 | |
| 32 | static { |
| 33 | for(final Locale l : Locale.getAvailableLocales()) { |
| 34 | ALL.put(l.getLanguage(), new Language(l)); |
| 35 | DISP.put(l.getDisplayLanguage(Locale.ENGLISH), new Language(l)); |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * Private Constructor. |
| 41 | * @param locale locale |
| 42 | */ |
| 43 | private Language(final Locale locale) { |
| 44 | this.locale = locale; |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Returns an instance for the specified language code or {@code null}. |
| 49 | * @param lang name or code of language |
| 50 | * @return language code |
| 51 | */ |
| 52 | public static Language get(final String lang) { |
| 53 | final int i = lang.indexOf('-'); |
| 54 | final String l = i == -1 ? lang : lang.substring(0, i); |
| 55 | final Language ln = ALL.get(l.toLowerCase(Locale.ENGLISH)); |
| 56 | return ln == null ? DISP.get(lang) : ln; |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Returns an instance for the current language option, or English as default language. |
| 61 | * @param opts database options |
| 62 | * @return language code |
| 63 | */ |
| 64 | public static Language get(final MainOptions opts) { |
| 65 | final Language ln = get(opts.get(MainOptions.LANGUAGE)); |
| 66 | return ln == null ? get("en") : ln; |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Returns the user language as default language, or English if the language cannot be assigned. |
| 71 | * @return default language |
| 72 | */ |
| 73 | public static Language def() { |
| 74 | final Language ln = DISP.get(MainOptions.LANGUAGE.value()); |
| 75 | return ln == null ? get("en") : ln; |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Returns the language code (ISO 639). |
| 80 | * @return code |
| 81 | */ |