GithubHelp home page GithubHelp logo

Comments (7)

wizjany avatar wizjany commented on July 2, 2024 4

Use a translatable component? then you can use the friendly renderer and provider a translation function that replaces placeholders with values.

from adventure.

Scarsz avatar Scarsz commented on July 2, 2024 3

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.

Electroid avatar Electroid commented on July 2, 2024 1

I've added TextComponent#replace method that should allow for regex search and replacement. c6acc3c

from adventure.

Vankka avatar Vankka commented on July 2, 2024

Converting color codes at the same time isn't really possible as far as I can tell

from adventure.

Vankka avatar Vankka commented on July 2, 2024

@lucko any response other than that one reaction? Is it going to get added eventually?

from adventure.

liach avatar liach commented on July 2, 2024

Converting color codes

Use textformat man

from adventure.

Scarsz avatar Scarsz commented on July 2, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.