Skip to content

Commit 17c2a56

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

File tree

6 files changed

+168
-130
lines changed

6 files changed

+168
-130
lines changed

src/main/java/org/spongepowered/api/entity/player/Player.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
import org.spongepowered.api.resourcepack.ResourcePack;
3232
import org.spongepowered.api.scoreboard.Scoreboard;
3333
import org.spongepowered.api.text.Text;
34-
import org.spongepowered.api.text.translation.locale.Locales;
3534
import org.spongepowered.api.util.command.CommandSource;
3635

3736
import java.util.Locale;
@@ -50,7 +49,6 @@ public interface Player extends Human, User, CommandSource, Viewer {
5049
* Gets the locale used by the player.
5150
*
5251
* @return The player's locale
53-
* @see Locales
5452
*/
5553
Locale getLocale();
5654

@@ -87,7 +85,7 @@ public interface Player extends Human, User, CommandSource, Viewer {
8785
* @param reason The reason for the kick
8886
*/
8987
void kick(Text.Literal reason);
90-
88+
9189
/**
9290
* Gets the {@link Scoreboard} displayed to the player.
9391
*

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: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
* This file is part of SpongeAPI, licensed under the MIT License (MIT).
3+
*
4+
* Copyright (c) SpongePowered <https://www.spongepowered.org>
5+
* Copyright (c) contributors
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy
8+
* of this software and associated documentation files (the "Software"), to deal
9+
* in the Software without restriction, including without limitation the rights
10+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be included in
15+
* all copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
* THE SOFTWARE.
24+
*/
25+
package org.spongepowered.api.text.translation;
26+
27+
import com.google.common.base.Function;
28+
import com.google.common.base.Preconditions;
29+
30+
import java.util.Locale;
31+
import java.util.ResourceBundle;
32+
33+
/**
34+
* 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
35+
* resource bundles. A simple implementaion would look like:
36+
* <pre>
37+
* public class TranslationHelper {
38+
* private static final Function<Locale, ResourceBundle> LOOKUP_FUNC = new Function<Locale, ResourceBundle>() {
39+
* &at;Nullable
40+
* &at;Override
41+
* public ResourceBundle apply(Locale input) {
42+
* return ResourceBundle.getBundle("com.mydomain.myplugin.Translations", input);
43+
* }
44+
* };
45+
*
46+
* private TranslationHelper() {} // Prevent instance creation
47+
*
48+
* public static Text t(String key, Object... args) {
49+
* return Texts.of(new ResourceBundleTranslation(key, LOOKUP_FUNC), args);
50+
* }
51+
* }
52+
*
53+
* </pre>
54+
*/
55+
public class ResourceBundleTranslation implements Translation {
56+
private final String key;
57+
private final Function<Locale, ResourceBundle> bundleFunction;
58+
59+
public ResourceBundleTranslation(String key, Function<Locale, ResourceBundle> bundleFunction) {
60+
this.key = key;
61+
this.bundleFunction = bundleFunction;
62+
}
63+
64+
65+
@Override
66+
public String getId() {
67+
return this.key;
68+
}
69+
70+
@Override
71+
public String getName() {
72+
return this.key;
73+
}
74+
75+
@Override
76+
public String get(Locale locale) {
77+
Preconditions.checkNotNull(locale, "locale");
78+
ResourceBundle bundle = this.bundleFunction.apply(locale);
79+
return bundle == null ? this.key : bundle.getString(this.key);
80+
}
81+
82+
@Override
83+
public String get(Locale locale, Object... args) {
84+
return String.format(locale, get(locale), args);
85+
}
86+
}

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 renamed to src/main/java/org/spongepowered/api/util/SpongeApiTranslationHelper.java

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,43 @@
2222
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2323
* THE SOFTWARE.
2424
*/
25-
@org.spongepowered.api.util.annotation.NonnullByDefault package org.spongepowered.api.text.translation.locale;
25+
package org.spongepowered.api.util;
26+
27+
import com.google.common.base.Function;
28+
import org.spongepowered.api.text.Text;
29+
import org.spongepowered.api.text.Texts;
30+
import org.spongepowered.api.text.translation.ResourceBundleTranslation;
31+
32+
import java.util.Locale;
33+
import java.util.ResourceBundle;
34+
35+
import javax.annotation.Nullable;
36+
37+
/**
38+
* This class provides translations for strings within SpongeAPI. Plugins should consult an implementaion of Translation for help.
39+
*/
40+
public class SpongeApiTranslationHelper {
41+
42+
private static final Function<Locale, ResourceBundle> LOOKUP_FUNC = new Function<Locale, ResourceBundle>() {
43+
@Nullable
44+
@Override
45+
public ResourceBundle apply(Locale input) {
46+
return ResourceBundle.getBundle("org.spongepowered.api.Translations", input);
47+
}
48+
};
49+
50+
private SpongeApiTranslationHelper() {
51+
} // Prevent instance creation
52+
53+
/**
54+
* Get the translated text for a given string.
55+
*
56+
* @param key The translation key
57+
* @param args Translation parameters
58+
* @return The translatable text
59+
*/
60+
public static Text t(String key, Object... args) {
61+
return Texts.of(new ResourceBundleTranslation(key, LOOKUP_FUNC), args);
62+
}
63+
64+
}

0 commit comments

Comments
 (0)