001package net.filebot;
002
003import static java.util.Arrays.*;
004import static java.util.Collections.*;
005import static java.util.Comparator.*;
006import static java.util.stream.Collectors.*;
007import static net.filebot.Logging.*;
008import static net.filebot.util.RegularExpressions.*;
009
010import java.io.Serializable;
011import java.util.Comparator;
012import java.util.List;
013import java.util.Locale;
014import java.util.MissingResourceException;
015import java.util.ResourceBundle;
016import java.util.stream.Stream;
017
018public class Language implements Serializable {
019
020        // ISO 639-1 code
021        private final String iso_639_1;
022
023        // ISO 639-3 code (mostly identical to ISO 639-2/T)
024        private final String iso_639_3;
025
026        // ISO 639-2/B code
027        private final String iso_639_2B;
028
029        // BCP 47 language tag
030        private final String tag;
031
032        // Language name
033        private final String[] names;
034
035        public Language(String iso_639_1, String iso_639_3, String iso_639_2B, String tag, String[] names) {
036                this.iso_639_1 = iso_639_1;
037                this.iso_639_3 = iso_639_3;
038                this.iso_639_2B = iso_639_2B;
039                this.tag = tag;
040                this.names = names.clone();
041        }
042
043        public String getCode() {
044                return iso_639_1;
045        }
046
047        public String getISO2() {
048                return iso_639_1;
049        }
050
051        public String getISO3() {
052                return iso_639_3; // 3-letter code
053        }
054
055        public String getISO3B() {
056                return iso_639_2B; // alternative 3-letter code
057        }
058
059        public String getTag() {
060                return tag;
061        }
062
063        public String getName() {
064                return names[0];
065        }
066
067        public List<String> getNames() {
068                return unmodifiableList(asList(names));
069        }
070
071        @Override
072        public String toString() {
073                return iso_639_3;
074        }
075
076        public Locale getLocale() {
077                Locale locale = Locale.forLanguageTag(tag);
078
079                // e.g. x-jat
080                if (locale == null || locale.getLanguage().isEmpty()) {
081                        return new Locale(iso_639_1);
082                }
083
084                return locale;
085        }
086
087        public boolean matches(String code) {
088                return Stream.concat(Stream.of(iso_639_1, iso_639_2B, iso_639_3, tag), Stream.of(names)).anyMatch(c -> c.equalsIgnoreCase(code));
089        }
090
091        @Override
092        public Language clone() {
093                return new Language(iso_639_1, iso_639_3, iso_639_2B, tag, names);
094        }
095
096        public static final Comparator<Language> ALPHABETIC_ORDER = comparing(Language::getName, String::compareToIgnoreCase);
097
098        public static Language getLanguage(String code) {
099                if (code == null || code.isEmpty()) {
100                        return null;
101                }
102
103                try {
104                        String[] values = TAB.split(getProperty(code), 4);
105                        return new Language(code, values[0], values[1], values[2], TAB.split(values[3]));
106                } catch (Exception e) {
107                        debug.finest(cause(e)); // log and ignore
108                }
109
110                return null;
111        }
112
113        public static List<Language> getLanguages(String... codes) {
114                return stream(codes).map(Language::getLanguage).collect(toList());
115        }
116
117        public static Language getLanguage(Locale locale) {
118                return locale == null ? null : findLanguage(locale.getLanguage());
119        }
120
121        public static Language findLanguage(String language) {
122                return availableLanguages().stream().filter(it -> it.matches(language)).findFirst().orElse(null);
123        }
124
125        public static String getStandardLanguageCode(String lang) {
126                try {
127                        return Language.findLanguage(lang).getISO3();
128                } catch (Exception e) {
129                        return null;
130                }
131        }
132
133        public static List<Language> availableLanguages() {
134                String languages = getProperty("languages.ui");
135                return getLanguages(SPACE.split(languages));
136        }
137
138        public static List<Language> commonLanguages() {
139                String languages = getProperty("languages.common");
140                return getLanguages(SPACE.split(languages));
141        }
142
143        public static List<Language> preferredLanguages() {
144                // English | system language | common languages
145                Stream<String> codes = Stream.of(Locale.ENGLISH, Locale.getDefault()).map(Locale::getLanguage);
146
147                // append common languages
148                codes = Stream.concat(codes, SPACE.splitAsStream(getProperty("languages.common"))).distinct();
149
150                return codes.map(Language::getLanguage).collect(toList());
151        }
152
153        private static String getProperty(String key) {
154                try {
155                        return ResourceBundle.getBundle(Language.class.getName()).getString(key);
156                } catch (MissingResourceException e) {
157                        throw new IllegalArgumentException("Illegal language code: " + key);
158                }
159        }
160
161}