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

com.github.bloodshura.ignitium.cfg.i18n.I18n 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.list.XList;
import com.github.bloodshura.ignitium.collection.list.impl.XArrayList;
import com.github.bloodshura.ignitium.collection.map.XMap;
import com.github.bloodshura.ignitium.collection.map.impl.XLinkedMap;
import com.github.bloodshura.ignitium.collection.view.XBasicView;
import com.github.bloodshura.ignitium.collection.view.XView;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.swing.AbstractButton;
import javax.swing.JLabel;
import javax.swing.text.JTextComponent;
import java.io.IOException;
import java.util.concurrent.atomic.AtomicInteger;

public final class I18n {
	private static final XMap LISTENERS = new XLinkedMap<>();
	private static final AtomicInteger NEXT_ID = new AtomicInteger();
	private static final XList UNITS = new XArrayList<>();
	private static TranslationUnit CURRENT_UNIT;

	private I18n() {
	}

	public static void apply(@Nonnull Language language) throws IOException {
		TranslationUnit unit = unitOf(language);

		if (unit != null) {
			apply(unit);
		}
	}

	public static void apply(@Nonnull String languageCode) throws IOException {
		TranslationUnit unit = unitOf(languageCode);

		if (unit != null) {
			apply(unit);
		}
	}

	public static void apply(@Nonnull TranslationUnit unit) throws IOException {
		I18n.CURRENT_UNIT = unit;

		if (!unit.isLoaded()) {
			unit.load();
		}

		for (Runnable listener : LISTENERS.values()) {
			listener.run();
		}
	}

	@Nullable
	public static TranslationUnit currentUnit() {
		return CURRENT_UNIT;
	}

	public static boolean has(@Nonnull Language language) {
		return unitOf(language) != null;
	}

	public static int listen(@Nonnull AbstractButton component, @Nonnull String i18nKey) {
		return listen(() -> component.setText(translate(i18nKey)));
	}

	public static int listen(@Nonnull JLabel component, @Nonnull String i18nKey) {
		return listen(() -> component.setText(translate(i18nKey)));
	}

	public static int listen(@Nonnull JTextComponent component, @Nonnull String i18nKey) {
		return listen(() -> component.setText(translate(i18nKey)));
	}

	public static int listen(@Nonnull Runnable runnable) {
		int id = NEXT_ID.getAndIncrement();

		synchronized (LISTENERS) {
			LISTENERS.add(id, runnable);
		}

		if (currentUnit() != null) {
			runnable.run();
		}

		return id;
	}

	public static void register(@Nonnull TranslationUnit unit) throws IOException {
		synchronized (UNITS) {
			UNITS.add(unit);
		}

		if (currentUnit() == null) {
			apply(unit);
		}
	}

	@Nonnull
	public static String translate(@Nonnull String i18nKey, @Nonnull Object... parameters) {
		if (currentUnit() == null) {
			return i18nKey;
		}

		return currentUnit().getDictionary().translate(i18nKey, parameters);
	}

	@Nullable
	public static TranslationUnit unitOf(@Nonnull Language language) {
		return units().selectFirst(unit -> unit.getLanguage() == language);
	}

	@Nullable
	public static TranslationUnit unitOf(@Nonnull String languageCode) {
		return units().selectFirst(unit -> unit.getLanguage().getCode().equals(languageCode));
	}

	@Nonnull
	public static XView units() {
		return new XBasicView<>(UNITS);
	}

	public static boolean unlisten(int id) {
		synchronized (LISTENERS) {
			return LISTENERS.removeKey(id);
		}
	}

	public static boolean unregister(@Nonnull TranslationUnit unit) {
		try {
			TranslationUnit replacement;

			synchronized (UNITS) {
				replacement = units().selectFirst(u -> u != unit);
				UNITS.remove(unit);
			}

			apply(replacement);

			return true;
		} catch (IOException exception) {
			return false;
		}
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy