Class I18n
This class serves as an interface for application localization.
It is mostly a wrapper around Localizer but supports flexible specification of the bundle to be used.
To state which bundle to use the direct way is to include it in the key following the format bundle$key.
For example a message with key user$not-found will be searched for in the bundle user and the key not-found.
dollar sign
The dollar ($) is a reserved character for bundle name separation.
Practically, in all cases this doesn't really bother, there are only 2 niche situations where the dollar has to be escaped:
- your message key contains
$and no bundle is explicitly stated, e.g.key.with$.in.it - the string is a directly inserted localization messages containing
$, that happens to have it's prior$part to match a bundle name and its after$part to match a message key, e.g.- you have a bundle called
my_bundle - you have a message key called
my-keyin that bundle - and you want to print the message
my_bundle$my-keyto the user (not the message stored under "my-key" in the bundle "my_bundle")
- you have a bundle called
In these cases just prefix your whole message with a $, e.g. $my_bundle$my-key or $key.with$.in.it.
Now the bundle will be treated as not stated explicitly and the dollar sign will be preserved.
Special bundle names
JDA-Commands uses a special bundle called 'jdac' to allow the customization of certain error messages and general strings used by the framework that are presented to the user of the discord bot later. That means: The bundle name 'jdac' is reserved by JDA-Commands and cannot be used for your own localization messages.
For information on what strings are localizable/customizable please visit our wiki.
bundle name traversal
If no bundle is specified, it will traverse the stack (the called methods) and search for the nearest
@Bundle("mybundle") annotation with following order:
- method that called
localize(Locale, String, Entry...) - other called methods in the same class
- this methods class
- the class' packages
package-info.javafile
If no annotation is found, the previous method (in another class) is searched with the same pattern up to the class at the very beginning.
If even after this no bundle name could be found, the bundle default will be used.
Example
A.java:
package my.app;
class A {
void aOne() {
i18n.localize(Locale.GERMAN, "fail", Map.of())
}
void aTwo() {
aOne();
}
}
B.java:
package my.app.other;
@Bundle("b_bundle")
class B {
A another = new A();
void bOne() {
a.aOne();
}
@Bundle("mB_bundle")
void bTwo() {
bOne();
}
}
package-info.java:
@Bundle("pack_bundle")
package my.app;
The order in which the bundle name is searched for is following:
- method
A$aOne() - method
A$aTwo() - class
A package-info.javaof packagemy.app- method
B$bOne() - method
B$two()
The found bundle would be pack_bundle.
If localize(java.util.Locale, java.lang.String, Entry...)
would be called in, for example, B$bTwo the bundle would be mB_bundle.
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
-
Field Details
-
DEFAULT_BUNDLE
- See Also:
-
-
Constructor Details
-
I18n
- Parameters:
descriptor- theDescriptionto be used to get theBundleannotationlocalizer- the usedLocalizerto retrieve the messages
-
-
Method Details
-
localize
public String localize(Locale locale, String combinedKey, Map<String, @Nullable Object> placeholder) This method returns the localized method found by the provided
Localeand key in the given bundle.The bundle can be either explicitly stated by adding it to the key in the following format:
bundle$key. Alternatively, the bundle name can also be contextual retrieved by a search for theBundleannotation, see class docs.Please note that the character
$is forbidden in bundle names and the bundle name 'jdac' is reserved. For further information visit the class docs.- Parameters:
locale- theLocaleto be used to localize the keycombinedKey- the messages keyplaceholder- the placeholder to be used- Returns:
- the localized message or the key if not found
-
localize
This method returns the localized message found by the provided
Localeand key in the given bundle.The bundle can be either explicitly stated by adding it to the key in the following format:
bundle$key. Alternatively, the bundle name can also be contextual retrieved by a search for theBundleannotation, see class docs.- Parameters:
locale- theLocaleto be used to localize the keykey- the messages keyplaceholder- the placeholder to be used- Returns:
- the localized message or the key if not found
-
localizationFunction
- Returns:
- the
LocalizationFunctionbases on this class, for use with JDA
-