Using GetText to implement multilanguage support in Lua

Source: Internet
Author: User
Tags gettext i18n lua parse error

Using GetText to implement multilanguage support in Lua

The GNU GetText is an excellent international tool. is used heavily in Linux. WordPress also uses GetText to implement multi-language support.

This article describes how to use GetText long language support in Quick-cocos2d-x. At the same time, the multilingual translation tool Poedit support to the Lua language is presented. GetText Introduction

GetText is the name of a set of toolset. This set of Toolset contains a set of tools for building templates (POT), creating PO files, and compiling MO files, such as XGETTEXT/MSGINIT/MSGFMT.

GetText contains tools such as: Xgettext generated from the source pot template or directly generated PO file; msginit based on pot file to generate the Po file to be translated; msgfmt compile the Po file into a binary mo file; msgunfmt as the name suggests, Decompile the mo file into a PO file, msgmerge the pot template file with the Po file. Used to update the new content to be translated into the translated Po file after the source update, Msgcat to merge multiple PO files together.

The use of gettext needs to involve several concepts: source
Program source code, this is the Lua file; POT file
The original language is based on the natural language used in the source string and is recommended for use in English. Plain text format; PO file
A variety of language versions of documents to be translated according to the POT file, including the original language and the translated target language. Plain text format; MO file
The files that are actually used by the final software are compiled using PO. binary format.

The general workflow is this: in the source code using the contract syntax to write the string, C language default is GetText ("my Text"). In this article, you will use _ ("my Text"), use Xgettext to scan from source code for text that needs to be translated, create POT files, and use the Msginit command to create PO files based on POT files. or directly in the previous step can also directly establish a PO file; manual translation (also can be machine translation), the results of the translation saved in the PO file; Use the MSGFMT command to compile the Po file into a MO file; In the program to implement the call command, this article is the _ function, this function will read and parse MO file, which returns the translated text according to the original language text of the call. Poedit Introduction

The toolset provided by GetText is command-line based, and some programmers are not accustomed to the command line. It also does not provide a control translation tool for translation workers.

and Poedit provides everything.

Poedit can extract text from the source to generate PO files, but also provides a GUI interface for contrast translation. It can also generate the final MO file directly.

Of course, after the GUI, it is still handled using GetText, but the specifics are hidden. generate and translate PO files using Poedit Software Settings

1. Open Poedit, execute file–preferences command, and fill in your personal information.

2. Switch to Editor tab and select automatically compile. mo file on save and show summary after catalog update two.

3. Switch to Parsers tab, which provides several source parsers, but the default is no LUA. We will join LUA source resolution below.

4. Click New to create a new source parser to set the following figure.

Xgettext-c--force-po-o%o%c%k%F

-k%k

--from-code=%c

These settings are basically consistent with the C + + settings, except that the extension support and the Parser command field are modified. In the parameters of the Xgettext command,-C is the shorthand for –language=c.

Next, you will create a new project to set up. Project Settings

1. Execute file–new catelog ... command, add your own information to the Translation Properties tab. The Language option here is just a display option, and it doesn't determine what language you're using, and it's not related to the parsers in the software setup just now.

2. Set the source code path, this is very important.

I use the quick default directory style, all the source is placed in the scripts. I also set up a new i18n directory, which is similar to the scripts directory. The generated PO file will be saved in this directory.

For the PO file currently being edited, Base path should be set to ... \scripts, the following Paths should be added. This path. Note that under Windows, the path separator for Base path must be in Windows format (backslash \) and should not be used/.

3. Set Source code Keywords

We have already identified their own keywords, that is _. Of course we can also use the C language default keyword GetText, but that would be a bit longer.

4. After setting, save the project as I18N/ZH_CN.PO, please note the 2nd step I have stressed above. Parse Source

After the success of all settings, directly press the main interface of the Update button to resolve the source code. Poedit will be parsed in the source code to use _ ("my text") format of the keywords, the string extraction process is displayed in the main interface.

We can translate the key words in the main interface.

If the source code has an update, click the Update button again to resolve the source code again. This is if you parse to a new string, or if a string is no longer in use, Poedit will be prompted.


One thing to be aware of is that you do not include half-width single quotes in Lua annotations.

This is because LUA's annotations use the-symbol, and the C language uses//, so GetText thinks that annotations in Lua are part of the code without ignoring parsing. There is no problem with the general situation, but if you include a half-width single quotation mark in the note (which is the literal value in the C language), GetText will consider the syntax error to result in a parse error. Generate MO file

In the software setup, we set up to save the project automatically generated MO file, so don't worry about it. The generated MO file is in the same directory as the PO file. parsing MO files in Lua

MO is a binary format file, and our program, when it reads it, needs to analyze its structure and parse it into the original text-the key-value pair form of the translated text for our use.

Many languages already contain parsing libraries for MO files, but Lua does not. So we need to write one ourselves.

But J.j?rgen von Bargen has done this, and I've simply encapsulated his code and put it in my Lua library, the name is Utils. Gettext.

To support cross-platform reasons, reading MO files is ccfileutils and can be used instead of an IO library if you want to use them in other environments. I kept the IO library code in Gettext._getfiledata and just commented on it. Integrated Paradigm

Using this method, the MO file is parsed successfully and a table returns:

Local Mo_data=assert (Require ("Utils"). Gettext "). Loadmofromfile (" Res/zh_cn.mo "))
print (mo_data[" Hello world! "])
-Hello, World
print (mo_data["Foobar"])
--Nil

The more common approach is this:

_ = Assert (Require ("Utils"). Gettext "). Gettextfromfile (" Res/zh_cn.mo "))
Print (_ (" Hello world! ")

In my game, this is the way it is used:

LANG = "Res/zh_cn.mo"
Function _ (__text) return
    __text
end
If Ccfileutils:sharedfileutils (): Isfileexist (LANG) Then
    _ = Assert (Require ("Utils. Gettext "). Gettextfromfile (LANG)) End



The site is reproduced in addition to the article, are original site or translation. This site welcomes the various forms of reprint, but please 18 years of age above the reprint to indicate the source of the article, respect for my labor, but also respect your IQ; This site part of the original and translation articles to provide markdown format source code, welcome to use the article source to reprint; Article title: Using GetText to implement multilanguage support in Lua this article links: http://zengrong.net/post/1986.htm

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.