All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.bloodshura.ignitium.cfg.i18n.Language Maven / Gradle / Ivy

Go to download

A collection of configuration and serialization readers and writers, like JSON, internationalization (I18n), and CSV.

There is a newer version: 1.0.1
Show newest version
package com.github.bloodshura.ignitium.cfg.i18n;

import com.github.bloodshura.ignitium.collection.view.XArrayView;
import com.github.bloodshura.ignitium.collection.view.XView;
import com.github.bloodshura.ignitium.enumeration.Enumerations;
import com.github.bloodshura.ignitium.enumeration.ExtensibleEnum;
import com.github.bloodshura.ignitium.lang.Lazy;
import com.github.bloodshura.ignitium.lang.Nameable;
import com.github.bloodshura.ignitium.object.Base;
import com.github.bloodshura.ignitium.worker.StringWorker;

import javax.annotation.Nonnull;
import java.util.Locale;

public final class Language extends Base implements ExtensibleEnum, Nameable {
	public static final Language ARABIC_ALGERIA = new Language("ar-DZ", "Arabic Algeria");
	public static final Language ARABIC_BAHRAIN = new Language("ar-BH", "Arabic Bahrain");
	public static final Language ARABIC_IRAQ = new Language("ar-IQ", "Arabic Iraq");
	public static final Language ARABIC_JORDAN = new Language("ar-JO", "Arabic Jordan");
	public static final Language ARABIC_LEBANON = new Language("ar-LB", "Arabic Lebanon");
	public static final Language ARABIC_LYBIA = new Language("ar-LY", "Arabic Lybia");
	public static final Language ARABIC_MOROCCO = new Language("ar-MA", "Arabic Morocco");
	public static final Language ARABIC_OMAN = new Language("ar-OM", "Arabic Oman");
	public static final Language ARABIC_QATAR = new Language("ar-QA", "Arabic Qatar");
	public static final Language ARABIC_SAUDI_ARABIA = new Language("ar-SA", "Arabic Saudi Arabia");
	public static final Language ARABIC_TUNISIA = new Language("ar-TN", "Arabic Tunisia");
	public static final Language ARABIC_UAE = new Language("ar-AE", "Arabic Uae");
	public static final Language ARABIC_YEMEN = new Language("ar-YE", "Arabic Yemen");
	public static final Language BASQUE = new Language("eu", "Basque");
	public static final Language BRAZILIAN_PORTUGUESE = new Language("pt-BR", "Brazilian Portuguese");
	public static final Language CATALAN = new Language("ca", "Catalan");
	public static final Language CHINESE_CANTONESE = new Language("zh-yue", "Chinese Cantonese");
	public static final Language CHINESE_HONGKONG = new Language("zh-HK", "Chinese Hongkong");
	public static final Language CHINESE_SIMPLIFIED = new Language("zh-CN", "Chinese Simplified");
	public static final Language CHINESE_TRANDITIONAL = new Language("zh-TW", "Chinese Tranditional");
	public static final Language CZECH = new Language("cs", "Czech");
	public static final Language DUTCH = new Language("nl-NL", "Dutch");
	public static final Language ENGLISH_AUSTRALIA = new Language("en-AU", "English Australia");
	public static final Language ENGLISH_CANADA = new Language("en-CA", "English Canada");
	public static final Language ENGLISH_INDIA = new Language("en-IN", "English India");
	public static final Language ENGLISH_NEW_ZEALAND = new Language("en-NZ", "English New Zealand");
	public static final Language ENGLISH_SOUTH_AFRICA = new Language("en-ZA", "English South Africa");
	public static final Language ENGLISH_UK = new Language("en-GB", "English UK");
	public static final Language ENGLISH_US = new Language("en-US", "English US");
	public static final Language FINNISH = new Language("fi", "Finnish");
	public static final Language FRENCH = new Language("fr-FR", "French");
	public static final Language GALICIAN = new Language("gl", "Galician");
	public static final Language GERMAN = new Language("de-DE", "German");
	public static final Language HEBREW = new Language("he", "Hebrew");
	public static final Language HUNGARIAN = new Language("hu", "Hungarian");
	public static final Language ICELANDIC = new Language("is", "Icelandic");
	public static final Language INDONESIAN = new Language("id", "Indonesian");
	public static final Language ITALIAN = new Language("it-IT", "Italian");
	public static final Language JAPANESE = new Language("ja", "Japanese");
	public static final Language KOREAN = new Language("kr", "Korean");
	public static final Language LATIN = new Language("la", "Latin");
	public static final Language MALAYSIAN = new Language("ms-MY", "Malaysian");
	public static final Language NORWEGIAN = new Language("no-NO", "Norwegian");
	public static final Language PIG_LATIN = new Language("xx-pig", "Pig Latin");
	public static final Language POLISH = new Language("pl", "Polish");
	public static final Language PORTUGUESE = new Language("pt-PT", "Portuguese");
	public static final Language ROMANIAN = new Language("ro-RO", "Romanian");
	public static final Language RUSSIAN = new Language("ru", "Russian");
	public static final Language SERBIAN = new Language("sr-SP", "Serbian");
	public static final Language SLOVAK = new Language("sk", "Slovak");
	public static final Language SPANISH_ARGENTINA = new Language("es-AR", "Spanish Argentina");
	public static final Language SPANISH_BOLIVIA = new Language("es-BO", "Spanish Bolivia");
	public static final Language SPANISH_CHILE = new Language("es-CL", "Spanish Chile");
	public static final Language SPANISH_COLOMBIA = new Language("es-CO", "Spanish Colombia");
	public static final Language SPANISH_COSTA_RICA = new Language("es-CR", "Spanish Costa Rica");
	public static final Language SPANISH_DOMINICAN_REPUBLIC = new Language("es-DO", "Spanish Dominican Republic");
	public static final Language SPANISH_ECUADOR = new Language("es-EC", "Spanish Ecuador");
	public static final Language SPANISH_EL_SALVADOR = new Language("es-SV", "Spanish El Salvador");
	public static final Language SPANISH_GUATEMALA = new Language("es-GT", "Spanish Guatemala");
	public static final Language SPANISH_HONDURAS = new Language("es-HN", "Spanish Honduras");
	public static final Language SPANISH_MEXICO = new Language("es-MX", "Spanish Mexico");
	public static final Language SPANISH_NICARAGUA = new Language("es-NI", "Spanish Nicaragua");
	public static final Language SPANISH_PANAMA = new Language("es-PA", "Spanish Panama");
	public static final Language SPANISH_PARAGUAY = new Language("es-PY", "Spanish Paraguay");
	public static final Language SPANISH_PERU = new Language("es-PE", "Spanish Peru");
	public static final Language SPANISH_PUERTO_RICO = new Language("es-PR", "Spanish Puerto Rico");
	public static final Language SPANISH_SPAIN = new Language("es-ES", "Spanish Spain");
	public static final Language SPANISH_URUGUAY = new Language("es-UY", "Spanish Uruguay");
	public static final Language SPANISH_US = new Language("es-US", "Spanish Us");
	public static final Language SPANISH_VENEZUELA = new Language("es-VE", "Spanish Venezuela");
	public static final Language SWEDISH = new Language("sv-SE", "Swedish");
	public static final Language TURKISH = new Language("tr", "Turkish");
	public static final Language ZULU = new Language("zu", "Zulu");

	private final XView codes;
	private final Lazy locale;
	private final String name;

	// The language code must be in format ISO-639, optionally with ISO-3166
	public Language(@Nonnull String code, @Nonnull String name) {
		if (code.length() == 2) {
			if (StringWorker.containsOnly(code, Character::isLetter)) {
				this.codes = new XArrayView<>(code.toLowerCase() + '-' + code.toUpperCase(), code.toLowerCase(), code.toUpperCase());
				this.locale = new Lazy<>(() -> new Locale(getIsoLanguage(), ""));
			} else {
				throw new IllegalArgumentException("Invalid language code \"" + code + "\"; can only contain letters");
			}
		} else if ((code.length() == 5 || code.length() == 6) && code.charAt(2) == '-') {
			String language = code.substring(0, 2).toLowerCase();
			String country = code.substring(3).toUpperCase();

			if (StringWorker.containsOnly(language, Character::isLetter) && StringWorker.containsOnly(country, Character::isLetter)) {
				this.codes = new XArrayView<>(language + '-' + country, language, country);
				this.locale = new Lazy<>(() -> new Locale(getIsoLanguage(), getIsoCountry()));
			} else {
				throw new IllegalArgumentException("Invalid language code \"" + code + "\"; can only contain letters and separator (xx-xx)");
			}
		} else {
			throw new IllegalArgumentException("Invalid language code \"" + code + "\"; must be in format xx, xx-xx or xx-xxx");
		}

		this.name = name;
	}

	@Nonnull
	public Locale createLocale() {
		return new Locale(getCode());
	}

	@Nonnull
	public String getCode() {
		return getIdentifiers().first();
	}

	@Nonnull
	@Override
	public XView getIdentifiers() {
		return codes;
	}

	@Nonnull
	public String getIsoCountry() {
		return getIdentifiers().last();
	}

	@Nonnull
	public String getIsoLanguage() {
		return getIdentifiers().get(1);
	}

	@Nonnull
	public Locale getLocale() {
		return locale.get();
	}

	@Nonnull
	@Override
	public String getName() {
		return name;
	}

	@Nonnull
	@Override
	protected Object[] stringValues() {
		return new Object[] { getCode() };
	}

	@Nonnull
	public static XView values() {
		return Enumerations.values(Language.class);
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy