Language.java 2.95 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package emu.grasscutter.utils;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import emu.grasscutter.Grasscutter;

import javax.annotation.Nullable;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

public final class Language {
    private final JsonObject languageData;
    private final Map<String, String> cachedTranslations = new HashMap<>();

    /**
     * Creates a language instance from a code.
     * @param langCode The language code.
     * @return A language instance.
     */
    public static Language getLanguage(String langCode) {
        return new Language(langCode + ".json");
    }

    /**
     * Returns the translated value from the key while substituting arguments.
     * @param key The key of the translated value to return.
     * @param args The arguments to substitute.
     * @return A translated value with arguments substituted.
     */
    public static String translate(String key, Object... args) {
KingRainbow44's avatar
KingRainbow44 committed
32
33
34
35
36
37
38
        String translated = Grasscutter.getLanguage().get(key);
        try {
            return translated.formatted(args);
        } catch (Exception exception) {
            Grasscutter.getLogger().error("Failed to format string: " + key, exception);
            return translated;
        }
39
40
41
42
43
44
45
46
47
48
    }

    /**
     * Reads a file and creates a language instance.
     * @param fileName The name of the language file.
     */
    private Language(String fileName) {
        @Nullable JsonObject languageData = null;
        
        try {
KingRainbow44's avatar
KingRainbow44 committed
49
            InputStream file = Grasscutter.class.getResourceAsStream("/languages/" + fileName);
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
            languageData = Grasscutter.getGsonFactory().fromJson(Utils.readFromInputStream(file), JsonObject.class);
        } catch (Exception exception) {
            Grasscutter.getLogger().error("Failed to load language file: " + fileName, exception);
        } this.languageData = languageData;
    }

    /**
     * Returns the value (as a string) from a nested key.
     * @param key The key to look for.
     * @return The value (as a string) from a nested key.
     */
    public String get(String key) {
        if(this.cachedTranslations.containsKey(key)) {
            return this.cachedTranslations.get(key);
        }
        
        String[] keys = key.split("\\.");
        JsonObject object = this.languageData;

        int index = 0;
70
        String result = "This value does not exist. Please report this to the Discord: " + key;
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88

        while (true) {
            if(index == keys.length) break;
            
            String currentKey = keys[index++];
            if(object.has(currentKey)) {
                JsonElement element = object.get(currentKey);
                if(element.isJsonObject())
                    object = element.getAsJsonObject();
                else {
                    result = element.getAsString(); break;
                }
            } else break;
        }
        
        this.cachedTranslations.put(key, result); return result;
    }
}