|
| 1 | +--- |
| 2 | +title: Localizer |
| 3 | +sidebar: |
| 4 | + order: 20 |
| 5 | + badge: |
| 6 | + text: Experimental |
| 7 | + variant: caution |
| 8 | +--- |
| 9 | + |
| 10 | +:::caution[Experimental] |
| 11 | +The API described in this page is still experimental and subject to change. |
| 12 | +However, there will be no breaking changes to methods detailed below. |
| 13 | +If necessary, they would only be deprecated for future removal. |
| 14 | +::: |
| 15 | + |
| 16 | +The Localizer is an **experimental** interface defining the methods available for localization. |
| 17 | +Currently, it is only used by addons to load their language files, which is described below. |
| 18 | + |
| 19 | +## Obtaining a Localizer |
| 20 | +A Localizer instance is available through your [registered addon](../../skript/addons) by calling the `localizer()` method: |
| 21 | +```java |
| 22 | +SkriptAddon addon = ...; |
| 23 | +Localizer addonLocalizer = addon.localizer(); |
| 24 | +``` |
| 25 | + |
| 26 | +## Loading language files |
| 27 | +The localizer currently provides a single method, `setSourceDirectories(String, String)` for loading an addon's language files. |
| 28 | +It takes two parameters: |
| 29 | +- A string representing the path to the directory (on the jar) containing language files |
| 30 | +- A string representing the path to the directory (on the disk) containing language files |
| 31 | + |
| 32 | +The second parameter may be null. That is, there is no requirement that you store language files outside your application's jar. |
| 33 | +However, you may wish to do so if you have language files that are intended to be customizable by the user. |
| 34 | + |
| 35 | +### Example usage |
| 36 | +Let's say my addon's language files are stored in a directory `lang` on the jar (as is typical). |
| 37 | +I can simply call the `setSourceDirectories` method with this path: |
| 38 | +```java |
| 39 | +Localizer localizer = ...; |
| 40 | +localizer.setSourceDirectories("lang", null); |
| 41 | +``` |
| 42 | +After calling this method, Skript will immediately load the `default.lang` and `english.lang` (if present) files from that directory in your jar. |
| 43 | + |
| 44 | +## Evaluating entries |
| 45 | +The `translate(String)` method can be used for obtaining the translation of a language key (the parameter). |
| 46 | +This method will return `null` if there is no translation for the provided key. |
| 47 | + |
| 48 | +Let's say my addon's default language file has the following entry: |
| 49 | +``` |
| 50 | +// default.lang |
| 51 | +error messages: |
| 52 | + unknown error: An unknown error has occurred. |
| 53 | +``` |
| 54 | +I can obtain its value by simply calling: |
| 55 | +```java |
| 56 | +Localizer localizer = ...; |
| 57 | +String message = localizer.translate("error messages.unknown error"); |
| 58 | +``` |
0 commit comments