1*946379e7Schristos /* Localization of proper names.
2*946379e7Schristos Copyright (C) 2006 Free Software Foundation, Inc.
3*946379e7Schristos Written by Bruno Haible <bruno@clisp.org>, 2006.
4*946379e7Schristos
5*946379e7Schristos This program is free software; you can redistribute it and/or modify
6*946379e7Schristos it under the terms of the GNU General Public License as published by
7*946379e7Schristos the Free Software Foundation; either version 2, or (at your option)
8*946379e7Schristos any later version.
9*946379e7Schristos
10*946379e7Schristos This program is distributed in the hope that it will be useful,
11*946379e7Schristos but WITHOUT ANY WARRANTY; without even the implied warranty of
12*946379e7Schristos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13*946379e7Schristos GNU General Public License for more details.
14*946379e7Schristos
15*946379e7Schristos You should have received a copy of the GNU General Public License
16*946379e7Schristos along with this program; if not, write to the Free Software Foundation,
17*946379e7Schristos Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
18*946379e7Schristos
19*946379e7Schristos #include <config.h>
20*946379e7Schristos
21*946379e7Schristos /* Specification. */
22*946379e7Schristos #include "propername.h"
23*946379e7Schristos
24*946379e7Schristos #include <stdio.h>
25*946379e7Schristos #include <stdlib.h>
26*946379e7Schristos #include <string.h>
27*946379e7Schristos #if HAVE_ICONV
28*946379e7Schristos # include <iconv.h>
29*946379e7Schristos #endif
30*946379e7Schristos
31*946379e7Schristos #include "localcharset.h"
32*946379e7Schristos #include "c-strcase.h"
33*946379e7Schristos #include "xstriconv.h"
34*946379e7Schristos #include "c-strstr.h"
35*946379e7Schristos #include "strstr.h"
36*946379e7Schristos #include "xalloc.h"
37*946379e7Schristos #include "gettext.h"
38*946379e7Schristos
39*946379e7Schristos
40*946379e7Schristos /* Return the localization of NAME. NAME is written in ASCII. */
41*946379e7Schristos
42*946379e7Schristos const char *
proper_name(const char * name)43*946379e7Schristos proper_name (const char *name)
44*946379e7Schristos {
45*946379e7Schristos /* See whether there is a translation. */
46*946379e7Schristos const char *translation = gettext (name);
47*946379e7Schristos
48*946379e7Schristos if (translation != name)
49*946379e7Schristos {
50*946379e7Schristos /* See whether the translation contains the original name. */
51*946379e7Schristos if (strstr (translation, name) != NULL)
52*946379e7Schristos return translation;
53*946379e7Schristos else
54*946379e7Schristos {
55*946379e7Schristos /* Return "TRANSLATION (NAME)". */
56*946379e7Schristos char *result =
57*946379e7Schristos (char *) xmalloc (strlen (translation) + 2 + strlen (name) + 1 + 1);
58*946379e7Schristos
59*946379e7Schristos sprintf (result, "%s (%s)", translation, name);
60*946379e7Schristos return result;
61*946379e7Schristos }
62*946379e7Schristos }
63*946379e7Schristos else
64*946379e7Schristos return name;
65*946379e7Schristos }
66*946379e7Schristos
67*946379e7Schristos /* Return the localization of a name whose original writing is not ASCII.
68*946379e7Schristos NAME_UTF8 is the real name, written in UTF-8 with octal or hexadecimal
69*946379e7Schristos escape sequences. NAME_ASCII is a fallback written only with ASCII
70*946379e7Schristos characters. */
71*946379e7Schristos
72*946379e7Schristos const char *
proper_name_utf8(const char * name_ascii,const char * name_utf8)73*946379e7Schristos proper_name_utf8 (const char *name_ascii, const char *name_utf8)
74*946379e7Schristos {
75*946379e7Schristos /* See whether there is a translation. */
76*946379e7Schristos const char *translation = gettext (name_ascii);
77*946379e7Schristos
78*946379e7Schristos /* Try to convert NAME_UTF8 to the locale encoding. */
79*946379e7Schristos const char *locale_code = locale_charset ();
80*946379e7Schristos char *alloc_name_converted = NULL;
81*946379e7Schristos char *alloc_name_converted_translit = NULL;
82*946379e7Schristos const char *name_converted = NULL;
83*946379e7Schristos const char *name_converted_translit = NULL;
84*946379e7Schristos const char *name;
85*946379e7Schristos
86*946379e7Schristos if (c_strcasecmp (locale_code, "UTF-8") != 0)
87*946379e7Schristos {
88*946379e7Schristos #if HAVE_ICONV
89*946379e7Schristos name_converted = alloc_name_converted =
90*946379e7Schristos xstr_iconv (name_utf8, "UTF-8", locale_code);
91*946379e7Schristos
92*946379e7Schristos # if (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 2) || __GLIBC__ > 2 \
93*946379e7Schristos || _LIBICONV_VERSION >= 0x0105
94*946379e7Schristos {
95*946379e7Schristos size_t len = strlen (locale_code);
96*946379e7Schristos char *locale_code_translit = (char *) xmalloc (len + 10 + 1);
97*946379e7Schristos memcpy (locale_code_translit, locale_code, len);
98*946379e7Schristos memcpy (locale_code_translit + len, "//TRANSLIT", 10 + 1);
99*946379e7Schristos
100*946379e7Schristos name_converted_translit = alloc_name_converted_translit =
101*946379e7Schristos xstr_iconv (name_utf8, "UTF-8", locale_code_translit);
102*946379e7Schristos
103*946379e7Schristos free (locale_code_translit);
104*946379e7Schristos }
105*946379e7Schristos # endif
106*946379e7Schristos #endif
107*946379e7Schristos }
108*946379e7Schristos else
109*946379e7Schristos {
110*946379e7Schristos name_converted = name_utf8;
111*946379e7Schristos name_converted_translit = name_utf8;
112*946379e7Schristos }
113*946379e7Schristos
114*946379e7Schristos /* The name in locale encoding. */
115*946379e7Schristos name = (name_converted != NULL ? name_converted :
116*946379e7Schristos name_converted_translit != NULL ? name_converted_translit :
117*946379e7Schristos name_ascii);
118*946379e7Schristos
119*946379e7Schristos if (translation != name_ascii)
120*946379e7Schristos {
121*946379e7Schristos /* See whether the translation contains the original name.
122*946379e7Schristos A multibyte-aware strstr() is not absolutely necessary here. */
123*946379e7Schristos if (c_strstr (translation, name_ascii) != NULL
124*946379e7Schristos || (name_converted != NULL
125*946379e7Schristos && strstr (translation, name_converted) != NULL)
126*946379e7Schristos || (name_converted_translit != NULL
127*946379e7Schristos && strstr (translation, name_converted_translit) != NULL))
128*946379e7Schristos {
129*946379e7Schristos if (alloc_name_converted != NULL)
130*946379e7Schristos free (alloc_name_converted);
131*946379e7Schristos if (alloc_name_converted_translit != NULL)
132*946379e7Schristos free (alloc_name_converted_translit);
133*946379e7Schristos return translation;
134*946379e7Schristos }
135*946379e7Schristos else
136*946379e7Schristos {
137*946379e7Schristos /* Return "TRANSLATION (NAME)". */
138*946379e7Schristos char *result =
139*946379e7Schristos (char *) xmalloc (strlen (translation) + 2 + strlen (name) + 1 + 1);
140*946379e7Schristos
141*946379e7Schristos sprintf (result, "%s (%s)", translation, name);
142*946379e7Schristos
143*946379e7Schristos if (alloc_name_converted != NULL)
144*946379e7Schristos free (alloc_name_converted);
145*946379e7Schristos if (alloc_name_converted_translit != NULL)
146*946379e7Schristos free (alloc_name_converted_translit);
147*946379e7Schristos return result;
148*946379e7Schristos }
149*946379e7Schristos }
150*946379e7Schristos else
151*946379e7Schristos {
152*946379e7Schristos if (alloc_name_converted != NULL && alloc_name_converted != name)
153*946379e7Schristos free (alloc_name_converted);
154*946379e7Schristos if (alloc_name_converted_translit != NULL
155*946379e7Schristos && alloc_name_converted_translit != name)
156*946379e7Schristos free (alloc_name_converted_translit);
157*946379e7Schristos return name;
158*946379e7Schristos }
159*946379e7Schristos }
160