Class Language

java.lang.Object
com.ultikits.ultitools.entities.Language

public class Language extends Object
Language entity class.
Version:
1.0.0
Author:
wisdomme
  • Field Details

    • LOGGER

      private static final Logger LOGGER
    • GSON

      private static final com.google.gson.Gson GSON
    • MAP_TYPE

      private static final Type MAP_TYPE
    • dictionary

      private final Map<String,String> dictionary
    • fallback

      private final Language fallback
  • Constructor Details

    • Language

      public Language(File file)
      Creates a language dictionary by reading a JSON file.
      Parameters:
      file - the language JSON file
    • Language

      public Language(Map<String,String> dictionary)
      Creates directly from a dictionary.
      Parameters:
      dictionary - the language dictionary
    • Language

      public Language(String json)
      Creates a language dictionary from JSON.
      Parameters:
      json - the JSON string
    • Language

      private Language(Map<String,String> dictionary, Language fallback)
      Creates a language whose dictionary is consulted first, falling back to fallback for any key this dictionary does not contain.
      Parameters:
      dictionary - this language's own dictionary, authoritative for every key it contains
      fallback - consulted only for a key absent from dictionary; may be null
  • Method Details

    • withFallback

      public Language withFallback(Language fallback)
      Returns a Language that answers from this dictionary first and, for any key this dictionary lacks, from fallback instead of immediately returning the raw key.

      Real-machine finding (phase 13, PR #418): on an upgraded server a module jar adds a language key, but the copy of that language file already sitting in the module's data folder -- extracted by an older jar -- does not have it. Before this method existed, whichever Language the loader picked (disk, if present at all) was used exclusively, so a key the disk file lacked rendered as its own raw key even though the new jar shipped a translation for it. The disk file stays authoritative for every key it does contain, since server owners customise it.

      Parameters:
      fallback - the language to consult for a key this dictionary does not contain; may be null, in which case this language is returned unchanged
      Returns:
      a language falling back to fallback for missing keys
      Throws:
      IllegalArgumentException - if fallback is this language, or if fallback's own fallback chain already contains this language -- either would make getLocalizedText(java.lang.String) recurse forever for a key present in neither
      Since:
      6.3.0
    • fromYaml

      public static Language fromYaml(Reader reader)
      Builds a Language from a YAML language file.

      Eight of this ecosystem's sixteen modules ship lang/*.yml rather than lang/*.json, and until 6.3.0 the loader looked only for .json and returned an empty dictionary when it found nothing -- silently, so every message in those modules rendered as its own raw key (#389).

      The YAML is nested and the modules address it with dotted keys (i18n("world.delete.deleting")), which is exactly the form getKeys(true) produces, so the flattening is the library's own and not a convention invented here. Section nodes are skipped: only leaves that are actually strings become entries.

      Parameters:
      reader - the YAML source; the caller owns closing it
      Returns:
      a language whose dictionary is the flattened YAML
      Since:
      6.3.0
    • getLocalizedText

      public String getLocalizedText(String str)