|
24 | 24 |
|
25 | 25 | public class LanguageManager { |
26 | 26 |
|
27 | | - private static Set<String> modids = new HashSet<>(); |
28 | | - private static final Splitter SPLITTER = Splitter.on('=').limit(2); |
29 | | - private static final Pattern PATTERN = Pattern.compile("%(\\d+\\$)?[\\d\\.]*[df]"); |
| 27 | + private static Set<String> modids = new HashSet<>(); |
| 28 | + private static final Splitter SPLITTER = Splitter.on('=').limit(2); |
| 29 | + private static final Pattern PATTERN = Pattern.compile("%(\\d+\\$)?[\\d\\.]*[df]"); |
30 | 30 |
|
31 | | - public static void onResourceManagerReload(Map<String, String> original, IResourceManager iResourceManager, List<String> languageList) { |
32 | | - for (String language : languageList) { // Go through all loaded languages |
33 | | - language = language.toLowerCase(); // Set everything to lowercase which prevents headaches in 1.10.2 and below |
34 | | - for (String modid : modids) { // Iterate through all registered modids |
35 | | - HashMap<String, String> newTranslations = new HashMap<>(); |
36 | | - if (iResourceManager.getResourceDomains().contains(modid)) { |
37 | | - try { |
38 | | - newTranslations = getFromResourcePack(iResourceManager, modid, language); // Load .json translations from resource pack |
39 | | - } catch (IOException var9) { |
40 | | - } |
41 | | - } |
42 | | - if (newTranslations.isEmpty()) { |
43 | | - try { |
44 | | - newTranslations = loadLang(getFromResources(modid, language, "lang")); // Load .lang files from resources |
45 | | - } catch (IOException e) { |
46 | | - throw new RuntimeException(e); |
47 | | - } |
48 | | - } |
49 | | - if (newTranslations.isEmpty()) { |
50 | | - newTranslations = loadJson(getFromResources(modid, language, "json")); // Load .json translations from resources |
51 | | - } |
52 | | - /** |
53 | | - * Making this put if absent here creates the following hirarchy: |
54 | | - * Resourcepack .lang beats |
55 | | - * Resourcepack .json beats |
56 | | - * Resources .lang beats |
57 | | - * Resources .json |
58 | | - * |
59 | | - * Lang is preferred over json and resourcepacks are preferred over json |
60 | | - */ |
61 | | - newTranslations.forEach(original::putIfAbsent); |
62 | | - } |
63 | | - } |
64 | | - } |
| 31 | + public static void onResourceManagerReload(Map<String, String> original, IResourceManager iResourceManager, List<String> languageList) { |
| 32 | + for (String language : languageList) { // Go through all loaded languages |
| 33 | + language = language.toLowerCase(); // Set everything to lowercase which prevents headaches in 1.10.2 and below |
| 34 | + for (String modid : modids) { // Iterate through all registered modids |
| 35 | + HashMap<String, String> newTranslations = new HashMap<>(); |
| 36 | + if (iResourceManager.getResourceDomains().contains(modid)) { |
| 37 | + try { |
| 38 | + newTranslations = getFromResourcePack(iResourceManager, modid, language); // Load .json translations from resource pack |
| 39 | + } catch (IOException var9) { |
| 40 | + } |
| 41 | + } |
| 42 | + if (newTranslations.isEmpty()) { |
| 43 | + try { |
| 44 | + newTranslations = loadLang(getFromResources(modid, language, "lang")); // Load .lang files from resources |
| 45 | + } catch (IOException e) { |
| 46 | + throw new RuntimeException(e); |
| 47 | + } |
| 48 | + } |
| 49 | + if (newTranslations.isEmpty()) { |
| 50 | + newTranslations = loadJson(getFromResources(modid, language, "json")); // Load .json translations from resources |
| 51 | + } |
| 52 | + /** |
| 53 | + * Making this put if absent here creates the following hirarchy: |
| 54 | + * Resourcepack .lang beats |
| 55 | + * Resourcepack .json beats |
| 56 | + * Resources .lang beats |
| 57 | + * Resources .json |
| 58 | + * |
| 59 | + * Lang is preferred over json and resourcepacks are preferred over json |
| 60 | + */ |
| 61 | + newTranslations.forEach(original::putIfAbsent); |
| 62 | + } |
| 63 | + } |
| 64 | + } |
65 | 65 |
|
66 | | - private static InputStream getFromResources(String resourceDomain, String language, String fileending) { |
67 | | - return LanguageManager.class.getResourceAsStream(String.format("/assets/%s/lang/%s.%s", resourceDomain, language, fileending)); |
68 | | - } |
| 66 | + private static InputStream getFromResources(String resourceDomain, String language, String fileending) { |
| 67 | + return LanguageManager.class.getResourceAsStream(String.format("/assets/%s/lang/%s.%s", resourceDomain, language, fileending)); |
| 68 | + } |
69 | 69 |
|
70 | | - private static HashMap<String, String> getFromResourcePack(IResourceManager iResourceManager, String resourceDomain, String language) throws IOException { |
71 | | - String languageFile = String.format("lang/%s.json", language); |
72 | | - HashMap<String, String> out = new HashMap<>(); |
73 | | - Collection<IResource> allResources = iResourceManager.getAllResources(new ResourceLocation(resourceDomain, languageFile)); |
74 | | - for (IResource iResource : allResources) { |
75 | | - InputStream inputStream = iResource.getInputStream(); |
76 | | - out.putAll(loadJson(inputStream)); |
77 | | - } |
78 | | - return out; |
79 | | - } |
| 70 | + private static HashMap<String, String> getFromResourcePack(IResourceManager iResourceManager, String resourceDomain, String language) throws IOException { |
| 71 | + String languageFile = String.format("lang/%s.json", language); |
| 72 | + HashMap<String, String> out = new HashMap<>(); |
| 73 | + Collection<IResource> allResources = iResourceManager.getAllResources(new ResourceLocation(resourceDomain, languageFile)); |
| 74 | + for (IResource iResource : allResources) { |
| 75 | + InputStream inputStream = iResource.getInputStream(); |
| 76 | + out.putAll(loadJson(inputStream)); |
| 77 | + } |
| 78 | + return out; |
| 79 | + } |
80 | 80 |
|
81 | | - /** |
82 | | - * Registers your mod to be processed by the language manager<br> |
83 | | - * This will allow you to add .json and/or .lang files to assets/modid/lang<br> |
84 | | - * with en_us.lang/en_us.json (<strong>lowercase!</strong>) |
85 | | - * |
86 | | - * @param modid The modid of your mod |
87 | | - */ |
88 | | - public static void registerMod(String modid) { |
89 | | - modids.add(modid); |
90 | | - } |
| 81 | + /** |
| 82 | + * Registers your mod to be processed by the language manager<br> |
| 83 | + * This will allow you to add .json and/or .lang files to assets/modid/lang<br> |
| 84 | + * with en_us.lang/en_us.json (<strong>lowercase!</strong>) |
| 85 | + * |
| 86 | + * @param modid The modid of your mod |
| 87 | + */ |
| 88 | + public static void registerMod(String modid) { |
| 89 | + modids.add(modid); |
| 90 | + } |
91 | 91 |
|
92 | | - private static HashMap<String, String> loadJson(InputStream inputStream) { |
93 | | - if (inputStream == null) { |
94 | | - return new HashMap<String, String>(); |
95 | | - } |
96 | | - Gson gson = new Gson(); |
97 | | - HashMap<String, String> template = new HashMap<>(); |
98 | | - |
99 | | - @SuppressWarnings("unchecked") |
| 92 | + private static HashMap<String, String> loadJson(InputStream inputStream) { |
| 93 | + if (inputStream == null) { |
| 94 | + return new HashMap<String, String>(); |
| 95 | + } |
| 96 | + Gson gson = new Gson(); |
| 97 | + HashMap<String, String> template = new HashMap<>(); |
| 98 | + |
| 99 | + @SuppressWarnings("unchecked") |
100 | 100 | HashMap<String, String> out = (HashMap<String, String>) gson.fromJson(new InputStreamReader(inputStream), template.getClass()); |
101 | | - out.forEach((key, value) -> { |
102 | | - value = PATTERN.matcher(value).replaceAll("%$1s"); |
103 | | - }); |
104 | | - return out; |
105 | | - } |
| 101 | + out.forEach((key, value) -> { |
| 102 | + value = PATTERN.matcher(value).replaceAll("%$1s"); |
| 103 | + }); |
| 104 | + return out; |
| 105 | + } |
106 | 106 |
|
107 | | - private static HashMap<String, String> loadLang(InputStream inputStream) throws IOException { |
108 | | - HashMap<String, String> out = new HashMap<>(); |
109 | | - if (inputStream == null) { |
110 | | - return out; |
111 | | - } |
112 | | - for (String string : IOUtils.readLines(inputStream, StandardCharsets.UTF_8)) { |
113 | | - if (!string.isEmpty() && string.charAt(0) != '#') { |
114 | | - String[] key_value_pair = Iterables.toArray(SPLITTER.split(string), String.class); |
115 | | - if (key_value_pair != null && key_value_pair.length == 2) { |
116 | | - String key = key_value_pair[0]; |
117 | | - String value = PATTERN.matcher(key_value_pair[1]).replaceAll("%$1s"); |
118 | | - out.put(key, value); |
119 | | - } |
120 | | - } |
121 | | - } |
122 | | - return out; |
123 | | - } |
| 107 | + private static HashMap<String, String> loadLang(InputStream inputStream) throws IOException { |
| 108 | + HashMap<String, String> out = new HashMap<>(); |
| 109 | + if (inputStream == null) { |
| 110 | + return out; |
| 111 | + } |
| 112 | + for (String string : IOUtils.readLines(inputStream, StandardCharsets.UTF_8)) { |
| 113 | + if (!string.isEmpty() && string.charAt(0) != '#') { |
| 114 | + String[] key_value_pair = Iterables.toArray(SPLITTER.split(string), String.class); |
| 115 | + if (key_value_pair != null && key_value_pair.length == 2) { |
| 116 | + String key = key_value_pair[0]; |
| 117 | + String value = PATTERN.matcher(key_value_pair[1]).replaceAll("%$1s"); |
| 118 | + out.put(key, value); |
| 119 | + } |
| 120 | + } |
| 121 | + } |
| 122 | + return out; |
| 123 | + } |
124 | 124 | } |
0 commit comments