Comments (7)
Use a translatable component? then you can use the friendly renderer and provider a translation function that replaces placeholders with values.
from adventure.
https://github.com/KyoriPowered/text/blob/master/api/src/test/java/net/kyori/text/TranslatableComponentTest.java is as about as good of an example I can find for this. (thanks for the pointer, @wizjany)
var renderer = FriendlyComponentRenderer.from((locale, key) ->
new MessageFormat("{0} and {1} are cats."));
renderer.render(
TranslatableComponent
...
.args(
TextComponent.of("kashike"),
TextComponent.of("lucko")
)
...
);
I'm not really a fan of using the argument index to do the substitution though... I can already see users messing it all up seeing as these messages will be user-configured. Is there anything comparable to this but with named arguments or will this need to be done by the implementing library? I feel like this would be a useful addition if there isn't something already.
var renderer = FriendlyComponentRenderer.from((locale, key) ->
new MessageFormat("{thing1} and {thing2} are cats."));
renderer.render(
TranslatableComponent
...
.args(
"thing1", TextComponent.of("kashike"),
"thing2", TextComponent.of("lucko")
)
...
);
from adventure.
I've added TextComponent#replace
method that should allow for regex search and replacement. c6acc3c
from adventure.
Converting color codes at the same time isn't really possible as far as I can tell
from adventure.
@lucko any response other than that one reaction? Is it going to get added eventually?
from adventure.
Converting color codes
Use textformat man
from adventure.
This ended up being more of a pain to work around than expected. Because Kyori uses Java's MessageFormat, named arguments aren't easily possible because MessageFormat itself doesn't support named arguments.
I worked around this by keeping track of the order of named arguments given through the definition and sending the indexed version to the MessageFormat constructor. When the rendered component is needed, it replaces the indexed MessageFormat's parameters by matching the indexes to the supplied argument array indexes.
Definitely not the cleanest solution, but seems to work well enough for my purposes. Can't give an easy to look at example because I worked the implementation into my plugin's code but you can probably get an understanding of it easily enough. https://github.com/DiscordSRV/Iris/blob/a1a80daed915051050dec7fcafd356d072bf1023/discordsrv-common/src/main/java/com/discordsrv/common/Text.java#L86-L165
Definition KYORI_TEST_INDEX_SUBSTITUTION = new Definition("kyori.test.1",
"{0} interacted with {1}");
Definition KYORI_TEST_NAME_SUBSTITUTION = new Definition("kyori.test.2",
"{the first person} interacted with {the second person}");
...
@Test
public void name() {
String testOneRendered = Text.asPlain(Text.KYORI_TEST_INDEX_SUBSTITUTION.render("Person 1", TextComponent.of("Person 2")));
String testTwoRendered = Text.asPlain(Text.KYORI_TEST_NAME_SUBSTITUTION.renderNamedArgs(
"the first person", "Person 1",
"the second person", TextComponent.of("Person 2")
));
System.out.println("Index-based substitution: " + Text.KYORI_TEST_INDEX_SUBSTITUTION.getRawFormat() + "\n-> " + testOneRendered);
System.out.println("Name-based substitution: " + Text.KYORI_TEST_NAME_SUBSTITUTION.getRawFormat() + "\n-> " + testTwoRendered);
Assert.assertEquals(testOneRendered, testTwoRendered);
System.out.println("Results match");
}
Index-based substitution: {0} interacted with {1}
-> Person 1 interacted with Person 2
Name-based substitution: {the first person} interacted with {the second person}
-> Person 1 interacted with Person 2
Results match
from adventure.
Related Issues (20)
- `fastjson2` component serializer HOT 1
- MM rainbow phase problems
- LegacyComponentSerializer does not deserialize components properly if formatting code char is capitalized HOT 4
- Gradient tags in combination with length modifying tags
- Support new item hover format in 24w09+
- Particle Interfaces?
- Phase issues with <gradient> and more than 2 colors HOT 4
- BinaryTagIO.Reader & BinaryTagIO.Writer other tag support HOT 1
- Make `BinaryTagType.binaryTagType(int)` public HOT 1
- Emptying BossBar flags always calls bossBarFlagsChanged, even if nothing was changed
- Filtering tag provider
- Generic character mapping MiniMessage tag (for small caps)
- Default value for unresolved tags
- API for "caching" MiniMessage strings (or other serializer specific data) for ease of sending
- `CompoundBinaryTag#getBoolean` with default is incorrect
- `ListBinaryTag` is weakly immutable HOT 3
- NPE while sending a resource pack with no prompt HOT 1
- Adventure weighs hue too heavily when comparing colors HOT 3
- MiniMessage strips "redundant" tags which does not allow me to set prefixes as I can with legacy color codes HOT 1
- show_item client safety HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from adventure.