Localisation

Is there an elegant way to deal with multiple languages and fonts in Defold? It’s sometimes not ideal to include all chars in one font when dealing with multiple languages, each with many glyphs.

Ideally I’d like to change which font is used in a GUI at runtime, but I don’t think this is possible?

49

Is the only option, then, to map all text nodes in all GUIs and manually use set_font to change the font?

The only other option is to use native text rendering https://github.com/Lerg/extension-nativetext

Thanks @sergey.lerg. The project I’m working on is HTML5, and doesn’t look supported? In any case, this is great to know for future projects, nice one.

Yup, no idea how to render native text in HTML5.

You want to scan your text and only include the glyphs in the fonts you actually are using in text.

Since text glyphs are streamed in as they are actually needed having multiple fonts set in your GUI is not actually an issue.

Check https://github.com/subsoap/defglot for some useful functions.

3 Likes

Yes, I was planning on doing this anyway, just to minimise weight. HTML5, it’s like being back to a 2.4k modem. :slight_smile:

1 Like

BUMPing this topic.

I’m going to have to localise my game in languages that have foreign characters (russian/japanese).
All my text nodes are currently with a fond that only support traditional alphanumeric characters.
What’s the best practice at this point to localise the game? I’d rather avoid to duplicate all text nodes…

My recommendation:

  • Get the system language using sys.get_sys_info().language
  • Store all strings in the game in one Lua table per language, each string keyed on an id:
local strings = {
	en = {
		menu_newgame = "NEW GAME",
	},
	ru = {
		menu_newgame = "НОВАЯ ИГРА",
	},
	es = {
		...
	},
	...
}
  • Look up the string to use based on the language and key
  • Set the string using label.set_text(label_url, text) or gui.set_text(node, text)
  • Change font depending on language. It is quite likely that you’re able to find a font which contains all latin characters to handle English, German, Spanish etc, another font for Cyrillic, and one for Japanese etc
    • OR combine all font glyphs for all supported languages in a single ttf/otf file, in which case you can skip this step completely
    • For gui scenes you add all fonts to the scene and use gui.set_font(label, font)
    • For labels store the fonts as go.property("main_ru", go.property("/assets/fonts/main_ru.font")) and use go.set(label_url, "font", font) to change font
5 Likes

Can be done with FontForge. Open base font. Element → Merge Fonts. Test saying no and if that doesn’t work well restart and say yes. Repeat for any languages you want. Then File → Generate Fonts to get new font of merged glyphs.

You can then extract unique characters from your game and use that char list in your .font to only include glyphs that are actually used in game. This thread mentions some methods Scan text for unique characters (for Chinese, Japanese, Korean fonts)

2 Likes