From b9f76c407c816e600f399fe9902e2fe6015f0c43 Mon Sep 17 00:00:00 2001 From: Reinhard Pointner Date: Tue, 31 Dec 2013 07:28:30 +0000 Subject: [PATCH] * include 3-letter language codes in Language config file --- .../filebot/{ui => }/Language.java | 52 +++++++---- .../sourceforge/filebot/Language.properties | 45 +++++++++ .../filebot/cli/CmdlineOperations.java | 2 +- .../filebot/subtitle/SubtitleUtilities.java | 2 +- .../filebot/ui/Language.properties | 45 --------- .../filebot/ui/LanguageComboBox.java | 3 +- .../ui/LanguageComboBoxCellRenderer.java | 24 ++--- .../filebot/ui/LanguageComboBoxModel.java | 6 +- .../ui/episodelist/EpisodeListPanel.java | 2 +- .../filebot/ui/rename/RenamePanel.java | 2 +- .../filebot/ui/subtitle/SubtitlePackage.java | 92 +++++++------------ .../filebot/ui/subtitle/SubtitlePanel.java | 2 +- .../ui/subtitle/SubtitleUploadDialog.java | 4 +- 13 files changed, 133 insertions(+), 148 deletions(-) rename source/net/sourceforge/filebot/{ui => }/Language.java (72%) create mode 100644 source/net/sourceforge/filebot/Language.properties delete mode 100644 source/net/sourceforge/filebot/ui/Language.properties diff --git a/source/net/sourceforge/filebot/ui/Language.java b/source/net/sourceforge/filebot/Language.java similarity index 72% rename from source/net/sourceforge/filebot/ui/Language.java rename to source/net/sourceforge/filebot/Language.java index 2b94b7e6..b69b0f68 100644 --- a/source/net/sourceforge/filebot/ui/Language.java +++ b/source/net/sourceforge/filebot/Language.java @@ -1,4 +1,4 @@ -package net.sourceforge.filebot.ui; +package net.sourceforge.filebot; import static java.util.Arrays.*; import static java.util.Collections.*; @@ -12,16 +12,26 @@ import java.util.Set; public class Language { - private final String code; + private final String iso2; + private final String iso3; private final String name; - public Language(String code, String name) { - this.code = code; + public Language(String iso2, String iso3, String name) { + this.iso2 = iso2; + this.iso3 = iso3; this.name = name; } public String getCode() { - return code; + return iso2; + } + + public String getISO2() { + return iso2; + } + + public String getISO3() { + return iso3; } public String getName() { @@ -39,7 +49,7 @@ public class Language { @Override public Language clone() { - return new Language(code, name); + return new Language(iso2, iso3, name); } public static final Comparator ALPHABETIC_ORDER = new Comparator() { @@ -54,12 +64,14 @@ public class Language { ResourceBundle bundle = ResourceBundle.getBundle(Language.class.getName()); try { - return new Language(code, bundle.getString(code + ".name")); + String[] values = bundle.getString(code).split("\\t", 2); + return new Language(code, values[0], values[1]); } catch (Exception e) { if (code == null || code.isEmpty()) { return null; } - return new Language(code, new Locale(code).getDisplayLanguage(Locale.ROOT)); + Locale locale = new Locale(code); + return new Language(locale.getLanguage(), locale.getISO3Language(), locale.getDisplayLanguage(Locale.ENGLISH)); } } @@ -74,7 +86,16 @@ public class Language { } public static Language getLanguage(Locale locale) { - return locale == null ? null : getLanguageByName(locale.getDisplayLanguage(Locale.ENGLISH)); + if (locale == null) + return null; + + String code = locale.getLanguage(); + for (Language it : availableLanguages()) { + if (it.getISO2().equals(code) || it.getISO3().equals(code)) { + return it; + } + } + return null; } public static Language getLanguageByName(String name) { @@ -87,16 +108,11 @@ public class Language { } public static String getISO3LanguageCodeByName(String languageName) { - Language language = Language.getLanguageByName(languageName); - if (language != null) { - try { - return new Locale(language.getCode()).getISO3Language(); - } catch (Exception e) { - return language.getCode(); - } + try { + return Language.getLanguageByName(languageName).getISO3(); + } catch (Exception e) { + return null; } - - return null; } public static List availableLanguages() { diff --git a/source/net/sourceforge/filebot/Language.properties b/source/net/sourceforge/filebot/Language.properties new file mode 100644 index 00000000..d187af8b --- /dev/null +++ b/source/net/sourceforge/filebot/Language.properties @@ -0,0 +1,45 @@ +# available languages +languages.all = sq,ar,hy,pb,bg,ca,zh,hr,cs,da,nl,en,et,fi,fr,de,el,he,hi,hu,id,it,ja,ko,lv,lt,mk,ms,no,fa,pl,pt,ro,ru,sr,sk,sl,es,sv,th,tr,vi +languages.common = en,de,fr,es,pt,ru,ja,zh +sq: sqi Albanian +ar: ara Arabic +hy: hye Armenian +pb: pob Brazilian +bg: bul Bulgarian +ca: cat Catalan +zh: zho Chinese +hr: hrv Croatian +cs: ces Czech +da: dan Danish +nl: nld Dutch +en: eng English +et: est Estonian +fi: fin Finnish +fr: fra French +de: deu German +el: ell Greek +he: heb Hebrew +hi: hin Hindi +hu: hun Hungarian +id: ind Indonesian +it: ita Italian +ja: jpn Japanese +ko: kor Korean +lv: lav Latvian +lt: lit Lithuanian +mk: mkd Macedonian +ms: msa Malay +no: nor Norwegian +fa: fas Persian +pl: pol Polish +pt: por Portuguese +ro: ron Romanian +ru: rus Russian +sr: srp Serbian +sk: slk Slovak +sl: slv Slovenian +es: spa Spanish +sv: swe Swedish +th: tha Thai +tr: tur Turkish +vi: vie Vietnamese diff --git a/source/net/sourceforge/filebot/cli/CmdlineOperations.java b/source/net/sourceforge/filebot/cli/CmdlineOperations.java index 09693fbc..d3f17076 100644 --- a/source/net/sourceforge/filebot/cli/CmdlineOperations.java +++ b/source/net/sourceforge/filebot/cli/CmdlineOperations.java @@ -57,7 +57,7 @@ import net.sourceforge.filebot.similarity.SeriesNameMatcher; import net.sourceforge.filebot.similarity.SimilarityComparator; import net.sourceforge.filebot.similarity.SimilarityMetric; import net.sourceforge.filebot.subtitle.SubtitleFormat; -import net.sourceforge.filebot.ui.Language; +import net.sourceforge.filebot.Language; import net.sourceforge.filebot.vfs.MemoryFile; import net.sourceforge.filebot.web.AudioTrack; import net.sourceforge.filebot.web.Episode; diff --git a/source/net/sourceforge/filebot/subtitle/SubtitleUtilities.java b/source/net/sourceforge/filebot/subtitle/SubtitleUtilities.java index 0d71d39c..5f9d0131 100644 --- a/source/net/sourceforge/filebot/subtitle/SubtitleUtilities.java +++ b/source/net/sourceforge/filebot/subtitle/SubtitleUtilities.java @@ -33,7 +33,7 @@ import net.sourceforge.filebot.similarity.MetricCascade; import net.sourceforge.filebot.similarity.NameSimilarityMetric; import net.sourceforge.filebot.similarity.SequenceMatchSimilarity; import net.sourceforge.filebot.similarity.SimilarityMetric; -import net.sourceforge.filebot.ui.Language; +import net.sourceforge.filebot.Language; import net.sourceforge.filebot.vfs.ArchiveType; import net.sourceforge.filebot.vfs.MemoryFile; import net.sourceforge.filebot.web.SearchResult; diff --git a/source/net/sourceforge/filebot/ui/Language.properties b/source/net/sourceforge/filebot/ui/Language.properties deleted file mode 100644 index 5b213bb0..00000000 --- a/source/net/sourceforge/filebot/ui/Language.properties +++ /dev/null @@ -1,45 +0,0 @@ -# available languages -languages.all = sq,ar,hy,pb,bg,ca,zh,hr,cs,da,nl,en,et,fi,fr,de,el,he,hi,hu,id,it,ja,ko,lv,lt,mk,ms,no,fa,pl,pt,ro,ru,sr,sk,sl,es,sv,th,tr,vi -languages.common = en,de,fr,es,pt,ru,ja,zh -sq.name: Albanian -ar.name: Arabic -hy.name: Armenian -pb.name: Brazilian -bg.name: Bulgarian -ca.name: Catalan -zh.name: Chinese -hr.name: Croatian -cs.name: Czech -da.name: Danish -nl.name: Dutch -en.name: English -et.name: Estonian -fi.name: Finnish -fr.name: French -de.name: German -el.name: Greek -he.name: Hebrew -hi.name: Hindi -hu.name: Hungarian -id.name: Indonesian -it.name: Italian -ja.name: Japanese -ko.name: Korean -lv.name: Latvian -lt.name: Lithuanian -mk.name: Macedonian -ms.name: Malay -no.name: Norwegian -fa.name: Persian -pl.name: Polish -pt.name: Portuguese -ro.name: Romanian -ru.name: Russian -sr.name: Serbian -sk.name: Slovak -sl.name: Slovenian -es.name: Spanish -sv.name: Swedish -th.name: Thai -tr.name: Turkish -vi.name: Vietnamese diff --git a/source/net/sourceforge/filebot/ui/LanguageComboBox.java b/source/net/sourceforge/filebot/ui/LanguageComboBox.java index 39804d7c..5a8af121 100644 --- a/source/net/sourceforge/filebot/ui/LanguageComboBox.java +++ b/source/net/sourceforge/filebot/ui/LanguageComboBox.java @@ -1,7 +1,7 @@ package net.sourceforge.filebot.ui; import static java.awt.event.ItemEvent.*; -import static net.sourceforge.filebot.ui.Language.*; +import static net.sourceforge.filebot.Language.*; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; @@ -16,6 +16,7 @@ import javax.swing.JComboBox; import javax.swing.event.PopupMenuEvent; import javax.swing.event.PopupMenuListener; +import net.sourceforge.filebot.Language; import net.sourceforge.filebot.Settings; public class LanguageComboBox extends JComboBox { diff --git a/source/net/sourceforge/filebot/ui/LanguageComboBoxCellRenderer.java b/source/net/sourceforge/filebot/ui/LanguageComboBoxCellRenderer.java index 3bf066ee..f790ac78 100644 --- a/source/net/sourceforge/filebot/ui/LanguageComboBoxCellRenderer.java +++ b/source/net/sourceforge/filebot/ui/LanguageComboBoxCellRenderer.java @@ -1,7 +1,5 @@ - package net.sourceforge.filebot.ui; - import java.awt.Color; import java.awt.Component; @@ -12,48 +10,46 @@ import javax.swing.border.Border; import javax.swing.border.CompoundBorder; import javax.swing.border.EmptyBorder; +import net.sourceforge.filebot.Language; import net.sourceforge.filebot.ResourceManager; import net.sourceforge.tuned.ui.DashedSeparator; - public class LanguageComboBoxCellRenderer implements ListCellRenderer { - + private Border padding = new EmptyBorder(2, 2, 2, 2); - + private Border favoritePadding = new EmptyBorder(0, 6, 0, 6); - + private ListCellRenderer base; - public LanguageComboBoxCellRenderer(final ListCellRenderer base) { this.base = base; this.padding = new CompoundBorder(padding, ((JLabel) base).getBorder()); } - @Override public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { JLabel c = (JLabel) base.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus); - + Language language = (Language) value; c.setText(language.getName()); c.setIcon(ResourceManager.getFlagIcon(language.getCode())); - + // default padding c.setBorder(padding); - + LanguageComboBoxModel model = (LanguageComboBoxModel) list.getModel(); - + if ((index > 0 && index <= model.favorites().size())) { // add favorite padding c.setBorder(new CompoundBorder(favoritePadding, c.getBorder())); } - + if (index == 0 || index == model.favorites().size()) { // add separator border c.setBorder(new CompoundBorder(new DashedSeparator(10, 4, Color.lightGray, list.getBackground()), c.getBorder())); } - + return c; } } diff --git a/source/net/sourceforge/filebot/ui/LanguageComboBoxModel.java b/source/net/sourceforge/filebot/ui/LanguageComboBoxModel.java index 75d3911e..428575c3 100644 --- a/source/net/sourceforge/filebot/ui/LanguageComboBoxModel.java +++ b/source/net/sourceforge/filebot/ui/LanguageComboBoxModel.java @@ -1,6 +1,6 @@ package net.sourceforge.filebot.ui; -import static net.sourceforge.filebot.ui.Language.*; +import static net.sourceforge.filebot.Language.*; import java.util.AbstractList; import java.util.ArrayList; @@ -10,9 +10,11 @@ import java.util.Set; import javax.swing.AbstractListModel; import javax.swing.ComboBoxModel; +import net.sourceforge.filebot.Language; + public class LanguageComboBoxModel extends AbstractListModel implements ComboBoxModel { - public static final Language ALL_LANGUAGES = new Language("undefined", "All Languages"); + public static final Language ALL_LANGUAGES = new Language("", "", "All Languages"); private Language defaultLanguage; private Language selection; diff --git a/source/net/sourceforge/filebot/ui/episodelist/EpisodeListPanel.java b/source/net/sourceforge/filebot/ui/episodelist/EpisodeListPanel.java index b7fdfd6d..cb08ec92 100644 --- a/source/net/sourceforge/filebot/ui/episodelist/EpisodeListPanel.java +++ b/source/net/sourceforge/filebot/ui/episodelist/EpisodeListPanel.java @@ -32,7 +32,7 @@ import net.sourceforge.filebot.ui.AbstractSearchPanel; import net.sourceforge.filebot.ui.FileBotList; import net.sourceforge.filebot.ui.FileBotListExportHandler; import net.sourceforge.filebot.ui.FileBotTab; -import net.sourceforge.filebot.ui.Language; +import net.sourceforge.filebot.Language; import net.sourceforge.filebot.ui.LanguageComboBox; import net.sourceforge.filebot.ui.SelectDialog; import net.sourceforge.filebot.ui.transfer.ArrayTransferable; diff --git a/source/net/sourceforge/filebot/ui/rename/RenamePanel.java b/source/net/sourceforge/filebot/ui/rename/RenamePanel.java index 3b95691f..24778e3a 100644 --- a/source/net/sourceforge/filebot/ui/rename/RenamePanel.java +++ b/source/net/sourceforge/filebot/ui/rename/RenamePanel.java @@ -53,7 +53,7 @@ import net.sourceforge.filebot.StandardRenameAction; import net.sourceforge.filebot.WebServices; import net.sourceforge.filebot.format.MediaBindingBean; import net.sourceforge.filebot.similarity.Match; -import net.sourceforge.filebot.ui.Language; +import net.sourceforge.filebot.Language; import net.sourceforge.filebot.ui.rename.FormatDialog.Mode; import net.sourceforge.filebot.ui.rename.RenameModel.FormattedFuture; import net.sourceforge.filebot.web.AudioTrack; diff --git a/source/net/sourceforge/filebot/ui/subtitle/SubtitlePackage.java b/source/net/sourceforge/filebot/ui/subtitle/SubtitlePackage.java index 589a1646..4848ce4d 100644 --- a/source/net/sourceforge/filebot/ui/subtitle/SubtitlePackage.java +++ b/source/net/sourceforge/filebot/ui/subtitle/SubtitlePackage.java @@ -1,7 +1,5 @@ - package net.sourceforge.filebot.ui.subtitle; - import static java.util.Collections.*; import static net.sourceforge.filebot.MediaTypes.*; @@ -20,35 +18,33 @@ import java.util.ResourceBundle; import javax.swing.SwingWorker; import javax.swing.event.SwingPropertyChangeSupport; -import net.sourceforge.filebot.ui.Language; +import net.sourceforge.filebot.Language; import net.sourceforge.filebot.vfs.ArchiveType; import net.sourceforge.filebot.vfs.MemoryFile; import net.sourceforge.filebot.web.SubtitleDescriptor; import net.sourceforge.filebot.web.SubtitleProvider; import net.sourceforge.tuned.FileUtilities; - public class SubtitlePackage { - + private final SubtitleProvider provider; private final SubtitleDescriptor subtitle; private final Language language; private Download download; - public SubtitlePackage(SubtitleProvider provider, SubtitleDescriptor subtitle) { this.provider = provider; this.subtitle = subtitle; - + // resolve language name - this.language = new Language(languageCodeByName.get(subtitle.getLanguageName()), subtitle.getLanguageName()); - + this.language = new Language(languageCodeByName.get(subtitle.getLanguageName()), Language.getISO3LanguageCodeByName(subtitle.getLanguageName()), subtitle.getLanguageName()); + // initialize download worker download = new Download(subtitle); - + // forward phase events download.addPropertyChangeListener(new PropertyChangeListener() { - + @Override public void propertyChange(PropertyChangeEvent evt) { if (evt.getPropertyName().equals("phase")) { @@ -57,139 +53,119 @@ public class SubtitlePackage { } }); } - public SubtitleProvider getProvider() { return provider; } - public String getName() { return subtitle.getName(); } - public Language getLanguage() { return language; } - public String getType() { return subtitle.getType(); } - public Download getDownload() { return download; } - public void reset() { // cancel old download download.cancel(false); - + // create new download Download old = download; download = new Download(subtitle); - + // transfer listeners for (PropertyChangeListener listener : old.getPropertyChangeSupport().getPropertyChangeListeners()) { old.removePropertyChangeListener(listener); download.addPropertyChangeListener(listener); } - + pcs.firePropertyChange("download.phase", old.getPhase(), download.getPhase()); } - @Override public String toString() { return subtitle.getName(); } - private final PropertyChangeSupport pcs = new SwingPropertyChangeSupport(this, true); - public void addPropertyChangeListener(PropertyChangeListener listener) { pcs.addPropertyChangeListener(listener); } - public void removePropertyChangeListener(PropertyChangeListener listener) { pcs.removePropertyChangeListener(listener); } - public static class Download extends SwingWorker, Void> { - + enum Phase { - PENDING, - WAITING, - DOWNLOADING, - EXTRACTING, - DONE + PENDING, WAITING, DOWNLOADING, EXTRACTING, DONE } - private final SubtitleDescriptor subtitle; - + private Phase current = Phase.PENDING; - private Download(SubtitleDescriptor descriptor) { this.subtitle = descriptor; } - public void start() { setPhase(Phase.WAITING); - + // enqueue worker execute(); } - @Override protected List doInBackground() throws Exception { setPhase(Phase.DOWNLOADING); - + // fetch archive ByteBuffer data = subtitle.fetch(); - + // abort if download has been cancelled if (isCancelled()) return null; - + setPhase(Phase.EXTRACTING); - + ArchiveType archiveType = ArchiveType.forName(subtitle.getType()); - + if (archiveType == ArchiveType.UNKOWN) { // cannot extract files from archive return singletonList(new MemoryFile(subtitle.getPath(), data)); } - + // extract contents of the archive List vfs = extract(archiveType, data); - + // if we can't extract files from a rar archive, it might actually be a zip file with the wrong extension if (vfs.isEmpty() && archiveType != ArchiveType.ZIP) { vfs = extract(ArchiveType.ZIP, data); } - + if (vfs.isEmpty()) { throw new IOException("Cannot extract files from archive"); } - + // return file contents return vfs; } - private List extract(ArchiveType archiveType, ByteBuffer data) throws IOException { List vfs = new ArrayList(); - + for (MemoryFile file : archiveType.fromData(data)) { if (SUBTITLE_FILES.accept(file.getName())) { // add subtitle files, ignore non-subtitle files @@ -197,59 +173,53 @@ public class SubtitlePackage { } else { // check if file is a supported archive ArchiveType type = ArchiveType.forName(FileUtilities.getExtension(file.getName())); - + if (type != ArchiveType.UNKOWN) { // extract nested archives recursively vfs.addAll(extract(type, file.getData())); } } } - + return vfs; } - @Override protected void done() { setPhase(Phase.DONE); } - private void setPhase(Phase phase) { Phase old = current; current = phase; - + firePropertyChange("phase", old, phase); } - public boolean isStarted() { return current != Phase.PENDING; } - public Phase getPhase() { return current; } } - /** * Map english language name to language code. */ private static final Map languageCodeByName = mapLanguageCodeByName(); - private static Map mapLanguageCodeByName() { ResourceBundle bundle = ResourceBundle.getBundle(Language.class.getName(), Locale.ENGLISH); - + Map map = new HashMap(); - + for (String code : bundle.keySet()) { map.put(bundle.getString(code), code); } - + return map; } - + } diff --git a/source/net/sourceforge/filebot/ui/subtitle/SubtitlePanel.java b/source/net/sourceforge/filebot/ui/subtitle/SubtitlePanel.java index 311291b2..6d330cf1 100644 --- a/source/net/sourceforge/filebot/ui/subtitle/SubtitlePanel.java +++ b/source/net/sourceforge/filebot/ui/subtitle/SubtitlePanel.java @@ -36,7 +36,7 @@ import net.sourceforge.filebot.ResourceManager; import net.sourceforge.filebot.Settings; import net.sourceforge.filebot.WebServices; import net.sourceforge.filebot.ui.AbstractSearchPanel; -import net.sourceforge.filebot.ui.Language; +import net.sourceforge.filebot.Language; import net.sourceforge.filebot.ui.LanguageComboBox; import net.sourceforge.filebot.ui.SelectDialog; import net.sourceforge.filebot.web.OpenSubtitlesClient; diff --git a/source/net/sourceforge/filebot/ui/subtitle/SubtitleUploadDialog.java b/source/net/sourceforge/filebot/ui/subtitle/SubtitleUploadDialog.java index 15beb28b..383c036f 100644 --- a/source/net/sourceforge/filebot/ui/subtitle/SubtitleUploadDialog.java +++ b/source/net/sourceforge/filebot/ui/subtitle/SubtitleUploadDialog.java @@ -52,7 +52,7 @@ import net.miginfocom.swing.MigLayout; import net.sourceforge.filebot.Analytics; import net.sourceforge.filebot.ResourceManager; import net.sourceforge.filebot.media.MediaDetection; -import net.sourceforge.filebot.ui.Language; +import net.sourceforge.filebot.Language; import net.sourceforge.filebot.ui.LanguageComboBox; import net.sourceforge.filebot.ui.SelectDialog; import net.sourceforge.filebot.web.Movie; @@ -423,7 +423,7 @@ public class SubtitleUploadDialog extends JDialog { icon = ResourceManager.getIcon("database.ok"); break; case Identifying: - text = "Auto-detect missing information"; + text = "Auto-detecting missing information"; icon = ResourceManager.getIcon("action.export"); break; case IdentificationRequired: