Skip to content

Commons Module

H3xadecimal edited this page Mar 9, 2024 · 1 revision

Localizations

Utilities for language files

This class will load resource from specified ClassLoader as language file

Class cn.afternode.commons.Localizations

Example Localizations File

Saved in /src/main/resources/lang_en.properties

loc.example=Hello World
loc.placeholder=Hello %name%

Example codes

Localizations loc = new Localizations("lang", Localization.class.getClassLoader(), "en);
System.out.println(loc.get("loc.example"));

Map<String, String> ph = new HashMap<>();
ph.put("name", "AFterNode");
System.out.println(loc.get("loc.placeholder", ph));

Constructors

Localizations(String prefix, ClassLoader loader)

  • prefix Localization file prefix
  • loader Class loader for getResourceAsStream

Localizations(String prefix, ClassLoader loader, String defaultId)

  • prefix Localization file prefix
  • loader Class loader for getResourceAsStream
  • defaultId Localizations will load language file on construct with this id

Methods

void loadLocalizations(String id)

Load language file from resource with specified ID

<prefix>_<id>.properties will be loaded

void loadDefault()

Call loadLocalizations(String) with ISO 639 code from Locale.getDefault().getLanguage()

String get(String key)

Get locale from loaded file with specified key

If key not present in language file, the key will be returned

String get(String key, Map<String, Object> placeholders)

Get locale from loaded file with specified key and apply placeholders

e.g. Placeholder in file is %name%, placeholder map is name: XXX