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