Skip to content

Commit

Permalink
refactor: Complete overhaul of the localization system to translation…
Browse files Browse the repository at this point in the history
… system
  • Loading branch information
phinner committed Apr 14, 2024
1 parent 98ab7e9 commit 7dc9ded
Show file tree
Hide file tree
Showing 15 changed files with 137 additions and 123 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@
import com.xpdustry.distributor.command.cloud.parser.TeamParser;
import com.xpdustry.distributor.command.cloud.specifier.AllTeams;
import com.xpdustry.distributor.content.TypedContentType;
import com.xpdustry.distributor.localization.LocalizationSource;
import com.xpdustry.distributor.plugin.MindustryPlugin;
import com.xpdustry.distributor.plugin.PluginAware;
import com.xpdustry.distributor.translation.TranslationSource;
import io.leangen.geantyref.TypeToken;
import java.text.MessageFormat;
import java.util.Objects;
Expand Down Expand Up @@ -72,9 +72,9 @@ public ArcCommandManager(
this.registerDefaultExceptionHandlers();

this.captionRegistry().registerProvider((caption, sender) -> {
final var source = DistributorProvider.get().getGlobalLocalizationSource();
final var source = DistributorProvider.get().getGlobalTranslationSource();
final var locale = this.senderMapper().reverse(sender).getLocale();
return source.getLocalization(caption.key(), locale, LocalizationSource.DEFAULT_FALLBACK)
return source.getTranslation(caption.key(), locale, TranslationSource.DEFAULT_FALLBACK)
.formatEmpty();
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@
package com.xpdustry.distributor.command.lamp;

import com.xpdustry.distributor.DistributorProvider;
import com.xpdustry.distributor.localization.ListLocalizationSource;
import com.xpdustry.distributor.localization.Localization;
import com.xpdustry.distributor.localization.LocalizationSource;
import com.xpdustry.distributor.localization.LocalizationSourceRegistry;
import com.xpdustry.distributor.translation.MessageFormatTranslation;
import com.xpdustry.distributor.translation.Translation;
import com.xpdustry.distributor.translation.TranslationRegistry;
import com.xpdustry.distributor.translation.TranslationSource;
import com.xpdustry.distributor.translation.TranslationSourceRegistry;
import java.text.MessageFormat;
import java.util.Locale;
import java.util.MissingResourceException;
Expand All @@ -35,58 +36,58 @@

public final class DistributorTranslator implements Translator {

private final ListLocalizationSource source = ListLocalizationSource.create();
private final TranslationSourceRegistry source = TranslationSourceRegistry.create();
private Locale locale;

DistributorTranslator(final Translator parent) {
this.locale = parent.getLocale();
source.addLocalizationSource(new TranslatorSource(parent));
source.addLocalizationSource(DistributorProvider.get().getGlobalLocalizationSource());
source.register(new TranslatorSource(parent));
source.register(DistributorProvider.get().getGlobalTranslationSource());
}

@Override
public @NotNull String get(final String key) {
return source.getLocalization(key, locale, LocalizationSource.DEFAULT_FALLBACK)
return source.getTranslation(key, locale, TranslationSource.DEFAULT_FALLBACK)
.formatEmpty();
}

@Override
public @NotNull String get(final String key, final Locale locale) {
return source.getLocalization(key, locale, LocalizationSource.DEFAULT_FALLBACK)
return source.getTranslation(key, locale, TranslationSource.DEFAULT_FALLBACK)
.formatEmpty();
}

@Override
public void add(final LocaleReader reader) {
source.addLocalizationSource(new LocaleReaderSource(reader));
source.register(new LocaleReaderSource(reader));
}

@Override
public void add(final ResourceBundle resourceBundle) {
final var registry = LocalizationSourceRegistry.create(resourceBundle.getLocale());
final var registry = TranslationRegistry.create(resourceBundle.getLocale());
registry.registerAll(resourceBundle.getLocale(), resourceBundle);
source.addLocalizationSource(registry);
source.register(registry);
}

@Override
public void addResourceBundle(final String resourceBundle, final Locale... locales) {
final var registry = LocalizationSourceRegistry.create(Locale.ROOT);
final var registry = TranslationRegistry.create(Locale.ROOT);
for (final var locale : locales) {
registry.registerAll(locale, resourceBundle, getClass().getClassLoader());
}
source.addLocalizationSource(registry);
source.register(registry);
}

@Override
public void addResourceBundle(final @NotNull String resourceBundle) {
final var registry = LocalizationSourceRegistry.create(Locale.ROOT);
final var registry = TranslationRegistry.create(Locale.ROOT);
for (final var locale : Locales.getLocales()) {
try {
registry.registerAll(locale, resourceBundle, getClass().getClassLoader());
} catch (MissingResourceException ignored) {
}
}
source.addLocalizationSource(registry);
source.register(registry);
}

@Override
Expand All @@ -99,22 +100,22 @@ public void setLocale(final Locale locale) {
this.locale = locale;
}

private record LocaleReaderSource(LocaleReader reader) implements LocalizationSource {
private record LocaleReaderSource(LocaleReader reader) implements TranslationSource {

@Override
public @Nullable Localization getLocalization(final String key, Locale locale) {
public @Nullable Translation getTranslation(final String key, Locale locale) {
return reader.getLocale().equals(locale) && reader.containsKey(key)
? Localization.message(new MessageFormat(reader.get(key)))
? MessageFormatTranslation.of(new MessageFormat(reader.get(key)))
: null;
}
}

private record TranslatorSource(Translator translator) implements LocalizationSource {
private record TranslatorSource(Translator translator) implements TranslationSource {

@Override
public @Nullable Localization getLocalization(final String key, final Locale locale) {
public @Nullable Translation getTranslation(final String key, final Locale locale) {
final var result = translator.get(key, locale);
return result.equals(key) ? null : Localization.message(new MessageFormat(key));
return result.equals(key) ? null : MessageFormatTranslation.of(new MessageFormat(key));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@
package com.xpdustry.distributor;

import com.xpdustry.distributor.event.EventBus;
import com.xpdustry.distributor.localization.ListLocalizationSource;
import com.xpdustry.distributor.permission.PermissionManager;
import com.xpdustry.distributor.scheduler.PluginScheduler;
import com.xpdustry.distributor.service.ServiceManager;
import com.xpdustry.distributor.translation.TranslationSourceRegistry;

public interface Distributor {

ServiceManager getServiceManager();

ListLocalizationSource getGlobalLocalizationSource();
TranslationSourceRegistry getGlobalTranslationSource();

EventBus getEventBus();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,34 +19,34 @@
package com.xpdustry.distributor.command;

import com.xpdustry.distributor.DistributorProvider;
import com.xpdustry.distributor.localization.LocalizationSource;
import com.xpdustry.distributor.translation.TranslationSource;
import java.util.Locale;

record LocalizedDescriptionFacade(String key, Locale defaultLocale) implements DescriptionFacade {

@Override
public String getText() {
return DistributorProvider.get()
.getGlobalLocalizationSource()
.getLocalization(key, this.defaultLocale, LocalizationSource.DEFAULT_FALLBACK)
.getGlobalTranslationSource()
.getTranslation(key, this.defaultLocale, TranslationSource.DEFAULT_FALLBACK)
.formatEmpty();
}

@Override
public String getText(final CommandSender sender) {
return DistributorProvider.get()
.getGlobalLocalizationSource()
.getLocalization(key, sender.getLocale(), LocalizationSource.DEFAULT_FALLBACK)
.getGlobalTranslationSource()
.getTranslation(key, sender.getLocale(), TranslationSource.DEFAULT_FALLBACK)
.formatEmpty();
}

@Override
public boolean isEmpty() {
return DistributorProvider.get().getGlobalLocalizationSource().getLocalization(key, this.defaultLocale) != null;
return DistributorProvider.get().getGlobalTranslationSource().getTranslation(key, this.defaultLocale) != null;
}

@Override
public boolean isEmpty(final CommandSender sender) {
return DistributorProvider.get().getGlobalLocalizationSource().getLocalization(key, sender.getLocale()) != null;
return DistributorProvider.get().getGlobalTranslationSource().getTranslation(key, sender.getLocale()) != null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,35 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.xpdustry.distributor.localization;
package com.xpdustry.distributor.translation;

import com.xpdustry.distributor.internal.DistributorDataClass;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.immutables.value.Value;
import org.jspecify.annotations.Nullable;

record MessageFormatLocalization(MessageFormat message) implements Localization {
@DistributorDataClass
@Value.Immutable
public sealed interface MessageFormatTranslation extends Translation permits MessageFormatTranslationImpl {

private static final int MAX_LENGTH = 64;
int MAX_NAMED_INDEX = 63;

static MessageFormatTranslation of(final MessageFormat format) {
return MessageFormatTranslationImpl.of(format);
}

MessageFormat getMessageFormat();

@Override
public String formatArray(final Object... args) {
return this.message.format(args);
default String formatArray(final Object... args) {
return this.getMessageFormat().format(args);
}

@Override
public String formatNamed(final Map<String, Object> args) {
default String formatNamed(final Map<String, Object> args) {
final List<@Nullable Object> entries = new ArrayList<>();
for (final var entry : args.entrySet()) {
final int index;
Expand All @@ -43,20 +53,20 @@ public String formatNamed(final Map<String, Object> args) {
} catch (NumberFormatException ignored) {
continue;
}
if (MAX_LENGTH > index) {
if (index > MAX_NAMED_INDEX) {
throw new IllegalArgumentException(
"Max argument index exceeded, expected less than " + MAX_LENGTH + ", got " + index);
"Max argument index exceeded, expected less than " + MAX_NAMED_INDEX + ", got " + index);
}
for (int i = entries.size(); i < index; i++) {
for (int i = entries.size(); i <= index; i++) {
entries.add(null);
}
entries.set(index, entry.getValue());
}
return this.message.format(entries.toArray());
return this.getMessageFormat().format(entries.toArray());
}

@Override
public String formatEmpty() {
return this.message.toPattern();
default String formatEmpty() {
return this.getMessageFormat().toPattern();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,29 +16,27 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.xpdustry.distributor.localization;
package com.xpdustry.distributor.translation;

import java.text.MessageFormat;
import java.util.Locale;
import org.jspecify.annotations.Nullable;

final class RouterLocalizationSource implements LocalizationSource {
final class RouterTranslationSource implements TranslationSource {

static final RouterLocalizationSource INSTANCE = new RouterLocalizationSource();
static final RouterTranslationSource INSTANCE = new RouterTranslationSource();

static final Locale ROUTER_LOCALE = new Locale("router");
private static final Localization ROUTER_LOCALIZATION =
Localization.message(new MessageFormat("router", ROUTER_LOCALE));
private static final Translation ROUTER_TRANSLATION = TextTranslation.of("router");

private RouterLocalizationSource() {}
private RouterTranslationSource() {}

@Override
public @Nullable Localization getLocalization(final String key, final Locale locale) {
return locale.equals(ROUTER_LOCALE) ? ROUTER_LOCALIZATION : null;
public @Nullable Translation getTranslation(final String key, final Locale locale) {
return locale.getLanguage().equals(ROUTER_LOCALE.getLanguage()) ? ROUTER_TRANSLATION : null;
}

@Override
public String toString() {
return "RouterLocalizationSource";
return getClass().getSimpleName();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,34 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.xpdustry.distributor.localization;
package com.xpdustry.distributor.translation;

import com.xpdustry.distributor.internal.DistributorDataClass;
import java.util.Map;
import org.immutables.value.Value;

record TextLocalization(String text) implements Localization {
@DistributorDataClass
@Value.Immutable
public sealed interface TextTranslation extends Translation permits TextTranslationImpl {

static TextTranslation of(final String text) {
return TextTranslationImpl.of(text);
}

String getText();

@Override
public String formatArray(final Object... args) {
return this.text;
default String formatArray(final Object... args) {
return this.getText();
}

@Override
public String formatNamed(final Map<String, Object> args) {
return this.text;
default String formatNamed(final Map<String, Object> args) {
return this.getText();
}

@Override
public String formatEmpty() {
return this.text;
default String formatEmpty() {
return this.getText();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,11 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.xpdustry.distributor.localization;
package com.xpdustry.distributor.translation;

import java.text.MessageFormat;
import java.util.Map;

public interface Localization {

static Localization text(final String text) {
return new TextLocalization(text);
}

static Localization message(final MessageFormat message) {
return new MessageFormatLocalization(message);
}
public interface Translation {

String formatArray(final Object... args);

Expand Down
Loading

0 comments on commit 7dc9ded

Please sign in to comment.