Skip to content

Commit 4af025f

Browse files
committed
Create an API for plugin-defined text translations
1 parent ced8c68 commit 4af025f

File tree

6 files changed

+143
-151
lines changed

6 files changed

+143
-151
lines changed

src/main/java/org/spongepowered/api/text/TextFactory.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
*/
2525
package org.spongepowered.api.text;
2626

27+
import java.util.Locale;
28+
2729
/**
2830
* Represents the required implementation for the static methods in
2931
* {@link Texts}.
@@ -57,6 +59,16 @@ public interface TextFactory {
5759
*/
5860
String toPlain(Text text);
5961

62+
/**
63+
* Returns a plain text representation of the {@link Text} without any
64+
* formatting in the provided Locale.
65+
*
66+
* @param text The text to convert
67+
* @param locale The language to get the plain string in
68+
* @return The text converted to plain text
69+
*/
70+
String toPlain(Text text, Locale locale);
71+
6072
/**
6173
* Returns a JSON representation of the {@link Text} as used in commands.
6274
*
@@ -65,6 +77,14 @@ public interface TextFactory {
6577
*/
6678
String toJson(Text text);
6779

80+
/**
81+
* Returns a JSON representation of the {@link Text} as used in commands in the specified language.
82+
*
83+
* @param text The text to convert
84+
* @return The text converted to JSON
85+
*/
86+
String toJson(Text text, Locale locale);
87+
6888
/**
6989
* Returns the default legacy formatting character.
7090
*
@@ -111,4 +131,14 @@ public interface TextFactory {
111131
*/
112132
String toLegacy(Text text, char code);
113133

134+
/**
135+
* Returns a representation of the {@link Text} using the legacy color
136+
* codes in the given Locale.
137+
*
138+
* @param text The text to convert
139+
* @param code The legacy char to use for the message
140+
* @return The text converted to the old color codes
141+
*/
142+
String toLegacy(Text text, char code, Locale locale);
143+
114144
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package org.spongepowered.api.text.translation;
2+
3+
import com.google.common.base.Function;
4+
import com.google.common.base.Preconditions;
5+
6+
import java.util.Locale;
7+
import java.util.ResourceBundle;
8+
9+
/**
10+
* A translation class designed to be used for ResourceBundles. For convenience, most users will want to wrap this in a class that keeps track of
11+
* resource bundles. A simple implementaion would look like:
12+
* <pre>
13+
* public class TranslationHelper {
14+
* private static final Function<Locale, ResourceBundle> LOOKUP_FUNC = new Function<Locale, ResourceBundle>() {
15+
* &at;Nullable
16+
* &at;Override
17+
* public ResourceBundle apply(Locale input) {
18+
* return ResourceBundle.getBundle("com.mydomain.myplugin.Translations", input);
19+
* }
20+
* };
21+
*
22+
* private TranslationHelper() {} // Prevent instance creation
23+
*
24+
* public static Text t(String key, Object... args) {
25+
* return Texts.of(new ResourceBundleTranslation(key, LOOKUP_FUNC), args);
26+
* }
27+
* }
28+
*
29+
* </pre>
30+
*/
31+
public class ResourceBundleTranslation implements Translation {
32+
private final String key;
33+
private final Function<Locale, ResourceBundle> bundleFunction;
34+
35+
public ResourceBundleTranslation(String key, Function<Locale, ResourceBundle> bundleFunction) {
36+
this.key = key;
37+
this.bundleFunction = bundleFunction;
38+
}
39+
40+
41+
@Override
42+
public String getId() {
43+
return this.key;
44+
}
45+
46+
@Override
47+
public String getName() {
48+
return this.key;
49+
}
50+
51+
@Override
52+
public String get(Locale locale) {
53+
Preconditions.checkNotNull(locale, "locale");
54+
ResourceBundle bundle = this.bundleFunction.apply(locale);
55+
return bundle == null ? this.key : bundle.getString(this.key);
56+
}
57+
58+
@Override
59+
public String get(Locale locale, Object... args) {
60+
return String.format(locale, get(locale), args);
61+
}
62+
}

src/main/java/org/spongepowered/api/text/translation/Translation.java

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,21 @@
2525
package org.spongepowered.api.text.translation;
2626

2727
import org.spongepowered.api.CatalogType;
28-
import org.spongepowered.api.text.translation.locale.Locales;
28+
29+
import java.util.Locale;
2930

3031
/**
3132
* Represents an identifier for text that can be translated into multiple
32-
* languages. This is usually sent directly to the client and translated there,
33-
* not on the server.
33+
* languages. Minecraft-included translations are generally translated clientside.
34+
* Translations not included in Minecraft are generally expected to
35+
* be translated server-side, for example using Gettext or a
36+
* {@link java.util.ResourceBundle}
3437
*
3538
* <p>Some translations require parameters to be sent together with them, if
3639
* they're not given they will be filled with empty text.</p>
3740
*
38-
* <p>While the client has multiple locales available, most implementations
39-
* support only {@link Locales#ENGLISH}.</p>
41+
* <p>While the client has multiple locales available, server-side vanilla translations
42+
* support only {@link Locale#ENGLISH}.</p>
4043
*/
4144
public interface Translation extends CatalogType {
4245

@@ -45,6 +48,7 @@ public interface Translation extends CatalogType {
4548
*
4649
* @return The translation identifier of this translation
4750
*/
51+
@Override
4852
String getId();
4953

5054
/**
@@ -53,19 +57,13 @@ public interface Translation extends CatalogType {
5357
*
5458
* @return The default translation without any parameters
5559
*/
56-
String get();
60+
String get(Locale locale);
5761

5862
/**
5963
* Gets the default translation with the specified parameters.
6064
*
6165
* @param args The parameters for this translation
6266
* @return The default translation with the specified parameters
6367
*/
64-
String get(Object... args);
65-
66-
// This would only work on the client, the server has only the English
67-
// translation
68-
// String get(Locale locale);
69-
// String get(Locale locale, Object... args);
70-
68+
String get(Locale locale, Object... args);
7169
}

src/main/java/org/spongepowered/api/text/translation/locale/Locales.java

Lines changed: 0 additions & 113 deletions
This file was deleted.

src/main/java/org/spongepowered/api/text/translation/locale/package-info.java

Lines changed: 0 additions & 25 deletions
This file was deleted.
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package org.spongepowered.api.util;
2+
3+
import com.google.common.base.Function;
4+
import org.spongepowered.api.text.Text;
5+
import org.spongepowered.api.text.Texts;
6+
import org.spongepowered.api.text.translation.ResourceBundleTranslation;
7+
8+
import java.util.Locale;
9+
import java.util.ResourceBundle;
10+
11+
import javax.annotation.Nullable;
12+
13+
/**
14+
* This class provides translations for strings within SpongeAPI. Plugins should consult an implementaion of Translation for help.
15+
*/
16+
public class SpongeApiTranslationHelper {
17+
18+
private static final Function<Locale, ResourceBundle> LOOKUP_FUNC = new Function<Locale, ResourceBundle>() {
19+
@Nullable
20+
@Override
21+
public ResourceBundle apply(Locale input) {
22+
return ResourceBundle.getBundle("org.spongepowered.api.Translations", input);
23+
}
24+
};
25+
26+
private SpongeApiTranslationHelper() {
27+
} // Prevent instance creation
28+
29+
/**
30+
* Get the translated text for a given string.
31+
*
32+
* @param key The translation key
33+
* @param args Translation parameters
34+
* @return The translatable text
35+
*/
36+
public static Text t(String key, Object... args) {
37+
return Texts.of(new ResourceBundleTranslation(key, LOOKUP_FUNC), args);
38+
}
39+
40+
}

0 commit comments

Comments
 (0)