diff options
| author | 2020-03-05 22:17:08 +0000 | |
|---|---|---|
| committer | 2020-03-05 22:17:08 +0000 | |
| commit | 863d40a1c1f6591ef1ee8594b12ae4b0942fe810 (patch) | |
| tree | 614c0e3bc842e1ab50413dcc18b450c96224db10 /src/main/java/cuchaz/enigma/utils | |
| parent | Fix drop mappings not checking localVars (diff) | |
| download | enigma-fork-863d40a1c1f6591ef1ee8594b12ae4b0942fe810.tar.gz enigma-fork-863d40a1c1f6591ef1ee8594b12ae4b0942fe810.tar.xz enigma-fork-863d40a1c1f6591ef1ee8594b12ae4b0942fe810.zip | |
Made Enigma gui translatable (#193)
* made enigma gui translatable
* key renamings
* missed strings
* string.format() & another missed string
* cached content (thanks @liach)
* added a dialog when changing language
* better sentence
* more %s
* liach's requests
* empty map
* the last (?) missed strings
* IT WORKS
* French translation
* Update fr_fr.json
Diffstat (limited to 'src/main/java/cuchaz/enigma/utils')
| -rw-r--r-- | src/main/java/cuchaz/enigma/utils/I18n.java | 102 |
1 files changed, 102 insertions, 0 deletions
diff --git a/src/main/java/cuchaz/enigma/utils/I18n.java b/src/main/java/cuchaz/enigma/utils/I18n.java new file mode 100644 index 0000000..f91c916 --- /dev/null +++ b/src/main/java/cuchaz/enigma/utils/I18n.java | |||
| @@ -0,0 +1,102 @@ | |||
| 1 | package cuchaz.enigma.utils; | ||
| 2 | |||
| 3 | import java.io.BufferedReader; | ||
| 4 | import java.io.IOException; | ||
| 5 | import java.io.InputStream; | ||
| 6 | import java.io.InputStreamReader; | ||
| 7 | import java.nio.charset.StandardCharsets; | ||
| 8 | import java.util.ArrayList; | ||
| 9 | import java.util.Collections; | ||
| 10 | import java.util.Map; | ||
| 11 | import java.util.stream.Stream; | ||
| 12 | |||
| 13 | import com.google.common.collect.ImmutableList; | ||
| 14 | import com.google.common.collect.Maps; | ||
| 15 | import com.google.common.reflect.ClassPath; | ||
| 16 | import com.google.common.reflect.ClassPath.ResourceInfo; | ||
| 17 | import com.google.gson.Gson; | ||
| 18 | |||
| 19 | import cuchaz.enigma.config.Config; | ||
| 20 | |||
| 21 | public class I18n { | ||
| 22 | public static final String DEFAULT_LANGUAGE = "en_us"; | ||
| 23 | private static final Gson GSON = new Gson(); | ||
| 24 | private static Map<String, String> translations = Maps.newHashMap(); | ||
| 25 | private static Map<String, String> defaultTranslations = Maps.newHashMap(); | ||
| 26 | private static Map<String, String> languageNames = Maps.newHashMap(); | ||
| 27 | |||
| 28 | static { | ||
| 29 | translations = load(Config.getInstance().language); | ||
| 30 | defaultTranslations = load(DEFAULT_LANGUAGE); | ||
| 31 | } | ||
| 32 | |||
| 33 | @SuppressWarnings("unchecked") | ||
| 34 | public static Map<String, String> load(String language) { | ||
| 35 | try (InputStream inputStream = I18n.class.getResourceAsStream("/lang/" + language + ".json")) { | ||
| 36 | if (inputStream != null) { | ||
| 37 | try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8))) { | ||
| 38 | return GSON.fromJson(reader, Map.class); | ||
| 39 | } | ||
| 40 | } | ||
| 41 | } catch (IOException e) { | ||
| 42 | e.printStackTrace(); | ||
| 43 | } | ||
| 44 | return Collections.emptyMap(); | ||
| 45 | } | ||
| 46 | |||
| 47 | public static String translate(String key) { | ||
| 48 | String value = translations.get(key); | ||
| 49 | if (value != null) { | ||
| 50 | return value; | ||
| 51 | } | ||
| 52 | value = defaultTranslations.get(key); | ||
| 53 | if (value != null) { | ||
| 54 | return value; | ||
| 55 | } | ||
| 56 | return key; | ||
| 57 | } | ||
| 58 | |||
| 59 | public static String getLanguageName(String language) { | ||
| 60 | return languageNames.get(language); | ||
| 61 | } | ||
| 62 | |||
| 63 | public static void setLanguage(String language) { | ||
| 64 | Config.getInstance().language = language; | ||
| 65 | try { | ||
| 66 | Config.getInstance().saveConfig(); | ||
| 67 | } catch (IOException e) { | ||
| 68 | e.printStackTrace(); | ||
| 69 | } | ||
| 70 | } | ||
| 71 | |||
| 72 | public static ArrayList<String> getAvailableLanguages() { | ||
| 73 | ArrayList<String> list = new ArrayList<String>(); | ||
| 74 | |||
| 75 | try { | ||
| 76 | ImmutableList<ResourceInfo> resources = ClassPath.from(Thread.currentThread().getContextClassLoader()).getResources().asList(); | ||
| 77 | Stream<ResourceInfo> dirStream = resources.stream(); | ||
| 78 | dirStream.forEach(context -> { | ||
| 79 | String file = context.getResourceName(); | ||
| 80 | if (file.startsWith("lang/") && file.endsWith(".json")) { | ||
| 81 | String fileName = file.substring(5, file.length() - 5); | ||
| 82 | list.add(fileName); | ||
| 83 | loadLanguageName(fileName); | ||
| 84 | } | ||
| 85 | }); | ||
| 86 | } catch (IOException e) { | ||
| 87 | e.printStackTrace(); | ||
| 88 | } | ||
| 89 | return list; | ||
| 90 | } | ||
| 91 | |||
| 92 | private static void loadLanguageName(String fileName) { | ||
| 93 | try (InputStream stream = Thread.currentThread().getContextClassLoader().getResourceAsStream("lang/" + fileName + ".json")) { | ||
| 94 | try (BufferedReader reader = new BufferedReader(new InputStreamReader(stream, StandardCharsets.UTF_8))) { | ||
| 95 | Map<?, ?> map = GSON.fromJson(reader, Map.class); | ||
| 96 | languageNames.put(fileName, map.get("language").toString()); | ||
| 97 | } | ||
| 98 | } catch (IOException e) { | ||
| 99 | e.printStackTrace(); | ||
| 100 | } | ||
| 101 | } | ||
| 102 | } | ||