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