Jump to content

[1.16.4] get TranslationTextComponent from JSON with custom keys


than00ber1
 Share

Recommended Posts

I've recently moved from 1.12.2 and I am having some trouble getting translations from the JSON lang files.

Getting translations for items/blocks or enchantments work perfectly but not with custom keys.

 

I am trying to create translatable components for configs:

 

In the lang JSON file I created an entry like this,

 

"config.<config_name>" : "Some english translation text..."

 

and within the code I call it like this:

 

private static String fromConfigKey(String key) {
	return new TranslationTextComponent("config." + key).getUnformattedComponentText();
}

 

but returns an empty string.

 

I have noticed that TranslationTextComponent#getUnformattedComponentText returns an empty string by default, is this something new? Do I have to create my own sub class of TranslationTextComponent which implements this one way or another? 

Link to comment
Share on other sites

Ok. I've tried this using ITextComponent instead:

 

private static ITextComponent fromConfigKey(String key) {
	return ITextComponent.getTextComponentOrEmpty("config." + key);
}

 

This returns the key and not its translatation. I get the same result when calling getString or getUnformattedComponentText.

 

Maybe this might help too. Heres how I try to register a config:

 

public class Configs {

	...

    public static ForgeConfigSpec.EnumValue<CarveType> CARVE_TYPE;

    public Configs(ForgeConfigSpec.Builder forgeConfigBuilder) {
        ConfigBuilder builder = new ConfigBuilder(forgeConfigBuilder);

        CARVE_TYPE = builder.defineEnum("carve_type", CarveType.CONNECTED, CarveType.values());
    }
      
    private static class ConfigBuilder {
      
      	private static ForgeConfigSpec.Builder BUILDER;

        private ConfigBuilder(ForgeConfigSpec.Builder builder) {
            BUILDER = builder;
        }
      
      	...
      
        public final <V extends Enum<V>> ForgeConfigSpec.EnumValue<V> defineEnum(String key, V defaultValue, V... enums) {
            ITextComponent note = noteFromConfigKey(key);
            return !note.getString().equals("")
                    ? BUILDER.comment(fromConfigKey(key).getString(), note.getString()).defineEnum(key, defaultValue, enums)
                    : BUILDER.comment(fromConfigKey(key).getString()).defineEnum(key, defaultValue, enums);
        }
      
      	private static ITextComponent fromConfigKey(String key) {
      		return ITextComponent.getTextComponentOrEmpty("config." + key);
      	}
      
      	private static ITextComponent noteFromConfigKey(String key) {
            return fromConfigKey(key + ".note");
        }
	}

    public enum CarveType {
        CONNECTED, ALL
    }
}

 

This results in a config file like this:

 

#config.carve_type
#config.carve_type.note
#Allowed Values: CONNECTED, ALL
carve_type = "CONNECTED"

 

and my en_us.json:

 

{
	"config.carve_type": "Block carve type [...] "
}

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.