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
.
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.java
file
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.java
of packagemy.app
- method
B#bOne()
- method
B#two()
The found bundle would be pack_bundle
.
If localize(java.util.Locale, java.lang.String, com.github.kaktushose.jda.commands.i18n.I18n.Entry...)
would be called in, for example, B#bTwo
the bundle would be mB_bundle
.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final record
A placeholder identified by its name with the value to be substituted. -
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic I18n.Entry
This method returns anI18n.Entry
containing the name and value provided.localize
(Locale locale, String key, I18n.Entry... placeholder) This method returns the localized message found by the providedLocale
and key in the given bundle.This method returns the localized method found by the providedLocale
and key in the given bundle.
-
Field Details
-
DEFAULT_BUNDLE
- See Also:
-
-
Constructor Details
-
I18n
- Parameters:
descriptor
- theDescription
to be used to get theBundle
annotationlocalizer
- the usedLocalizer
to retrieve the messages
-
-
Method Details
-
localize
This method returns the localized method found by the provided
Locale
and 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 theBundle
annotation, see class docs.- Parameters:
locale
- theLocale
to 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
Locale
and 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 theBundle
annotation, see class docs.- Parameters:
locale
- theLocale
to be used to localize the keykey
- the messages keyplaceholder
- the placeholder to be used- Returns:
- the localized message or the key if not found
-
entry
This method returns anI18n.Entry
containing the name and value provided. It comes in handy when imported with a static import.- Parameters:
name
- the name of the placeholdervalue
- the value of the placeholder- Returns:
- the
I18n.Entry
consisting of the name and value
-
localizationFunction
- Returns:
- the
LocalizationFunction
bases on this class, for use with JDA
-