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

me.moros.bending.api.locale.Translation Maven / Gradle / Ivy

/*
 * Copyright 2020-2024 Moros
 *
 * This file is part of Bending.
 *
 * Bending is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Bending is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with Bending. If not, see .
 */

package me.moros.bending.api.locale;

import java.text.MessageFormat;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Locale;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Objects;
import java.util.ResourceBundle;

import net.kyori.adventure.key.Key;
import net.kyori.adventure.key.Keyed;

/**
 * Represents a translation of one or more key-value pairs.
 */
public final class Translation implements Keyed, Iterable> {
  public static final Locale DEFAULT_LOCALE = Locale.ENGLISH;

  private final Key key;
  private final Locale locale;
  private final Map formats;
  private final int hashcode;

  private Translation(Key key, Locale locale, Map formats) {
    this.key = key;
    this.locale = locale;
    this.formats = Map.copyOf(formats);
    this.hashcode = Objects.hash(this.key, this.formats);
  }

  /**
   * Get the locale for this translation.
   * @return the locale
   */
  public Locale locale() {
    return locale;
  }

  @Override
  public boolean equals(Object obj) {
    if (this == obj) {
      return true;
    }
    if (obj instanceof Translation other) {
      return key.equals(other.key) && formats.equals(other.formats);
    }
    return false;
  }

  @Override
  public int hashCode() {
    return hashcode;
  }

  @Override
  public Key key() {
    return key;
  }

  @Override
  public Iterator> iterator() {
    return formats.entrySet().iterator();
  }

  /**
   * Create a translation from a ResourceBundle using the default Bending Locale.
   * @param key a unique key to identify your translation when registering
   * @param bundle a bundle containing localized strings
   * @return the constructed translation
   * @throws IllegalArgumentException if bundle is empty
   */
  public static Translation fromBundle(Key key, ResourceBundle bundle) {
    return fromBundle(key, DEFAULT_LOCALE, bundle);
  }

  /**
   * Create a translation from a ResourceBundle.
   * @param key a unique key to identify your translation when registering
   * @param locale a locale for the translation
   * @param bundle a bundle containing localized strings
   * @return the constructed translation
   * @throws IllegalArgumentException if bundle is empty
   */
  public static Translation fromBundle(Key key, Locale locale, ResourceBundle bundle) {
    Objects.requireNonNull(key);
    Objects.requireNonNull(bundle);
    Map formats = new HashMap<>();
    for (String bundleKey : bundle.keySet()) {
      formats.put(bundleKey, new MessageFormat(bundle.getString(bundleKey), locale));
    }
    if (formats.isEmpty()) {
      throw new IllegalArgumentException("No translations found!");
    }
    return new Translation(key, locale, formats);
  }

  /**
   * Create a translation from a map of localized Strings using the default Bending Locale.
   * @param key a unique key to identify your translation when registering
   * @param translations a map with localized strings
   * @return the constructed translation
   * @throws IllegalArgumentException if translations is empty
   */
  public static Translation create(Key key, Map translations) {
    return create(key, DEFAULT_LOCALE, translations);
  }

  /**
   * Create a translation from a map of localized Strings.
   * @param key a unique key to identify your translation when registering
   * @param locale the locale for your translation
   * @param translations a map with localized strings
   * @return the constructed translation
   * @throws IllegalArgumentException if translations is empty
   */
  public static Translation create(Key key, Locale locale, Map translations) {
    Objects.requireNonNull(key);
    Objects.requireNonNull(locale);
    Objects.requireNonNull(translations);
    Map formats = new HashMap<>();
    for (var entry : translations.entrySet()) {
      formats.put(entry.getKey(), new MessageFormat(entry.getValue(), locale));
    }
    if (formats.isEmpty()) {
      throw new IllegalArgumentException("No translations found!");
    }
    return new Translation(key, locale, formats);
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy