question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Sort of a String#replace for working with Components

See original GitHub issue

I’m looking for a way to take a config value like %displayname%: %message% and replace %displayname% and %message% with components (and somehow probably also parse & color codes at the same time (ex. %displayname%&r: %message%))

Example: (simplified for demonstration purposes, in the real world the replaced components could have click & hover events and wouldn’t just be a simple text component with just content)

Component comp = TextComponent.of("%displayname%: %message%")
    .replace("%displayname", TextComponent.of("Steve"))
    .replace("%message%", TextComponent.of("Hello World!"));

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:4
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
Scarszcommented, Sep 26, 2019

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")
        )
        ...
);
3reactions
wizjanycommented, Jul 14, 2019

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

Sort of a String#replace for working with Components #38
I'm looking for a way to take a config value like %displayname%: %message% and replace %displayname% and %message% with components (and somehow probably ......
Read more >
Safe/Efficient to use string.replace() with styled components?
To clarify, I do have it working: const replaceStringWithTheme = (string, theme) => { const modifiedString = string .replace("primary", ...
Read more >
String.Replace Method (System)
Returns a new string in which all occurrences of a specified Unicode character or String in the current string are replaced with another...
Read more >
Java String replace() method
The Java String class replace() method returns a string replacing all the old char or CharSequence to new char or CharSequence. Since JDK...
Read more >
Python String | replace()
The replace() in Python returns a copy of the string where all occurrences of a substring are replaced with another substring. Syntax of...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found