2021-06-29 09:41:08 +00:00
|
|
|
package the.bytecode.club.bytecodeviewer.translation;
|
|
|
|
|
|
|
|
import com.google.gson.reflect.TypeToken;
|
|
|
|
import org.apache.commons.collections4.map.HashedMap;
|
|
|
|
import org.apache.commons.collections4.map.LinkedMap;
|
|
|
|
import org.apache.commons.io.IOUtils;
|
|
|
|
import the.bytecode.club.bytecodeviewer.BytecodeViewer;
|
|
|
|
import the.bytecode.club.bytecodeviewer.Resources;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.HashSet;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* All of the supported languages
|
|
|
|
*
|
|
|
|
* @author Konloch
|
|
|
|
* @since 6/28/2021
|
|
|
|
*/
|
|
|
|
public enum Language
|
|
|
|
{
|
|
|
|
ENGLISH("/translations/english.json", "English", "en"),
|
|
|
|
MANDARIN("/translations/mandarin.json", "普通话", "zh_cn", "zh"),
|
2021-06-30 19:03:39 +00:00
|
|
|
//HINDI("/translations/hindi.json", "हिंदी", "hi"),
|
2021-06-30 19:16:44 +00:00
|
|
|
SPANISH("/translations/spanish.json", "Español", "es"),
|
2021-06-30 19:21:18 +00:00
|
|
|
FRENCH("/translations/french.json", "Français", "fr"),
|
2021-06-30 19:03:39 +00:00
|
|
|
ARABIC("/translations/arabic.json", "عربى", "ab"),
|
2021-06-30 19:05:53 +00:00
|
|
|
RUSSIAN("/translations/russian.json", "русский", "ru"),
|
2021-06-29 21:50:26 +00:00
|
|
|
GERMAN("/translations/german.json", "Deutsch", "de"),
|
2021-06-30 19:36:16 +00:00
|
|
|
JAPANESE("/translations/japanese.json", "日本語", "ja"),
|
2021-06-30 19:48:26 +00:00
|
|
|
PORTUGUESE("/translations/portuguese.json", "Português", "pt"),
|
2021-06-29 09:41:08 +00:00
|
|
|
;
|
|
|
|
|
|
|
|
private static final HashedMap<String, Language> languageCodeLookup;
|
|
|
|
|
|
|
|
static
|
|
|
|
{
|
|
|
|
languageCodeLookup = new HashedMap<>();
|
|
|
|
for(Language l : values())
|
|
|
|
l.languageCode.forEach((langCode)->
|
|
|
|
languageCodeLookup.put(langCode, l));
|
|
|
|
}
|
|
|
|
|
|
|
|
private final String resourcePath;
|
|
|
|
private final String readableName;
|
|
|
|
private final HashSet<String> languageCode;
|
|
|
|
|
|
|
|
Language(String resourcePath, String readableName, String... languageCodes)
|
|
|
|
{
|
|
|
|
this.resourcePath = resourcePath;
|
|
|
|
this.languageCode = new HashSet<>(Arrays.asList(languageCodes));
|
|
|
|
this.readableName = readableName;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void loadLanguage() throws IOException
|
|
|
|
{
|
2021-06-29 16:11:26 +00:00
|
|
|
printMissingLanguageKeys();
|
|
|
|
|
2021-06-29 09:41:08 +00:00
|
|
|
HashMap<String, String> translationMap = BytecodeViewer.gson.fromJson(
|
|
|
|
IOUtils.toString(Resources.class.getResourceAsStream(resourcePath), StandardCharsets.UTF_8),
|
|
|
|
new TypeToken<HashMap<String, String>>(){}.getType());
|
|
|
|
|
|
|
|
for(Translation translation : Translation.values())
|
|
|
|
{
|
|
|
|
TranslatedComponent text = translation.getTranslatedComponent();
|
|
|
|
|
|
|
|
//skip translating if the language config is missing the translation key
|
|
|
|
if(!translationMap.containsKey(text.key))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
//update translation text value
|
|
|
|
text.value = translationMap.get(text.key);
|
|
|
|
|
|
|
|
//check if translation key has been assigned to a component,
|
|
|
|
//on fail print an error alerting the devs
|
|
|
|
if(translation.getTranslatedComponent().runOnUpdate.isEmpty())
|
|
|
|
{
|
|
|
|
System.err.println("Translation Reference " + translation.name() + " is missing component attachment, skipping...");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
//trigger translation event
|
|
|
|
translation.getTranslatedComponent().runOnUpdate.forEach(Runnable::run);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//TODO
|
|
|
|
// When adding new Translation Components:
|
|
|
|
// 1) start by adding the strings into the english.json
|
2021-06-29 16:11:26 +00:00
|
|
|
// 2) run this function to get the keys and add them into the Translation.java enum
|
2021-06-29 09:41:08 +00:00
|
|
|
// 3) replace the swing component (MainViewerGUI) with a translated component
|
|
|
|
// and reference the correct translation key
|
|
|
|
// 4) add the translation key to the rest of the translation files
|
2021-06-29 16:11:26 +00:00
|
|
|
public void printMissingLanguageKeys() throws IOException
|
2021-06-29 09:41:08 +00:00
|
|
|
{
|
|
|
|
if(this != ENGLISH)
|
|
|
|
return;
|
|
|
|
|
|
|
|
LinkedMap<String, String> translationMap = BytecodeViewer.gson.fromJson(
|
|
|
|
IOUtils.toString(Resources.class.getResourceAsStream(resourcePath), StandardCharsets.UTF_8),
|
|
|
|
new TypeToken<LinkedMap<String, String>>(){}.getType());
|
|
|
|
|
2021-06-29 16:11:26 +00:00
|
|
|
HashSet<String> existingKeys = new HashSet<>();
|
|
|
|
for(Translation t : Translation.values())
|
|
|
|
existingKeys.add(t.name());
|
|
|
|
|
2021-06-29 09:41:08 +00:00
|
|
|
for(String key : translationMap.keySet())
|
2021-06-29 16:11:26 +00:00
|
|
|
if(!existingKeys.contains(key) && !key.startsWith("TODO"))
|
|
|
|
System.err.println(key + ",");
|
2021-06-29 09:41:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public String getResourcePath()
|
|
|
|
{
|
|
|
|
return resourcePath;
|
|
|
|
}
|
|
|
|
|
|
|
|
public HashSet<String> getLanguageCode()
|
|
|
|
{
|
|
|
|
return languageCode;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getReadableName()
|
|
|
|
{
|
|
|
|
return readableName;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static HashedMap<String, Language> getLanguageCodeLookup()
|
|
|
|
{
|
|
|
|
return languageCodeLookup;
|
|
|
|
}
|
|
|
|
}
|