2014-04-19 02:30:29 -04:00
|
|
|
package net.filebot;
|
2008-06-21 15:24:18 -04:00
|
|
|
|
2011-08-09 23:51:21 -04:00
|
|
|
import static java.util.Arrays.*;
|
|
|
|
import static java.util.Collections.*;
|
|
|
|
|
2013-12-31 02:59:58 -05:00
|
|
|
import java.io.Serializable;
|
2009-05-31 12:38:13 -04:00
|
|
|
import java.util.Comparator;
|
2011-08-09 23:51:21 -04:00
|
|
|
import java.util.LinkedHashSet;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Locale;
|
2009-05-31 12:38:13 -04:00
|
|
|
import java.util.ResourceBundle;
|
|
|
|
import java.util.Set;
|
2008-06-21 15:24:18 -04:00
|
|
|
|
2013-12-31 02:59:58 -05:00
|
|
|
public class Language implements Serializable {
|
2013-09-21 03:29:57 -04:00
|
|
|
|
2014-08-27 15:53:10 -04:00
|
|
|
// ISO 639-1 code
|
2014-09-04 12:41:20 -04:00
|
|
|
private final String iso_639_1;
|
2014-08-27 15:53:10 -04:00
|
|
|
|
2014-09-04 12:41:20 -04:00
|
|
|
// ISO 639-3 code (mostly identical to ISO 639-2/T)
|
|
|
|
private final String iso_639_3;
|
2014-08-27 15:53:10 -04:00
|
|
|
|
|
|
|
// ISO 639-2/B code
|
2014-09-04 12:41:20 -04:00
|
|
|
private final String iso_639_2B;
|
2014-08-27 15:53:10 -04:00
|
|
|
|
|
|
|
// Language name
|
2009-05-31 12:38:13 -04:00
|
|
|
private final String name;
|
2013-09-21 03:29:57 -04:00
|
|
|
|
2014-09-04 12:41:20 -04:00
|
|
|
public Language(String iso_639_1, String iso_639_3, String iso_639_2B, String name) {
|
|
|
|
this.iso_639_1 = iso_639_1;
|
|
|
|
this.iso_639_3 = iso_639_3;
|
|
|
|
this.iso_639_2B = iso_639_2B;
|
2009-05-31 12:38:13 -04:00
|
|
|
this.name = name;
|
2009-05-31 11:19:56 -04:00
|
|
|
}
|
2013-09-21 03:29:57 -04:00
|
|
|
|
2009-05-31 12:38:13 -04:00
|
|
|
public String getCode() {
|
2014-09-04 12:41:20 -04:00
|
|
|
return iso_639_1;
|
2013-12-31 02:28:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
public String getISO2() {
|
2014-09-04 12:41:20 -04:00
|
|
|
return iso_639_1; // 2-letter code
|
2013-12-31 02:28:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
public String getISO3() {
|
2014-09-04 12:41:20 -04:00
|
|
|
return iso_639_3; // 3-letter code
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getISO3B() {
|
|
|
|
return iso_639_2B; // alternative 3-letter code
|
2008-06-21 15:24:18 -04:00
|
|
|
}
|
2013-09-21 03:29:57 -04:00
|
|
|
|
2008-06-21 15:24:18 -04:00
|
|
|
public String getName() {
|
|
|
|
return name;
|
|
|
|
}
|
2013-09-21 03:29:57 -04:00
|
|
|
|
2009-05-31 12:38:13 -04:00
|
|
|
@Override
|
|
|
|
public String toString() {
|
2014-09-04 12:41:20 -04:00
|
|
|
return iso_639_3;
|
2008-06-21 15:24:18 -04:00
|
|
|
}
|
2013-09-21 03:29:57 -04:00
|
|
|
|
2013-12-31 02:59:58 -05:00
|
|
|
public Locale getLocale() {
|
2011-09-09 10:50:01 -04:00
|
|
|
return new Locale(getCode());
|
|
|
|
}
|
2013-09-21 03:29:57 -04:00
|
|
|
|
2014-08-27 15:53:10 -04:00
|
|
|
public boolean matches(String code) {
|
2015-01-15 13:06:55 -05:00
|
|
|
return iso_639_1.equalsIgnoreCase(code) || iso_639_3.equalsIgnoreCase(code) || iso_639_2B.equalsIgnoreCase(code) || name.equalsIgnoreCase(code) || code.toLowerCase().contains(name.toLowerCase());
|
2014-08-27 15:53:10 -04:00
|
|
|
}
|
|
|
|
|
2009-05-31 11:19:56 -04:00
|
|
|
@Override
|
|
|
|
public Language clone() {
|
2014-09-04 12:41:20 -04:00
|
|
|
return new Language(iso_639_1, iso_639_3, iso_639_2B, name);
|
2008-06-21 15:24:18 -04:00
|
|
|
}
|
2013-09-21 03:29:57 -04:00
|
|
|
|
2009-05-31 12:38:13 -04:00
|
|
|
public static final Comparator<Language> ALPHABETIC_ORDER = new Comparator<Language>() {
|
2013-09-21 03:29:57 -04:00
|
|
|
|
2009-05-31 12:38:13 -04:00
|
|
|
@Override
|
|
|
|
public int compare(Language o1, Language o2) {
|
|
|
|
return o1.name.compareToIgnoreCase(o2.name);
|
|
|
|
}
|
|
|
|
};
|
2013-09-21 03:29:57 -04:00
|
|
|
|
2009-05-31 12:38:13 -04:00
|
|
|
public static Language getLanguage(String code) {
|
|
|
|
ResourceBundle bundle = ResourceBundle.getBundle(Language.class.getName());
|
2013-09-21 03:29:57 -04:00
|
|
|
|
2009-05-31 12:38:13 -04:00
|
|
|
try {
|
2014-08-27 15:53:10 -04:00
|
|
|
String[] values = bundle.getString(code).split("\\t", 3);
|
|
|
|
return new Language(code, values[0], values[1], values[2]);
|
2009-07-29 16:31:08 -04:00
|
|
|
} catch (Exception e) {
|
2013-12-31 04:12:28 -05:00
|
|
|
return null;
|
2009-05-31 11:19:56 -04:00
|
|
|
}
|
2008-06-21 15:24:18 -04:00
|
|
|
}
|
2013-09-21 03:29:57 -04:00
|
|
|
|
2011-08-09 23:51:21 -04:00
|
|
|
public static List<Language> getLanguages(String... codes) {
|
|
|
|
Language[] languages = new Language[codes.length];
|
2013-09-21 03:29:57 -04:00
|
|
|
|
2011-08-09 23:51:21 -04:00
|
|
|
for (int i = 0; i < codes.length; i++) {
|
|
|
|
languages[i] = getLanguage(codes[i]);
|
2009-05-31 11:19:56 -04:00
|
|
|
}
|
2013-09-21 03:29:57 -04:00
|
|
|
|
2011-08-09 23:51:21 -04:00
|
|
|
return asList(languages);
|
2008-06-21 15:24:18 -04:00
|
|
|
}
|
2013-09-21 03:29:57 -04:00
|
|
|
|
|
|
|
public static Language getLanguage(Locale locale) {
|
2013-12-31 02:59:58 -05:00
|
|
|
if (locale != null) {
|
|
|
|
String code = locale.getLanguage();
|
|
|
|
for (Language it : availableLanguages()) {
|
2014-08-27 15:53:10 -04:00
|
|
|
if (it.matches(code)) {
|
2013-12-31 02:59:58 -05:00
|
|
|
return it;
|
|
|
|
}
|
2013-12-31 02:28:30 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
2013-09-21 03:29:57 -04:00
|
|
|
}
|
|
|
|
|
2014-01-02 10:49:20 -05:00
|
|
|
public static Language findLanguage(String lang) {
|
2011-09-22 08:24:10 -04:00
|
|
|
for (Language it : availableLanguages()) {
|
2014-08-27 15:53:10 -04:00
|
|
|
if (it.matches(lang)) {
|
2011-09-22 08:24:10 -04:00
|
|
|
return it;
|
2014-01-02 10:49:20 -05:00
|
|
|
}
|
2011-09-22 08:24:10 -04:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2013-09-21 03:29:57 -04:00
|
|
|
|
2014-09-04 12:41:20 -04:00
|
|
|
public static String getStandardLanguageCode(String lang) {
|
2013-12-31 02:28:30 -05:00
|
|
|
try {
|
2014-09-04 12:41:20 -04:00
|
|
|
return Language.findLanguage(lang).getISO3();
|
2013-12-31 02:28:30 -05:00
|
|
|
} catch (Exception e) {
|
|
|
|
return null;
|
2011-11-24 12:52:11 -05:00
|
|
|
}
|
|
|
|
}
|
2013-09-21 03:29:57 -04:00
|
|
|
|
2011-08-09 23:51:21 -04:00
|
|
|
public static List<Language> availableLanguages() {
|
|
|
|
ResourceBundle bundle = ResourceBundle.getBundle(Language.class.getName());
|
2014-08-27 15:53:10 -04:00
|
|
|
return getLanguages(bundle.getString("languages.ui").split(","));
|
2011-08-09 23:51:21 -04:00
|
|
|
}
|
2013-09-21 03:29:57 -04:00
|
|
|
|
2011-08-09 23:51:21 -04:00
|
|
|
public static List<Language> commonLanguages() {
|
|
|
|
ResourceBundle bundle = ResourceBundle.getBundle(Language.class.getName());
|
|
|
|
return getLanguages(bundle.getString("languages.common").split(","));
|
|
|
|
}
|
2013-09-21 03:29:57 -04:00
|
|
|
|
2011-08-09 23:51:21 -04:00
|
|
|
public static List<Language> preferredLanguages() {
|
|
|
|
Set<String> codes = new LinkedHashSet<String>();
|
2013-09-21 03:29:57 -04:00
|
|
|
|
2011-08-09 23:51:21 -04:00
|
|
|
// English | System language | common languages
|
|
|
|
codes.add("en");
|
|
|
|
codes.add(Locale.getDefault().getLanguage());
|
2013-09-21 03:29:57 -04:00
|
|
|
|
2011-08-09 23:51:21 -04:00
|
|
|
ResourceBundle bundle = ResourceBundle.getBundle(Language.class.getName());
|
|
|
|
addAll(codes, bundle.getString("languages.common").split(","));
|
2013-09-21 03:29:57 -04:00
|
|
|
|
2011-08-09 23:51:21 -04:00
|
|
|
return getLanguages(codes.toArray(new String[0]));
|
|
|
|
}
|
2013-09-21 03:29:57 -04:00
|
|
|
|
2008-06-21 15:24:18 -04:00
|
|
|
}
|