Localization support and some more cool stuff!

// June 19th, 2010 // Uncategorized

A feature request made Nifty user lynorics create a patch that solved the issue and it directly point to solve the localization feature request too :)

Every attribute of every Nifty element can now contain a special markup “${…}” that gets replaced with something else when the xml is loaded.

Example:

<text text="your home directory: ${ENV.HOME}" />

The “${ENV.HOME}” will be replaced with your $HOME environment variable! :D

You have the following options now to use this new feature:

  • ${id.key} lookup resource bundle with “id” and request “key” from it. This is explained in more detail below.
  • ${ENV.key} lookup “key” in the environment variables (System.getEnv()) and replace ${ENV.key} with the value received.
  • ${PROP.key} lookup “key” in the new Nifty.setGlobalProperties(Properties) properties or if the properties are not set this will use System.getProperties() to lookup “key”
  • ${CALL.method()} call method() at the current ScreenController and it is replaced with the value that method() returns. method() should return a String in this case.

If for some reason the replacement does not work out then nothing is replaced and you’ll get the original ${…} String back.

Localization Details

Currently Nifty Localization is using standard Property file based Resourcebundles. This simply means you can create a property file containing keys that are referenced from the xml files.

Example:

dialog.properties:
hello = Hello World in Default Language

dialog_de.properties:
hello = Hallo Welt in Deutsch

dialog_en.properties:
hello = hello world in english

Once you have created these files you need to register the resourceBundle “dialog” with Nifty so that Nifty knows that it exists. You can do that with the new “resourceBundle” tag:

<resourceBundle id="dialog" filename="src/main/resources/dialog" />
 

Now that Nifty knows about your resourceBundle you can access it with the method mentioned above:

<text text="${dialog.hello}" />
 

Now Nifty will use the current set default locale to access the ResourceBundle with the id “dialog” and looks up the value for “hello”. If you don’t like that Nifty uses the default Locale you can set the Locale that Nifty should use with the “nifty.setLocale(Locale)” method.

Nifty stuff and Kudos goes out to lynorics! :D

void

7 Responses to “Localization support and some more cool stuff!”

  1. Jattra says:

    Good work.
    I am going to rework my multi-lang menu to be done in proper way.
    Grrr. Hurray!

  2. Tumaini says:

    Very nice!
    I just started converting our game to use Nifty instead of GBUI and having done some Android work lately, this was just what I was looking for!
    Thank you very much!

  3. Loki says:

    Unfortunately, Nifty GUI do not support Chinese.

  4. Loki says:

    Nifty GUI with Chinese problems
    1) When the font of text field is Chinese, then you cannot input for the text field.
    2) When integrate with jME, cannot show Chinese rightly, just can show the words in the first page of the font.

Leave a Reply