- Add some hints to reduce strings to localize when using NLS
Reviewed by: rene
This commit is contained in:
parent
ec9bd87c63
commit
9948e825ef
Notes:
svn2git
2020-12-08 03:00:23 +00:00
svn path=/head/; revision=34158
1 changed files with 54 additions and 0 deletions
|
@ -221,6 +221,60 @@ printf(getstr(1));
|
|||
#ifndef WITHOUT_NLS
|
||||
catclose(catalog);
|
||||
#endif</programlisting>
|
||||
|
||||
<sect3>
|
||||
<title>Reducing Strings to Localize</title>
|
||||
|
||||
<para>There is a good way of reducing the strings that
|
||||
need to be localized by using <application>libc</application>
|
||||
error messages. This is also useful to just avoid duplication
|
||||
and provide consistent error messages for the common errors
|
||||
that can be encountered by a great many of programs.</para>
|
||||
|
||||
<para>First, here is an example that does not use
|
||||
<application>libc</application> error messages:</para>
|
||||
|
||||
<programlisting>
|
||||
#include <err.h>
|
||||
...
|
||||
if (!S_ISDIR(st.st_mode))
|
||||
err(1, "argument is not a directory");
|
||||
</programlisting>
|
||||
|
||||
<para>This can be transformed to print an error message by
|
||||
reading <varname>errno</varname> and printing an error message
|
||||
accordingly:</para>
|
||||
|
||||
<programlisting>
|
||||
#include <err.h>
|
||||
#include <errno.h>
|
||||
...
|
||||
if (!S_ISDIR(st.st_mode)) {
|
||||
errno = ENOTDIR;
|
||||
err(1, NULL);
|
||||
}
|
||||
</programlisting>
|
||||
|
||||
<para>In this example, the custom string is eliminated, thus
|
||||
translators will have less work when localizing the program
|
||||
and users will see the usual <quote>Not a directory</quote>
|
||||
error message when they encounter this error. This message
|
||||
will probably seem more familiar to them. Please note that
|
||||
it was necessary to include <filename
|
||||
class="headerfile">errno.h</filename> in order to directly
|
||||
access <varname>errno</varname>.</para>
|
||||
|
||||
<para>It is worth to note that there are cases when
|
||||
<varname>errno</varname> is set automatically by a preceding
|
||||
call, so it is not necessary to set it explicitly:</para>
|
||||
|
||||
<programlisting>
|
||||
#include <err.h>
|
||||
...
|
||||
if ((p = malloc(size)) == NULL)
|
||||
err(1, NULL);
|
||||
</programlisting>
|
||||
</sect3>
|
||||
</sect2>
|
||||
|
||||
<sect2 id="nls-mk">
|
||||
|
|
Loading…
Reference in a new issue