Implementation principle of i18n-gettext

Source: Internet
Author: User
Tags i18n
[Newedit] Implementation of i18n (III)-implementation principle of gettext

 

Http://blog.donews.com/limodou/archive/2004/06/15/28961.aspx

 

Appendix: implementation principle of gettext

It's just my understanding!

The gettext function is actually quite simple: define and install the _ () function, obtain the translation from the mo file based on the input translation items, and process the Unicode code of the application according to the uniocde identifier.

A gettext object provides two basic functions for obtaining the translation: gettext and ugettext. One is used to obtain a translation that has not been converted into a Unicode character. According to the Unicode flag, one of these two functions will be referenced to the _ () function. If no translation is installed, only the translation items are returned by default. Therefore, you can use gettext without installing any translations.

How to install _().

Import _ buildin __
_ Buildin _. _ dict _ ['_'] = Unicode and self. ugettext or self. gettext

In this way, the _ () function is converted into a built-in function, which can be directly used in other subsequent modules. Therefore, when using gettext, the installation should be very advanced, and you only need to install it in the startup module.

Postscript: Why does newedit not dynamically update the interface?

Since international processing is implemented, is it better if the user changes the language used and the entire application changes immediately? Of course it is very attractive, but there is a problem with newedit. On the one hand, all interfaces related to international processing should be updated with code, resulting in heavy workload. In addition, unless tested, a user does not always switch between different languages, and generally uses a fixed language. Therefore, enabling new language settings after a restart is acceptable. Another important reason is that the _ () method is only applicable to the function call method and is powerless to global variables or module variables. Why? For example:

Message = _ ('China ')

This defines a variable. If a module exists as a global variable of the module, the statement is executed when the module is imported. The import process is executed only once. In this way, after the statement is executed, the message is the specific value, instead of the message = _ ('China') statement. In this way, when we change the language, the message will only be the translation of the previous language and will not become a new translation. This is really difficult to solve. Why is it possible for function calls? Strictly speaking, it should be in the following form:

Call (_ ('China '))

This is because the parameters are dynamically calculated when a function is called. Therefore, the parameter value is recalculated every time a call is called. Therefore, this method is acceptable. In general, if the _ () function can be called every time during execution, dynamic language switching can be realized. If not, use the static language switch. Therefore, newedit uses static language switching.

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.