Use printf to print text in Indian language

Gourish Singla
4 min readOct 31, 2021

Aim

To generalize or to translate text in some way, such that, when a user selects a particular locale, the application switches its strings/output to the language described by that locale. For example if I select a locale hi_IN (hi->Hindi; IN->India), the output/strings of this application should be displayed in Hindi.

This is done using the concept of a Message Catalog, which is a database of strings in some file. Gettext supports Message Catalogs in the form of MO files (.mo). These MO files are binary files which store strings for different applications for different locales. When an application runs, all the strings are extracted from application’s MO file(s) based on a certain locale. This extraction at run-time is done using gettext functions described in gettext framework.

STEP-1

Write the program below, in which gettext(“c format string”) function has the responsibility of extracting (for “How are you?\n” in English) an equivalent translated string(“आप कैसे हैं?\n” in Hindi), at runtime, in local script of the language describe by a locale (e.g. hi_IN for Hindi belonging to country India). The general practice is to use macros like _(“String”) instead of gettext(“String”) in order to save number of letters we type in.

STEP-2

Create a new directory named po/

Extract the strings in a POT (test.pot) file.

STEP-3

Change directory to po/

A new file ‘test.pot’ will be created in it.

pot (portable object template) extensions for user interface text translation files. These use the GNU Gettext format. . pot files are templates that contain source text.

STEP-4

This POT file is copied as PO (.po) file in respective language folder (hi/) and then translated. Corresponding to every string adjacent to ‘msgid’ there is a translated string (in local script), adjacent to ‘msgstr’.

The core of each PO file is made of pairs of the source text (that which is found in the code) and target text (the translated text).

Msgid “How are you?”
msgstr “आप कैसे हैं?”

A directory with the name of your language is created (in this case hindi).

This language name should be probably a 2-digit/3-digit code listed for your language in ISO 639–1.

  1. http://www.loc.gov/standards/iso639-2/php/code_list.php for reference.

STEP-5

Make the necessary changes to the test.po file.

STEP-6

Create an MO (.mo) file. à msgfmt test.po –o test.mo

In root mode copy the MO file to /usr/share/locale/hi/LC_MESSAGES.

STEP-7

Compile the test.c file.

Now if the object file test is run, it displays the simple English

STEP-8

The respective environment variables (LANG,LC_ALL) are required to be set to the lacale for hindi language.

— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — -

--

--

Gourish Singla
0 Followers

B.E. Computer Engineering @T.I.E.T.