Search in sources :

Example 1 with Lang

use of com.djrapitops.plan.settings.locale.lang.Lang in project Plan by plan-player-analytics.

the class LocaleFileReader method loadLegacy.

/**
 * Used to migrate old files to new format.
 */
public Locale loadLegacy(LangCode code) throws IOException {
    final List<String> lines = resource.asLines();
    Locale locale = new Locale(code);
    Map<String, Lang> identifiers = LocaleSystem.getIdentifiers();
    lines.forEach(line -> {
        String[] split = line.split(" \\|\\| ");
        if (split.length == 2) {
            String identifier = split[0].trim();
            Lang msg = identifiers.get(identifier);
            if (msg != null) {
                locale.put(msg, new Message(split[1]));
            }
        }
    });
    return locale;
}
Also used : Lang(com.djrapitops.plan.settings.locale.lang.Lang)

Example 2 with Lang

use of com.djrapitops.plan.settings.locale.lang.Lang in project Plan by plan-player-analytics.

the class Locale method replaceLanguageInHtml.

public String replaceLanguageInHtml(String from) {
    if (isEmpty()) {
        return from;
    }
    Matcher scriptMatcher = FIND_SCRIPT.matcher(from);
    List<String> foundScripts = new ArrayList<>();
    while (scriptMatcher.find()) {
        foundScripts.add(scriptMatcher.toMatchResult().group(0));
    }
    TranslatedString translated = new TranslatedString(from);
    Arrays.stream(HtmlLang.values()).sorted((one, two) -> Integer.compare(two.getIdentifier().length(), one.getIdentifier().length())).forEach(lang -> getNonDefault(lang).ifPresent(replacement -> translated.translate(lang.getDefault(), replacement.toString())));
    StringBuilder complete = new StringBuilder(translated.length());
    String[] parts = FIND_SCRIPT.split(translated.toString());
    for (int i = 0; i < parts.length; i++) {
        complete.append(parts[i]);
        if (i < parts.length - 1) {
            complete.append(replaceLanguageInJavascript(foundScripts.get(i)));
        }
    }
    return complete.toString();
}
Also used : HtmlLang(com.djrapitops.plan.settings.locale.lang.HtmlLang) java.util(java.util) Matcher(java.util.regex.Matcher) Lang(com.djrapitops.plan.settings.locale.lang.Lang) JSLang(com.djrapitops.plan.settings.locale.lang.JSLang) FileResource(com.djrapitops.plan.storage.file.FileResource) IOException(java.io.IOException) Pattern(java.util.regex.Pattern) File(java.io.File) PlanFiles(com.djrapitops.plan.storage.file.PlanFiles) Serializable(java.io.Serializable) Matcher(java.util.regex.Matcher)

Example 3 with Lang

use of com.djrapitops.plan.settings.locale.lang.Lang in project Plan by plan-player-analytics.

the class Locale method replaceLanguageInJavascript.

public String replaceLanguageInJavascript(String from) {
    if (isEmpty()) {
        return from;
    }
    TranslatedString translated = new TranslatedString(from);
    Arrays.stream(JSLang.values()).sorted((one, two) -> Integer.compare(two.getIdentifier().length(), one.getIdentifier().length())).forEach(lang -> getNonDefault(lang).ifPresent(replacement -> translated.translate(lang.getDefault(), replacement.toString())));
    for (Lang extra : new Lang[] { HtmlLang.UNIT_NO_DATA, HtmlLang.TITLE_WORLD_PLAYTIME, // HtmlLang.LABEL_BANNED,
    HtmlLang.SIDE_SESSIONS, HtmlLang.LABEL_PLAYTIME, HtmlLang.LABEL_AFK_TIME, HtmlLang.LABEL_LONGEST_SESSION, HtmlLang.LABEL_SESSION_MEDIAN, HtmlLang.LABEL_PLAYER_KILLS, HtmlLang.LABEL_MOB_KILLS, HtmlLang.LABEL_DEATHS, HtmlLang.LABEL_PLAYERS_ONLINE, HtmlLang.LABEL_REGISTERED, HtmlLang.TITLE_SERVER, HtmlLang.TITLE_LENGTH, HtmlLang.TITLE_AVG_PING, HtmlLang.TITLE_BEST_PING, HtmlLang.TITLE_WORST_PING, HtmlLang.LABEL_FREE_DISK_SPACE, HtmlLang.LABEL_NEW_PLAYERS, HtmlLang.LABEL_UNIQUE_PLAYERS, HtmlLang.LABEL_ACTIVE_PLAYTIME, HtmlLang.LABEL_AFK_TIME, HtmlLang.LABEL_AVG_SESSION_LENGTH, HtmlLang.LABEL_AVG_PLAYTIME, HtmlLang.LABEL_AVG_ACTIVE_PLAYTIME, HtmlLang.LABEL_AVG_AFK_TIME, HtmlLang.LABEL_AVG_PLAYTIME, HtmlLang.SIDE_GEOLOCATIONS, HtmlLang.LABEL_PER_PLAYER, HtmlLang.TITLE_JOIN_ADDRESSES }) {
        getNonDefault(extra).ifPresent(replacement -> translated.translate(extra.getDefault(), replacement.toString()));
    }
    return translated.toString();
}
Also used : HtmlLang(com.djrapitops.plan.settings.locale.lang.HtmlLang) java.util(java.util) Matcher(java.util.regex.Matcher) Lang(com.djrapitops.plan.settings.locale.lang.Lang) JSLang(com.djrapitops.plan.settings.locale.lang.JSLang) FileResource(com.djrapitops.plan.storage.file.FileResource) IOException(java.io.IOException) Pattern(java.util.regex.Pattern) File(java.io.File) PlanFiles(com.djrapitops.plan.storage.file.PlanFiles) Serializable(java.io.Serializable) HtmlLang(com.djrapitops.plan.settings.locale.lang.HtmlLang) Lang(com.djrapitops.plan.settings.locale.lang.Lang) JSLang(com.djrapitops.plan.settings.locale.lang.JSLang)

Example 4 with Lang

use of com.djrapitops.plan.settings.locale.lang.Lang in project Plan by plan-player-analytics.

the class LocaleFileReader method load.

public Locale load(LangCode code) throws IOException {
    try (ConfigReader reader = new ConfigReader(resource.asInputStream())) {
        Config config = reader.read();
        Locale locale = new Locale(code);
        Map<String, Lang> keys = LocaleSystem.getKeys();
        config.getConfigPaths().forEach(key -> {
            Lang msg = keys.get(key);
            if (msg != null) {
                locale.put(msg, new Message(config.getString(key)));
            }
        });
        return locale;
    }
}
Also used : Config(com.djrapitops.plan.settings.config.Config) ConfigReader(com.djrapitops.plan.settings.config.ConfigReader) Lang(com.djrapitops.plan.settings.locale.lang.Lang)

Example 5 with Lang

use of com.djrapitops.plan.settings.locale.lang.Lang in project Plan by plan-player-analytics.

the class ComparatorTest method localeEntryComparator.

@Test
void localeEntryComparator() {
    Map<Lang, Message> messageMap = new HashMap<>();
    messageMap.put(HelpLang.SERVERS, new Message(RandomData.randomString(10)));
    messageMap.put(HelpLang.SERVER, new Message(RandomData.randomString(10)));
    messageMap.put(HelpLang.DB_RESTORE, new Message(RandomData.randomString(10)));
    List<Lang> result = messageMap.entrySet().stream().sorted(new LocaleEntryComparator()).map(Map.Entry::getKey).collect(Collectors.toList());
    List<Lang> expected = Arrays.asList(HelpLang.DB_RESTORE, HelpLang.SERVER, HelpLang.SERVERS);
    assertEquals(expected, result);
}
Also used : Message(com.djrapitops.plan.settings.locale.Message) HelpLang(com.djrapitops.plan.settings.locale.lang.HelpLang) Lang(com.djrapitops.plan.settings.locale.lang.Lang) Test(org.junit.jupiter.api.Test)

Aggregations

Lang (com.djrapitops.plan.settings.locale.lang.Lang)5 HtmlLang (com.djrapitops.plan.settings.locale.lang.HtmlLang)2 JSLang (com.djrapitops.plan.settings.locale.lang.JSLang)2 FileResource (com.djrapitops.plan.storage.file.FileResource)2 PlanFiles (com.djrapitops.plan.storage.file.PlanFiles)2 File (java.io.File)2 IOException (java.io.IOException)2 Serializable (java.io.Serializable)2 java.util (java.util)2 Matcher (java.util.regex.Matcher)2 Pattern (java.util.regex.Pattern)2 Config (com.djrapitops.plan.settings.config.Config)1 ConfigReader (com.djrapitops.plan.settings.config.ConfigReader)1 Message (com.djrapitops.plan.settings.locale.Message)1 HelpLang (com.djrapitops.plan.settings.locale.lang.HelpLang)1 Test (org.junit.jupiter.api.Test)1