# Using Lua for configuration and localization data

Since my game is already going to be scripted with Lua, I've decided to make all my configuration and localization data defined using Lua tables. For example, this could be a basic locale file:

-- en-us.lua
text = {
error = {
sdl = "Error initializing SDL",
opengl = "Error initializing OpenGL",
-- ...
},
offline = "Play solo",
online = "Play co-op",
quit = "Quit",
-- ...
},
-- ...
}


Different locales and profiles can be loaded simply by choosing the right file, for example between en-us.lua and fr.lua.

Because the table is in global scope, it's easy for other Lua code to reference. If a script needed to access a configuration variable or throw an error, it would just have to look up the value using something like text.error.opengl or config.graphics.fullscreen.

The difficulties come when native code (C) has to work with these values. There are basically two options that I can think of:

1. The tables are loaded into client code as a tree. It's easy to get and set values on the client side, but changes on either side need to be synchronized.

2. The native code does no caching and instead directly reads from/writes to the Lua state each time. Possible performance issues?

What's the best way to handle this from the native side?

I highly recommend you implement it using the gettext system. It is widely ported and very well supported, for example having lots of tools that support its workflow.

One workflow is to add gettext calls in your code like this:

printf(_("My name is %s.\n"), my_name);


Which you can use xgettext to generate a .pot file. Using this file, translators generate a language-specific .po file, which they can fill like this (everything except the msgstr is generated):

#: src/name.c:36
msgid "My name is %s.\n"
msgstr "Je m'appelle %s.\n"


As I've said, this system is very well supported. I haven't used it in Lua, but Wikipedia states that there is a Lua implementation.

• Not quite what I'm looking for. For one, this only works as far as localization goes. Secondly, it's under a copyleft license. – jmegaffin Feb 24 '14 at 2:48
• @Boreal about the licensing, the only libraries you need to distribute are LGPL licensed, which is very easy to comply with since they do not require you open your source. As for configurations, I think that can be covered in a separate question. – congusbongus Feb 24 '14 at 3:25

I wrote an open-source library that does that. It provides an easy access to Lua data from C++ code, while providing a set of interesting features like data hot reloading and late binding. It is not limited to strings, too.