Minecraft Modding: Localization

Lang File Key List


In recent versions of Forge, localization is done with the .lang file which maps unlocalized names to your language. For example, the English names would be in the en_US.lang file.

Tip: The various .lang files should be in a package named assets.<modid>.lang (where you replace <modid> with the actual mod id for your mod.

Warning: Make sure you capitalize the file name properly: en_US.lang is correct, en_us.lang is wrong. This can be hard to debug because for some reason in Eclipse if it is not capitalized right it will still work, but it will fail in the exported JAR.

Warning: If you save the .lang file as UTF-8 make sure you choose option to save with BOM (byte-order mapping) or it won't be read properly.

In the .lang file, the unlocalized names are converted to keys by prepending the type (e.g. "item." or "tile.") to the front and in some cases (it is kind of inconsistent) postpending ".name" to the end.

Here are examples of the ones I'm aware of (replace <unlocalizedname> with unlocalized name that makes sense in your mob:
  • item.<unlocalizedname>.name for items
  • tile.<unlocalizedname>.name for blocks and ores
  • achievement.<unlocalizedname> for achievements
  • achievement.<unlocalizedname>.desc for achievement descriptions
  • entity.<unlocalizedname>.name for entities (not sure why but sometimes it seems to want entity.<modid>.<unlocalizedname>.name
  • death.attack.<unlocalizedname> for what it says if you're killed by specified entity
  • itemGroup.<unlocalizedname> for name of creative tab
  • container.<unlocalizedname> for name of container
  • potion.<unlocalizedname> for name of potion effect
  • potion.<unlocalizedname>.postfix for name of potion (e.g. "Potion of Coolness")
  • potion.prefix.<unlocalizedname> for type of potion (e.g. "Splash")
Warning: For some reason the .lang file is strict about the assignment operator -- you cannot have any space after the equals sign.  (But you can have spaces in the text that follows.)

Here is an example of one of my en_US.lang files:

entity.tiger.name=Tiger
entity.maneatingtiger.name=Man-Eating Tiger
entity.lion.name=Lion
entity.jaguar.name=Jaguar
entity.lynx.name=Lynx
entity.python.name=Python
entity.elephant.name=Elephant

tile.tomatoes.name=Tomatoes
tile.blueberries.name=Blueberries
tile.stove.name=Stove
tile.stove_lit.name=Stove
tile.pantry.name=Pantry

item.tomato.name=Tomato
item.blueberry.name=Blueberry

itemGroup.Recipe+=Recipe+

container.stove=Stove
container.pantry=Pantry


Making A Lang File That Contains Escape Characters


Thanks to diesieben07 for this tip.

In strings, sometimes you want to include special things like newline or color codes. These are usually represented by some character that is unlikely to be in a real string followed by a code. These are called "escape characters".

By default, FML will ignore escape characters in the lang file. However, it is possible to make FML process them by marking your language file as "special" by placing "#PARSE_ESCAPES" in the first line of the file. FML will then load the language file as if it were a standard Java properties file.

Checking What Language Is Currently Loaded From Client Side


Thanks to diesieben07 for this tip.

The game may be played in various languages (called "localization"), and if you want your mod to be international you might want any text resources to have translations.  Many text fields (like names of entities) are already translated via the .lang file mechanism, but there may be situations where you want to process the localization yourself.  Since languages are related to GUI, the language is a client-side value.

You can check the current language using the following (client-side!) method:

Minecraft.getMinecraft().getLanguageManager().getCurrentLanguage()

If you're worried about the language changing during game play (a rare occurrence as most people would install Minecraft with language of choice) you can either poll the above method or you can use the IResourceManagerReloadListener interface to detect when the ResourceManager reloads (that happens when the language is changed, too).

Checking What Client Language Is From Server Side


If you're making a server-only mod, you may run into the situation where you want to send messages to the client but since the mod is not installed on the client you can't rely on the normal chat translation methods.

Apparently (I haven't tried this out), EntityPlayerMP has private String translator field (that gets set during the player joining with the C15PacketClientSettings packet, with values like "en_US";

It's private so you'd need to use Java reflection to get it, there's no getter method for it.

2 comments:

  1. In 1.11.2, use en_us.lang instead of en_US.lang!!!
    It took me hours to figure it out!

    ReplyDelete
    Replies
    1. thx i did the same before your comment.

      Delete