1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
|
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" href="highlight.min.css">
<script src="highlight.min.js"></script><script>
hljs.configure({languages: ['cpp']});
hljs.highlightAll();
</script><title>Chapter 27. Internationalization and Localization</title>
<link rel="stylesheet" type="text/css" href="style.css">
<meta name="generator" content="DocBook XSL Stylesheets Vsnapshot">
<link rel="home" href="index.html" title="Programming with gtkmm 4">
<link rel="up" href="index.html" title="Programming with gtkmm 4">
<link rel="prev" href="sec-builder-using-derived-widgets.html" title="Using derived widgets">
<link rel="next" href="sec-i18n-marking-strings.html" title="Marking strings for translation">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<div class="navheader">
<table width="100%" summary="Navigation header">
<tr><th colspan="3" align="center">Chapter 27. Internationalization and Localization</th></tr>
<tr>
<td width="20%" align="left">
<a accesskey="p" href="sec-builder-using-derived-widgets.html"><img src="icons/prev.png" alt="Prev"></a> </td>
<th width="60%" align="center"> </th>
<td width="20%" align="right"> <a accesskey="n" href="sec-i18n-marking-strings.html"><img src="icons/next.png" alt="Next"></a>
</td>
</tr>
</table>
<hr>
</div>
<div class="chapter">
<div class="titlepage"><div><div><h1 class="title">
<a name="chapter-internationalization"></a>Chapter 27. Internationalization and Localization</h1></div></div></div>
<div class="toc">
<p><b>Table of Contents</b></p>
<ul class="toc">
<li><span class="section"><a href="chapter-internationalization.html#sec-internationalization-intro">Preparing your project</a></span></li>
<li><span class="section"><a href="sec-i18n-marking-strings.html">Marking strings for translation</a></span></li>
<li><span class="section"><a href="sec-i18n-expecting-utf8.html">Expecting UTF8</a></span></li>
<li><span class="section"><a href="sec-i18n-pitfalls.html">Pitfalls</a></span></li>
<li><span class="section"><a href="sec-i18n-getting-help-with-translations.html">Getting help with translations</a></span></li>
</ul>
</div>
<p>
<span class="application">gtkmm</span> applications can easily support multiple languages, including
non-European languages such as Chinese and right-to-left languages such as
Arabic. An appropriately-written and translated <span class="application">gtkmm</span> application will use
the appropriate language at runtime based on the user's environment.
</p>
<p>
You might not anticipate the need to support additional languages, but
you can never rule it out. And it's easier to develop the application
properly in the first place rather than retrofitting later.
</p>
<p>
The process of writing source code that allows for translation is called
<code class="literal">internationalization</code>, often abbreviated to
<code class="literal">i18n</code>. The <code class="literal">Localization</code> process,
sometimes abbreviated as <code class="literal">l10n</code>, provides translated text
for other languages, based on that source code.
</p>
<p>
The main activity in the internationalization process is finding strings
seen by users and marking them for translation. You do not need to do it all
at once - if you set up the necessary project infrastructure correctly then
your application will work normally regardless of how many strings you've
covered.
</p>
<p>
String literals should be typed in the source code in English, but
surrounded by a macro. The <span class="application">gettext</span> (or intltool)
utility can then extract the marked strings for translation, and substitute
the translated text at runtime.
</p>
<div class="section">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="sec-internationalization-intro"></a>Preparing your project</h2></div></div></div>
<div class="note" style="margin-left: 0.5in; margin-right: 0.5in;"><table border="0" summary="Note">
<tr>
<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="icons/note.png"></td>
<th align="left">Note</th>
</tr>
<tr><td align="left" valign="top">
<p>
In the instructions below we will assume that you will not be using
<span class="application">gettext</span> directly, but
<span class="application">intltool</span>, which was written specifically for
<code class="literal">GNOME</code>. <span class="application">intltool</span> uses
<code class="function">gettext()</code>, which extracts strings from source code,
but <span class="application">intltool</span> can also combine strings from
other files, for example from desktop menu details, and GUI resource
files such as <code class="filename">.ui</code> files, into standard
<span class="application">gettext</span> <code class="filename">.pot/.po</code> files.
</p>
<p>
We also assume that you are using autotools (<span class="application">automake</span>
and <span class="application">autoconf</span>) to build your project (although
autotools is not recommended for new applications), and
that you are using <a class="ulink" href="https://gitlab.gnome.org/GNOME/gnome-common/blob/master/autogen.sh" target="_top">
<code class="literal">./autogen.sh</code> from <span class="application">gnome-common</span></a>
or a similar <code class="literal">autogen.sh</code> file, which, among other
things, takes care of some <span class="application">intltool</span>
initialization.
</p>
</td></tr>
</table></div>
<div class="note" style="margin-left: 0.5in; margin-right: 0.5in;"><table border="0" summary="Note">
<tr>
<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="icons/note.png"></td>
<th align="left">Note</th>
</tr>
<tr><td align="left" valign="top">
<p>
If you are using <span class="application">meson</span> (recommended), see the
<a class="ulink" href="https://mesonbuild.com/Localisation.html" target="_top">Localisation</a>
chapter in Meson's manual. You can then skip this section.
</p>
</td></tr>
</table></div>
<p>
An alternative to <span class="application">gnome-common</span>'s
<code class="literal">autogen.sh</code> may look like this:
</p>
<pre class="programlisting"><code class="code">#! /bin/sh -e
test -n "$srcdir" || srcdir=`dirname "$0"`
test -n "$srcdir" || srcdir=.
autoreconf --force --install --verbose --warnings=all "$srcdir"
echo "Running intltoolize --copy --force --automake"
intltoolize --copy --force --automake
test -n "$NOCONFIGURE" || "$srcdir/configure" "$@"</code></pre>
<p>
Create a sub-directory named <code class="literal">po</code> in your project's root
directory. This directory will eventually contain all of your
translations. Within it, create a file named <code class="literal">LINGUAS</code>
and a file named <code class="literal">POTFILES.in</code>. It is common practice to
also create a <code class="literal">ChangeLog</code> file in the
<code class="literal">po</code> directory so that translators can keep track of
translation changes.
</p>
<p>
<code class="literal">LINGUAS</code> contains an alphabetically sorted list of codes
identifying the languages for which your program is translated (comment
lines starting with a <code class="literal">#</code> are ignored). Each language
code listed in the <code class="literal">LINGUAS</code> file must have a
corresponding <code class="literal">.po</code> file. So, if your program has German
and Japanese translations, your <code class="literal">LINGUAS</code> file would
look like this:
</p>
<pre class="programlisting"><code class="code"># keep this file sorted alphabetically, one language code per line
de
ja</code></pre>
<p>
(In addition, you'd have the files <code class="literal">de.po</code> and
<code class="literal">ja.po</code> in your
<code class="literal">po</code> directory which contain the German and Japanese
translations, respectively.)
</p>
<p>
<code class="literal">POTFILES.in</code> is a list of paths to all files which
contain strings marked up for translation, starting from the project root
directory. So for example, if your project sources were located in a
subdirectory named <code class="literal">src</code>, and you had two files that
contained strings that should be translated, your
<code class="literal">POTFILES.in</code> file might look like this:
</p>
<pre class="programlisting"><code class="code">src/main.cc
src/other.cc</code></pre>
<p>
If you are using <span class="application">gettext</span> directly, you can only
mark strings for translation if they are in source code file. However, if
you use <span class="application">intltool</span>, you can mark strings for
translation in a variety of other file formats, including
<code class="filename">.ui</code> files, xml,
<a class="ulink" href="http://standards.freedesktop.org/desktop-entry-spec/latest/" target="_top">.desktop files</a>
and several more. So, if you have designed some of the
application UI in xml files then also add your
<code class="filename">.ui</code> files to the list in
<code class="literal">POTFILES.in</code>.
</p>
<p>
Now that there is a place to put your translations, you need to initialize
<span class="application">intltool</span> and <span class="application">gettext</span>.
Add the following code to your <code class="literal">configure.ac</code>,
substituting 'programname' with the name of your program:
</p>
<pre class="programlisting"><code class="code">IT_PROG_INTLTOOL([0.35.0])
GETTEXT_PACKAGE=programname
AC_SUBST(GETTEXT_PACKAGE)
AC_DEFINE_UNQUOTED([GETTEXT_PACKAGE], ["$GETTEXT_PACKAGE"],
[The domain to use with gettext])
AM_GNU_GETTEXT([external])
AM_GNU_GETTEXT_VERSION([0.17])
PROGRAMNAME_LOCALEDIR=[${datadir}/locale]
AC_SUBST(PROGRAMNAME_LOCALEDIR)</code></pre>
<p>
This <code class="varname">PROGRAMNAME_LOCALEDIR</code> variable will be used later
in the <code class="literal">Makefile.am</code> file, to define a macro that will be
used when you initialize <span class="application">gettext</span> in your source
code.
</p>
<p>
<code class="literal">AM_GLIB_GNU_GETTEXT</code> has been an alternative to
<code class="literal">AM_GNU_GETTEXT</code> and <code class="literal">AM_GNU_GETTEXT_VERSION</code>,
but <code class="literal">AM_GLIB_GNU_GETTEXT</code> is now deprecated, and shall
not be used in new code.
</p>
<p>
In the top-level Makefile.am:
</p>
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
<li class="listitem">
<p>Add <code class="literal">po</code> to the <code class="literal">SUBDIRS</code>
variable. Without this, your translations won't get built and
installed when you build the program</p>
</li>
<li class="listitem">
<p>
Define <code class="literal">INTLTOOL_FILES</code> as:</p>
<pre class="programlisting"><code class="code">INTLTOOL_FILES = intltool-extract.in \
intltool-merge.in \
intltool-update.in</code></pre>
</li>
<li class="listitem">
<p>
Add <code class="literal">INTLTOOL_FILES</code> to the
<code class="literal">EXTRA_DIST</code> list of files. This ensures that when
you do a <span class="command"><strong>make dist</strong></span>, these files will be
included in the source tarball.
</p>
</li>
<li class="listitem">
<p>
Update your <code class="literal">DISTCLEANFILES</code>:</p>
<pre class="programlisting"><code class="code">DISTCLEANFILES = ... intltool-extract \
intltool-merge \
intltool-update \
po/.intltool-merge-cache</code></pre>
</li>
<li class="listitem">
<p>
Depending on the types of files that contain translatable strings,
add code such as</p>
<pre class="programlisting"><code class="code">desktopdir = $(datadir)/applications
desktop_in_files = programname.desktop.in
desktop_DATA = $(desktop_in_files:.desktop.in=.desktop)
@INTLTOOL_DESKTOP_RULE@</code></pre>
</li>
</ul></div>
<p>
</p>
<p>
In your <code class="literal">src/Makefile.am</code>, update your
<code class="literal">AM_CPPFLAGS</code> to add the following preprocessor macro
definition:
</p>
<pre class="programlisting"><code class="code">AM_CPPFLAGS = ... -DPROGRAMNAME_LOCALEDIR=\"${PROGRAMNAME_LOCALEDIR}\"</code></pre>
<p>
This macro will be used when you initialize <code class="literal">gettext</code> in
your source code.
</p>
</div>
</div>
<div class="navfooter">
<hr>
<table width="100%" summary="Navigation footer">
<tr>
<td width="40%" align="left">
<a accesskey="p" href="sec-builder-using-derived-widgets.html"><img src="icons/prev.png" alt="Prev"></a> </td>
<td width="20%" align="center"> </td>
<td width="40%" align="right"> <a accesskey="n" href="sec-i18n-marking-strings.html"><img src="icons/next.png" alt="Next"></a>
</td>
</tr>
<tr>
<td width="40%" align="left" valign="top">Using derived widgets </td>
<td width="20%" align="center"><a accesskey="h" href="index.html"><img src="icons/home.png" alt="Home"></a></td>
<td width="40%" align="right" valign="top"> Marking strings for translation</td>
</tr>
</table>
</div>
</body>
</html>
|