| 1 | package be.nikiroo.fanfix.bundles; |
| 2 | |
| 3 | import java.io.File; |
| 4 | import java.io.IOException; |
| 5 | import java.util.Locale; |
| 6 | |
| 7 | import be.nikiroo.utils.resources.TransBundle; |
| 8 | |
| 9 | /** |
| 10 | * This class manages the translation resources of the application. |
| 11 | * |
| 12 | * @author niki |
| 13 | */ |
| 14 | public class StringIdBundle extends TransBundle<StringId> { |
| 15 | private String lang; |
| 16 | |
| 17 | /** |
| 18 | * Create a translation service for the given language (will fall back to |
| 19 | * the default one i not found). |
| 20 | * |
| 21 | * @param lang |
| 22 | * the language to use |
| 23 | */ |
| 24 | public StringIdBundle(String lang) { |
| 25 | super(StringId.class, Target.resources, lang); |
| 26 | this.lang = lang; |
| 27 | } |
| 28 | |
| 29 | /** |
| 30 | * Return the currently used language as a String. |
| 31 | * |
| 32 | * @return the language |
| 33 | * |
| 34 | * @deprecated use the call from {@link TransBundle} when available |
| 35 | */ |
| 36 | public Locale getLanguage() { |
| 37 | return getLocaleFor(lang); |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Update resource file. |
| 42 | * |
| 43 | * @param args |
| 44 | * not used |
| 45 | * |
| 46 | * @throws IOException |
| 47 | * in case of I/O error |
| 48 | */ |
| 49 | public static void main(String[] args) throws IOException { |
| 50 | String path = new File(".").getAbsolutePath() |
| 51 | + "/src/be/nikiroo/fanfix/bundles/"; |
| 52 | new StringIdBundle(null).updateFile(path); |
| 53 | System.out.println("Path updated: " + path); |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Return the {@link Locale} representing the given language. |
| 58 | * |
| 59 | * @param language |
| 60 | * the language to initialise, in the form "en-GB" or "fr" for |
| 61 | * instance |
| 62 | * |
| 63 | * @return the corresponding {@link Locale} or the default {@link Locale} if |
| 64 | * it is not known |
| 65 | * |
| 66 | * @deprecated Use the call from {@link TransBundle} when available. |
| 67 | */ |
| 68 | static private Locale getLocaleFor(String language) { |
| 69 | Locale locale; |
| 70 | |
| 71 | if (language == null) { |
| 72 | locale = Locale.getDefault(); |
| 73 | } else { |
| 74 | language = language.replaceAll("_", "-"); |
| 75 | String lang = language; |
| 76 | String country = null; |
| 77 | if (language.contains("-")) { |
| 78 | lang = language.split("-")[0]; |
| 79 | country = language.split("-")[1]; |
| 80 | } |
| 81 | |
| 82 | if (country != null) |
| 83 | locale = new Locale(lang, country); |
| 84 | else |
| 85 | locale = new Locale(lang); |
| 86 | } |
| 87 | |
| 88 | return locale; |
| 89 | } |
| 90 | } |