1
\$\begingroup\$

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",
        resource = "Error loading resource $",
        -- ...
    },
    menu = {
        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?

\$\endgroup\$
1
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ Not quite what I'm looking for. For one, this only works as far as localization goes. Secondly, it's under a copyleft license. \$\endgroup\$ – jmegaffin Feb 24 '14 at 2:48
  • \$\begingroup\$ @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. \$\endgroup\$ – congusbongus Feb 24 '14 at 3:25
0
\$\begingroup\$

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.

Here it is: https://github.com/tynril/luadata/

I've used it for a few personal projects, but I'm not aware of other people using it, which means there might be some bugs here and there, but as far as I've used it, it is pretty stable.

Sorry for the blatant advertising, but it seems to answer your question, and I don't profit from it anyway.

\$\endgroup\$
  • \$\begingroup\$ It looks interesting, but I'm using C, not C++. \$\endgroup\$ – jmegaffin Feb 25 '14 at 21:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.