MCPcopy Create free account
hub / github.com/ViaVersion/ViaProxy / I18n

Class I18n

src/main/java/net/raphimc/viaproxy/ui/I18n.java:30–119  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

28import java.util.stream.Collectors;
29
30public class I18n {
31
32 private static final String DEFAULT_LOCALE = "en_US";
33 private static Map<String, Properties> LOCALES = new LinkedHashMap<>();
34 private static String currentLocale;
35
36 static {
37 if (ViaProxy.getSaveManager() == null) {
38 throw new IllegalStateException("ViaProxy is not yet initialized");
39 }
40
41 try {
42 for (Map.Entry<Path, byte[]> entry : FileSystemUtil.getFilesInDirectory("assets/viaproxy/language").entrySet()) {
43 final Properties properties = new Properties();
44 properties.load(new InputStreamReader(new ByteArrayInputStream(entry.getValue()), StandardCharsets.UTF_8));
45 if (properties.isEmpty()) continue;
46 LOCALES.put(entry.getKey().getFileName().toString().replace(".properties", ""), properties);
47 }
48 } catch (Throwable e) {
49 throw new RuntimeException("Failed to load translations", e);
50 }
51 LOCALES = LOCALES.entrySet().stream()
52 .sorted(Comparator.comparing(e -> e.getValue().getProperty("language.name")))
53 .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (oldValue, newValue) -> newValue, LinkedHashMap::new));
54
55 currentLocale = ViaProxy.getSaveManager().uiSave.get("locale");
56 if (currentLocale == null || !LOCALES.containsKey(currentLocale)) {
57 final String systemLocale = Locale.getDefault().getLanguage() + '_' + Locale.getDefault().getCountry();
58 if (LOCALES.containsKey(systemLocale)) {
59 currentLocale = systemLocale;
60 } else {
61 for (Map.Entry<String, Properties> entry : LOCALES.entrySet()) {
62 if (entry.getKey().startsWith(Locale.getDefault().getLanguage() + '_')) {
63 currentLocale = entry.getKey();
64 break;
65 }
66 }
67 }
68 }
69
70 final int totalTranslation = LOCALES.get(DEFAULT_LOCALE).size();
71 for (Properties properties : LOCALES.values()) {
72 final int translated = properties.size();
73 final float percentage = (float) translated / totalTranslation * 100;
74 properties.put("language.completion", (int) Math.floor(percentage) + "%");
75 }
76 }
77
78 public static String get(final String key) {
79 return getSpecific(currentLocale, key);
80 }
81
82 public static String get(final String key, String... args) {
83 return String.format(getSpecific(currentLocale, key), (Object[]) args);
84 }
85
86 public static String getSpecific(final String locale, final String key) {
87 Properties properties = LOCALES.get(locale);

Callers

nothing calls this directly

Calls 7

getSaveManagerMethod · 0.95
isEmptyMethod · 0.80
putMethod · 0.80
replaceMethod · 0.80
loadMethod · 0.45
toStringMethod · 0.45
getMethod · 0.45

Tested by

no test coverage detected