Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,30 @@
import java.util.HashMap;
import java.util.Map;

/**
* Language mapping strategy builder.
*/
public final class LanguageMappingBuilder {

private final Map<Language, Language> mappings = new HashMap<>();

/**
* Sets the fallback language for a missing language.
*
* @param missingLanguage missing language
* @param mappedLanguage fallback language
* @return the builder itself
*/
public LanguageMappingBuilder put(Language missingLanguage, Language mappedLanguage) {
this.mappings.put(missingLanguage, mappedLanguage);
return this;
}

/**
* Builds language strategy.
*
* @return LanguageStrategy
*/
public LanguageStrategy build() {
return LanguageStrategy.mappingsOf(mappings);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public static BaseLanguage parseLanguage(String languageCode) {
public static BaseLanguage parseLocale(Locale locale) {
Objects.requireNonNull(locale, "locale must not be null");

String language = locale.getISO3Country();
String language = locale.getLanguage();
String country = locale.getCountry();

if (language.isEmpty() && country.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.densy.polyglot.api.language.Language;
import org.densy.polyglot.api.language.LanguageStandard;
import org.densy.polyglot.api.provider.TranslationProvider;
import org.densy.polyglot.core.util.ProviderUtils;

import java.io.File;
import java.io.FileReader;
Expand Down Expand Up @@ -48,10 +49,10 @@ private void loadTranslationsFromFolder() {

if (languageStandard.matches(languageString)) {
Language language = languageStandard.parseLanguage(languageString);
Map<String, Object> data = gson.fromJson(new FileReader(file), new TypeToken<Map<String, Object>>() {
Map<String, Object> translations = gson.fromJson(new FileReader(file), new TypeToken<Map<String, Object>>() {
}.getType());
Map<String, String> flattenedTranslations = flattenMap(data, "");
translations.put(language, flattenedTranslations);
Map<String, String> flattenedTranslations = ProviderUtils.flattenMap(translations, "");
this.translations.put(language, flattenedTranslations);
}
} catch (IOException e) {
throw new RuntimeException("Error loading translation file: " + file.getName(), e);
Expand All @@ -61,24 +62,6 @@ private void loadTranslationsFromFolder() {
}
}

@SuppressWarnings("unchecked")
private Map<String, String> flattenMap(Map<String, Object> map, String prefix) {
Map<String, String> flattened = new HashMap<>();

for (Map.Entry<String, Object> entry : map.entrySet()) {
String key = prefix.isEmpty() ? entry.getKey() : prefix + "." + entry.getKey();
Object value = entry.getValue();

if (value instanceof Map) {
flattened.putAll(flattenMap((Map<String, Object>) value, key));
} else {
flattened.put(key, String.valueOf(value));
}
}

return flattened;
}

@Override
public Map<Language, Map<String, String>> getTranslations() {
return new HashMap<>(translations);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.densy.polyglot.api.language.Language;
import org.densy.polyglot.api.language.LanguageStandard;
import org.densy.polyglot.api.provider.TranslationProvider;
import org.densy.polyglot.core.util.ProviderUtils;
import org.yaml.snakeyaml.Yaml;

import java.io.File;
Expand Down Expand Up @@ -47,9 +48,9 @@ private void loadTranslationsFromFolder() {

if (languageStandard.matches(languageString)) {
Language language = languageStandard.parseLanguage(languageString);
Map<String, Object> data = yaml.load(new FileInputStream(file));
Map<String, String> flattenedTranslations = flattenMap(data, "");
translations.put(language, flattenedTranslations);
Map<String, Object> translations = yaml.load(new FileInputStream(file));
Map<String, String> flattenedTranslations = ProviderUtils.flattenMap(translations, "");
this.translations.put(language, flattenedTranslations);
}
} catch (IOException e) {
throw new RuntimeException("Error loading translation file: " + file.getName(), e);
Expand All @@ -59,24 +60,6 @@ private void loadTranslationsFromFolder() {
}
}

@SuppressWarnings("unchecked")
private Map<String, String> flattenMap(Map<String, Object> map, String prefix) {
Map<String, String> flattened = new HashMap<>();

for (Map.Entry<String, Object> entry : map.entrySet()) {
String key = prefix.isEmpty() ? entry.getKey() : prefix + "." + entry.getKey();
Object value = entry.getValue();

if (value instanceof Map) {
flattened.putAll(flattenMap((Map<String, Object>) value, key));
} else {
flattened.put(key, String.valueOf(value));
}
}

return flattened;
}

@Override
public Map<Language, Map<String, String>> getTranslations() {
return new HashMap<>(translations);
Expand Down
38 changes: 38 additions & 0 deletions core/src/main/java/org/densy/polyglot/core/util/ProviderUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package org.densy.polyglot.core.util;

import lombok.experimental.UtilityClass;

import java.util.HashMap;
import java.util.Map;

/**
* General provider utility.
*/
@UtilityClass
public class ProviderUtils {

/**
* Flattens a nested map into a single-level map.
*
* @param map source map
* @param prefix key prefix
* @return flattened map
*/
@SuppressWarnings("unchecked")
public Map<String, String> flattenMap(Map<String, Object> map, String prefix) {
Map<String, String> flattened = new HashMap<>();

for (Map.Entry<String, Object> entry : map.entrySet()) {
String key = prefix.isEmpty() ? entry.getKey() : prefix + "." + entry.getKey();
Object value = entry.getValue();

if (value instanceof Map) {
flattened.putAll(flattenMap((Map<String, Object>) value, key));
} else {
flattened.put(key, String.valueOf(value));
}
}

return flattened;
}
}