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
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ java {

allprojects {
group = "org.densy.polyglot"
version = "1.1.0-SNAPSHOT"
version = "1.1.1-SNAPSHOT"
}

subprojects {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.densy.polyglot.api.language.LanguageStandard;
import org.densy.polyglot.api.provider.TranslationProvider;
import org.densy.polyglot.core.BaseTranslation;
import org.densy.polyglot.core.language.LocaleLanguageStandard;
import org.densy.polyglot.core.language.SimpleLanguageStandard;
import org.densy.polyglot.core.parameter.KeyedTranslationParameters;
import org.densy.polyglot.core.provider.EmptyProvider;
Expand All @@ -26,7 +27,7 @@ public class BaseTranslationContext implements TranslationContext {
public BaseTranslationContext() {
this.globalTranslations = new HashMap<>();
this.globalParameters = new KeyedTranslationParameters();
this.languageStandard = new SimpleLanguageStandard();
this.languageStandard = new LocaleLanguageStandard();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package org.densy.polyglot.core.language;

import org.densy.polyglot.api.language.Language;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import org.densy.polyglot.api.language.Language;

import java.util.Locale;
import java.util.Objects;

/**
* Base implementation of the Language interface.
Expand Down Expand Up @@ -59,11 +62,43 @@ public boolean isCompatibleWith(Language other) {
* @return BaseLanguage class
*/
public static BaseLanguage parseLanguage(String languageCode) {
Objects.requireNonNull(languageCode, "language code must not be null");
if (languageCode.contains("_")) {
String[] parts = languageCode.split("_");
return new BaseLanguage(parts[0], parts[1]);
} else {
} else {
return new BaseLanguage(languageCode);
}
}

/**
* Parses the language from locale.
*
* @param locale the locale
* @return BaseLanguage class
*/
public static BaseLanguage parseLocale(Locale locale) {
Objects.requireNonNull(locale, "locale must not be null");

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

if (language.isEmpty() && country.isEmpty()) {
throw new IllegalArgumentException("Locale must have at least language or country");
}

if (!country.isEmpty()) {
return new BaseLanguage(language, country);
}
return new BaseLanguage(language);
}

/**
* Gets the default system language.
*
* @return BaseLanguage class.
*/
public static BaseLanguage getDefault() {
return parseLocale(Locale.getDefault());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

/**
* Locale language standard implementation.
* Supports format: "ru_RU", "en_US", "en_GB" (language_COUNTRY).
* Supports format: "en_US", "en_GB", "ru_RU" (language_COUNTRY).
*/
public class LocaleLanguageStandard implements LanguageStandard {
private static final Pattern PATTERN = Pattern.compile("^([a-z]{2})_([A-Z]{2})$");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

/**
* Simple language standard implementation.
* Supports format: "eng", "rus", "ukr" (2-3 letter language codes).
* Supports format: "eng", "rus", "ukr" (ISO 639-1, 2-3 letter language codes).
*/
public class SimpleLanguageStandard implements LanguageStandard {
private static final Pattern PATTERN = Pattern.compile("^[a-z]{2,3}$");
Expand Down