provide translation replacement
This commit is contained in:
parent
567a3385ff
commit
0030e77234
@ -7,6 +7,7 @@ import org.jetbrains.annotations.Nullable;
|
|||||||
* I18n translation with associated key path (full-key).
|
* I18n translation with associated key path (full-key).
|
||||||
* @author marhali
|
* @author marhali
|
||||||
*/
|
*/
|
||||||
|
@Deprecated // Replaced by Translation
|
||||||
public class KeyedTranslation {
|
public class KeyedTranslation {
|
||||||
|
|
||||||
private @NotNull KeyPath key;
|
private @NotNull KeyPath key;
|
||||||
|
@ -1,175 +1,54 @@
|
|||||||
package de.marhali.easyi18n.model.translation;
|
package de.marhali.easyi18n.model.translation;
|
||||||
|
|
||||||
import de.marhali.easyi18n.model.translation.variant.Plural;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.jetbrains.annotations.Nullable;
|
||||||
import de.marhali.easyi18n.model.translation.variant.ContextMap;
|
|
||||||
import de.marhali.easyi18n.model.translation.variant.LocaleMap;
|
/**
|
||||||
import de.marhali.easyi18n.model.translation.variant.PluralMap;
|
* Represents a translation with defined key and locale values.
|
||||||
|
*
|
||||||
import org.jetbrains.annotations.NotNull;
|
* @author marhali
|
||||||
import org.jetbrains.annotations.Nullable;
|
*/
|
||||||
|
public class Translation {
|
||||||
/**
|
|
||||||
* Represents the set values behind a specific translation.
|
private final @NotNull KeyPath key;
|
||||||
* Consideration is given to context, pluralization and locale.
|
private @Nullable TranslationValue value;
|
||||||
* <br />
|
|
||||||
* Data structure can be imagined like a layered map of: context => plural => locale
|
/**
|
||||||
*
|
* Constructs a new translation instance.
|
||||||
* @author marhali
|
* @param key Absolute key path
|
||||||
*/
|
* @param value Values to set - nullable to indicate removal
|
||||||
public class Translation {
|
*/
|
||||||
|
public Translation(@NotNull KeyPath key, @Nullable TranslationValue value) {
|
||||||
private @Nullable String description;
|
this.key = key;
|
||||||
private @NotNull ContextMap contexts;
|
this.value = value;
|
||||||
private @Nullable Object misc;
|
}
|
||||||
|
|
||||||
public Translation(@Nullable String description, @NotNull ContextMap contexts, @Nullable Object misc) {
|
/**
|
||||||
this.description = description;
|
* @return Absolute key path
|
||||||
this.contexts = contexts;
|
*/
|
||||||
this.misc = misc;
|
public @NotNull KeyPath getKey() {
|
||||||
}
|
return key;
|
||||||
|
}
|
||||||
public Translation() {
|
|
||||||
this(null, new ContextMap(), null);
|
/**
|
||||||
}
|
* @return values - nullable to indicate removal
|
||||||
|
*/
|
||||||
/**
|
public @Nullable TranslationValue getValue() {
|
||||||
* Retrieve additional description for this translation
|
return value;
|
||||||
* @return Description
|
}
|
||||||
*/
|
|
||||||
public @Nullable String getDescription() {
|
/**
|
||||||
return description;
|
* @param value Values to set - nullable to indicate removal
|
||||||
}
|
*/
|
||||||
|
public void setValue(@Nullable TranslationValue value) {
|
||||||
/**
|
this.value = value;
|
||||||
* Override or set description for this translation
|
}
|
||||||
* @param description Description
|
|
||||||
*/
|
@Override
|
||||||
public void setDescription(@Nullable String description) {
|
public String toString() {
|
||||||
this.description = description;
|
return "Translation{" +
|
||||||
}
|
"key=" + key +
|
||||||
|
", value=" + value +
|
||||||
/**
|
'}';
|
||||||
* Retrieve all contexts for this translation
|
}
|
||||||
* @return Map of specified contexts
|
}
|
||||||
*/
|
|
||||||
public @NotNull ContextMap getContexts() {
|
|
||||||
return contexts;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Check whether a specific context has been set for this translation
|
|
||||||
* @param context Context to check
|
|
||||||
* @return True if context has been configured otherwise false
|
|
||||||
*/
|
|
||||||
public boolean hasContext(@NotNull String context) {
|
|
||||||
return contexts.containsKey(context);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Retrieve all plurals for a specific context.
|
|
||||||
* @param context Context to apply
|
|
||||||
* @return Map of specified plurals
|
|
||||||
*/
|
|
||||||
public @Nullable PluralMap getPlurals(@NotNull String context) {
|
|
||||||
return contexts.get(context);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Retrieve all locale translations for a specific context & pluralization
|
|
||||||
* @param context Context to apply
|
|
||||||
* @param plural Pluralization to apply
|
|
||||||
* @return Map of specified locales
|
|
||||||
*/
|
|
||||||
public @Nullable LocaleMap getLocales(@NotNull String context, @NotNull Plural plural) {
|
|
||||||
return contexts.getOrDefault(context, new PluralMap()).get(plural);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Retrieve a specific locale translation for a specific context, pluralization and locale
|
|
||||||
* @param context Context to apply
|
|
||||||
* @param plural Pluralization to apply
|
|
||||||
* @param locale Locale to apply
|
|
||||||
* @return Translated locale value for the specified variant
|
|
||||||
*/
|
|
||||||
public @Nullable String getValue(@NotNull String context, @NotNull Plural plural, @NotNull String locale) {
|
|
||||||
return contexts.getOrDefault(context, new PluralMap()).getOrDefault(plural, new LocaleMap()).get(locale);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Override or set context map.
|
|
||||||
* @param contexts New contexts
|
|
||||||
*/
|
|
||||||
public void set(@NotNull ContextMap contexts) {
|
|
||||||
this.contexts = contexts;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Override or set a specific context
|
|
||||||
* @param context Context to use
|
|
||||||
* @param plurals New plurals map
|
|
||||||
*/
|
|
||||||
public void set(@NotNull String context, @NotNull PluralMap plurals) {
|
|
||||||
contexts.put(context, plurals);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Override or set locales for a specific context & pluralization
|
|
||||||
* @param context Context to use
|
|
||||||
* @param plural Pluralization to use
|
|
||||||
* @param locales New locales map
|
|
||||||
*/
|
|
||||||
public void set(@NotNull String context, @NotNull Plural plural, @NotNull LocaleMap locales) {
|
|
||||||
PluralMap plurals = getPlurals(context);
|
|
||||||
|
|
||||||
if(plurals == null) {
|
|
||||||
plurals = new PluralMap();
|
|
||||||
}
|
|
||||||
|
|
||||||
plurals.put(plural, locales);
|
|
||||||
set(context, plurals);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Override or update a specific translation variant
|
|
||||||
* @param context Context to use
|
|
||||||
* @param plural Pluralization to use
|
|
||||||
* @param locale Locale to use
|
|
||||||
* @param value New value to set
|
|
||||||
*/
|
|
||||||
public void set(@NotNull String context, @NotNull Plural plural, @NotNull String locale, @NotNull String value) {
|
|
||||||
LocaleMap locales = getLocales(context, plural);
|
|
||||||
|
|
||||||
if(locales == null) {
|
|
||||||
locales = new LocaleMap();
|
|
||||||
}
|
|
||||||
|
|
||||||
locales.put(locale, value);
|
|
||||||
set(context, plural, locales);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* I18n support data
|
|
||||||
* @return Data
|
|
||||||
*/
|
|
||||||
public @Nullable Object getMisc() {
|
|
||||||
return misc;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Set or update I18n support data
|
|
||||||
* @param misc New Data
|
|
||||||
*/
|
|
||||||
public void setMisc(@Nullable Object misc) {
|
|
||||||
this.misc = misc;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String toString() {
|
|
||||||
return "Translation{" +
|
|
||||||
"description='" + description + '\'' +
|
|
||||||
", contexts=" + contexts +
|
|
||||||
", misc=" + misc +
|
|
||||||
'}';
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
x
Reference in New Issue
Block a user