Skip to content

Add text translatable fallback method #981

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

EvanHsieh0415
Copy link
Contributor

Description

Add .fallback(String text) for Text.translate()

Example Script

Text.translate("text.kubejs.foo").fallback("FooBar");
Text.translatable("text.kubejs.foo").fallback("FooBar");

Other details

This is my first submission, so I may need more suggestions to improve this feature.

@EvanHsieh0415 EvanHsieh0415 changed the title add .fallback(String text) for translatable .fallback(String text) for Text Translatable Mar 25, 2025
@EvanHsieh0415 EvanHsieh0415 changed the title .fallback(String text) for Text Translatable Add text translatable fallback method Mar 25, 2025
Copy link
Contributor

@ChiefArug ChiefArug left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does not compile. Please test your changes before making a PR, using the GitHub web editor to make changes isn't a great idea because you cannot test it there (and it doesn't show syntax errors).

@ChiefArug
Copy link
Contributor

I'm not sure I like the idea of adding this to all components, as it is only usable for translatable components and not literal/keybind/nbt/score ect components.

@EvanHsieh0415
Copy link
Contributor Author

This feature still needs more corrections, I will continue to study how to make this feature better

@EvanHsieh0415
Copy link
Contributor Author

EvanHsieh0415 commented Mar 26, 2025

which one is better?

  1. replace from

    @Info("Returns a translatable component of the input key, with args of the objects")
    static MutableComponent translate(String key, @Nullable String fallback, Object... objects) {
        return Component.translatableWithFallback(key, fallback, objects);
    }

    to

    @Info("Returns a translatable component of the input key, with args of the objects")
    static MutableComponent translate(String key, Object... objects) {
        return Component.translatable(key, objects);
    }

    example:

    Text.translate("text.kubejs.foo", "FooBar", 1, 2, 3); // with fallback("FooBar")
    Text.translate("text.kubejs.foo", null, 1, 2, 3); // no fallback

    But add null to the front of each object.

    - Text.translate("text.kubejs.foo", 1, 2, 3);
    + Text.translate("text.kubejs.foo", null, 1, 2, 3);
  2. add a new one

    @Info("Returns a translatable component of the input key, with args of the objects")
    static MutableComponent translate(String key, @nullable String fallback, Object... objects) {
        return Component.translatableWithFallback(key, fallback, objects);
    }

    example:

    Text.translate("text.kubejs.foo", "FooBar", 1, 2, 3); // with fallback("FooBar") and objects(1, 2, 3)
    Text.translate("text.kubejs.foo", null, "FooBar", 1, 2, 3); // no fallback but with objects("FooBar", 1, 2, 3)
    Text.translate("text.kubejs.foo", 1, 2, 3); // no fallback

    But you have to add the null parameter to the front of each call where the first object is a String.

    Text.translate("text.kubejs.foo", 1, 2, 3); // Not need to change
    - Text.translate("text.kubejs.foo", "FooBar", 2, 3);
    + Text.translate("text.kubejs.foo", null, "FooBar", 2, 3);

@ChiefArug
Copy link
Contributor

I would opt to add a new method with a different name to prevent ambiguity issues. I.E. translatableWithFallback.

@EvanHsieh0415 EvanHsieh0415 marked this pull request as draft March 27, 2025 05:55
@EvanHsieh0415
Copy link
Contributor Author

圖片
It's working well.

@EvanHsieh0415 EvanHsieh0415 requested a review from ChiefArug March 29, 2025 01:43
Copy link
Contributor

@ChiefArug ChiefArug left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good!

@EvanHsieh0415 EvanHsieh0415 marked this pull request as ready for review March 29, 2025 16:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants