Class Translator

  • All Implemented Interfaces:

    
    public final class Translator
    
                        

    PolyTranslator is an i18n/l10n system used by PolyUI. It finds a language file according to the following spec:

    /translationDir/lang_COUNTRY.lang

    for example /translationDir/en_GB.lang.

    This can also be set with -Dpolyui.locale="ll_CC" or using Settings.defaultLocale.

    If the file cannot be found, PolyUI will check for a file named ll_default.lang (for example en_default.lang) and use that instead if present. If that isn't found, it will use the default locale (above), and if that isn't found it will crash.

    Keys can also be added using

    The file contains a simple table of my.key=value, with each line meaning a new translation. It will return the key if the file is found but the key is not present. We recommend using dot notation for your keys:

    my.key=hello there!
    hello.world=This works well!
    something.else=I can also substitute in %s objects using %s!
    really.useful=I use the sprintf(3) type format strings as specified by java.lang.String.format().
    • Method Detail

      • setDontWarn

         final Unit setDontWarn(@ApiStatus.Internal() Boolean dontWarn)

        Set to true to disable warnings when a translation is not found.

      • addDelegate

         final Translator addDelegate(String translationDir)

        add a delegate translator to this master translator.

        The specified translationDir will be used to load matching files of the current locale with keys.

        Since:

        1.2.1

      • setLocale

         final Translator setLocale(String locale)

        set the locale of this translator. All the keys are cleared, and PolyUI is reloaded.

      • loadKeys

        @ApiStatus.Internal() final Translator loadKeys(String resource, Boolean now)

        Add a file to the translation table, which will be used to load keys.

        Unless now is true, the file will only be loaded if the key is not found in the existing table.

        Note: Although this function is public it is technically internal API. using it directly probably won't work correctly when the locale is changed. You probably meant to use addDelegate instead.

        Since:

        0.17.5

      • addKeys

         final Translator addKeys(Pair<String, String> pairs)

        Add a set of keys to the translation table. You can use the Kotlin map syntax here:

        addKeys(
          "my.key" to "hello there!",
          "hello.world" to "This works well!",
        )
        Since:

        0.17.5

      • translate

         final Translator.Text translate(String key)

        translate the provided key, returning the key as per the translation table.

        If the key is not present in the table, then the resourcePath file is checked for the key. The value is then returned. Warnings will be issued if the file/key does not exist.