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
18 changes: 15 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Polyglot
Polyglot is an advanced and multifunctional library for localizing strings for Luminia Development projects.
Polyglot is an advanced and multifunctional library for localizing strings.

## Example of usage
```java
Expand All @@ -26,12 +26,24 @@ Translation translation = context.createTranslation(new YamlFileProvider(
new File("lang"), context.getLanguageStandard()
));
translation.setDefaultLanguage(SimpleLanguage.ENG); // Default language
translation.setFallbackStrategy(key -> key + "-fallback"); // Fallback strategy for missing localizations

// Strategy for determining the language when the requested language is not available.
// For example: return Russian when Ukrainian is not available, instead of the default English.
translation.setLanguageStrategy(LanguageStrategy.mappingsBuilder()
.put(SimpleLanguage.UKR, SimpleLanguage.RUS)
.build());
// We can also set the default language using `LanguageStrategy.defaultResult(SimpleLanguage.ENG)`.

// Fallback strategy for missing localizations
// You can also do the same thing this way: `translation.setFallbackStrategy(FallbackStrategy.suffix(“-fallback”));`.
// Or you can use `FallbackStrategy.keyToKey()` if you want the result to be the key of the missing locale.
translation.setFallbackStrategy(key -> key + "-fallback");

translation.addTranslation(SimpleLanguage.ENG, "local.translation", "Local message with param [0]"); // Adding local translation

// Translating the messages
String local = translation.translate(SimpleLanguage.RUS, "local.translation", 1);
String global = translation.translate(SimpleLanguage.RUS, "local.translation", new KeyedTrParameters().put("local", "Parameter"));
String global = translation.translate(SimpleLanguage.RUS, "global.translation", new KeyedTrParameters().put("local", "Parameter"));

System.out.println("Translated local message: " + local);
System.out.println("Translated global message: " + global);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.luminiadev.polyglot.api.parameter.TrParameters;
import com.luminiadev.polyglot.api.parameter.formatter.TrParameterFormatter;
import com.luminiadev.polyglot.api.util.FallbackStrategy;
import com.luminiadev.polyglot.api.util.LanguageStrategy;

import java.util.Set;

Expand All @@ -19,6 +20,8 @@ public interface Translation {

void setDefaultLanguage(Language language);

void setLanguageStrategy(LanguageStrategy languageStrategy);

void setFallbackStrategy(FallbackStrategy fallbackStrategy);

void addTranslation(Language language, String key, String value);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.luminiadev.polyglot.api.util;

import com.luminiadev.polyglot.api.language.Language;

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

public final class LanguageMappingBuilder {

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

public LanguageMappingBuilder put(Language missingLanguage, Language mappedLanguage) {
this.mappings.put(missingLanguage, mappedLanguage);
return this;
}

public LanguageStrategy build() {
return LanguageStrategy.mappingsOf(mappings);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.luminiadev.polyglot.api.util;

import com.luminiadev.polyglot.api.language.Language;

import java.util.Map;

/**
* Missing language strategy interface. Defines how to resolve missing language.
*/
@FunctionalInterface
public interface LanguageStrategy {

/**
* Resolves a value for the given missing language.
*
* @param missingLanguage the missing language
* @return resolved value
*/
Language get(Language missingLanguage);

/**
* Returns a default language.
*
* @param defaultLanguage default language
* @return strategy returning the specified language
*/
static LanguageStrategy defaultResult(Language defaultLanguage) {
return missingLanguage -> defaultLanguage;
}

/**
* Returns a strategy based on passed mappings.
*
* @param mappings the map with language mappings
* @return strategy based on passed mappings
*/
static LanguageStrategy mappingsOf(Map<Language, Language> mappings) {
return missingLanguage -> mappings.getOrDefault(missingLanguage, null);
}

/**
* Crates the new language mappings builder.
*
* @return crated LanguageMappingBuilder
*/
static LanguageMappingBuilder mappingsBuilder() {
return new LanguageMappingBuilder();
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.luminiadev.polyglot.api.parameter.TrParameters;
import com.luminiadev.polyglot.api.parameter.formatter.TrParameterFormatter;
import com.luminiadev.polyglot.api.provider.TranslationProvider;
import com.luminiadev.polyglot.api.util.LanguageStrategy;
import com.luminiadev.polyglot.core.parameter.KeyedTrParameters;
import com.luminiadev.polyglot.core.parameter.SimpleTrParameters;
import com.luminiadev.polyglot.core.parameter.formatter.BraceKeyedParameterFormatter;
Expand All @@ -27,6 +28,7 @@ public class BaseTranslation implements Translation {
private final Map<Class<? extends TrParameters>, TrParameterFormatter> formatters;

private Language defaultLanguage;
private LanguageStrategy languageStrategy;
private FallbackStrategy fallbackStrategy;

public BaseTranslation(TranslationContext context, TranslationProvider provider) {
Expand Down Expand Up @@ -70,10 +72,23 @@ private Language resolveLanguage(Language requestedLanguage) {
if (this.isLanguageAvailable(requestedLanguage)) {
return requestedLanguage;
}

Language candidateLanguage = requestedLanguage;
while (true) {
candidateLanguage = this.languageStrategy != null ? this.languageStrategy.get(candidateLanguage) : null;
if (candidateLanguage == null) {
break;
}
if (this.isLanguageAvailable(candidateLanguage)) {
return candidateLanguage;
}
}

Language defaultLanguage = this.defaultLanguage != null ? this.defaultLanguage : context.getDefaultLanguage();
if (this.isLanguageAvailable(defaultLanguage)) {
return defaultLanguage;
}

return requestedLanguage;
}

Expand Down Expand Up @@ -163,6 +178,11 @@ public void setDefaultLanguage(Language language) {
this.defaultLanguage = language;
}

@Override
public void setLanguageStrategy(LanguageStrategy languageStrategy) {
this.languageStrategy = languageStrategy;
}

@Override
public void setFallbackStrategy(FallbackStrategy fallbackStrategy) {
this.fallbackStrategy = fallbackStrategy;
Expand Down