]> git.ipfire.org Git - thirdparty/cups.git/blobdiff - cups/language.c
Merge changes from 1.4svn-r7067.
[thirdparty/cups.git] / cups / language.c
index 435c84bc446a37526093cde32d009e9487ebf4b1..4edaab418048a0cda113a1ca6c806f9e9684934d 100644 (file)
@@ -1,25 +1,16 @@
 /*
- * "$Id$"
+ * "$Id: language.c 6916 2007-09-05 21:14:08Z mike $"
  *
  *   I18N/language support for the Common UNIX Printing System (CUPS).
  *
- *   Copyright 1997-2006 by Easy Software Products.
+ *   Copyright 2007 by Apple Inc.
+ *   Copyright 1997-2007 by Easy Software Products.
  *
  *   These coded instructions, statements, and computer programs are the
- *   property of Easy Software Products and are protected by Federal
- *   copyright law.  Distribution and use rights are outlined in the file
- *   "LICENSE.txt" which should have been included with this file.  If this
- *   file is missing or damaged please contact Easy Software Products
- *   at:
- *
- *       Attn: CUPS Licensing Information
- *       Easy Software Products
- *       44141 Airport View Drive, Suite 204
- *       Hollywood, Maryland 20636 USA
- *
- *       Voice: (301) 373-9600
- *       EMail: cups-info@cups.org
- *         WWW: http://www.cups.org
+ *   property of Apple Inc. and are protected by Federal copyright
+ *   law.  Distribution and use rights are outlined in the file "LICENSE.txt"
+ *   which should have been included with this file.  If this file is
+ *   file is missing or damaged, see the license at "http://www.cups.org/".
  *
  *   This file is subject to the Apple OS-Developed Software exception.
  *
@@ -351,7 +342,7 @@ cupsLangGet(const char *language)   /* I - Language or locale */
   * preference so we have to look it up this way...
   */
 
-  if (!language)
+  if (!language && (language = getenv("LANG")) == NULL)
     language = appleLangDefault();
 
 #else
@@ -406,14 +397,6 @@ cupsLangGet(const char *language)  /* I - Language or locale */
 
         *ptr = '\0';
       }
-      else
-      {
-       /*
-        * Default to UTF-8...
-       */
-
-        strcpy(charset, "UTF8");
-      }
 
      /*
       * Get the locale for messages from the LC_MESSAGES locale setting...
@@ -430,8 +413,14 @@ cupsLangGet(const char *language)  /* I - Language or locale */
       strlcpy(locale, ptr, sizeof(locale));
       language = locale;
 
-      DEBUG_printf(("cupsLangGet: new language value is \"%s\"\n",
-                    language ? language : "(null)"));
+     /*
+      * CUPS STR #2575: Map "nb" to "no" for back-compatibility...
+      */
+
+      if (!strncmp(locale, "nb", 2))
+        locale[1] = 'o';
+
+      DEBUG_printf(("cupsLangGet: new language value is \"%s\"\n", language));
     }
   }
 #endif /* __APPLE__ */
@@ -473,6 +462,13 @@ cupsLangGet(const char *language)  /* I - Language or locale */
   }
 #endif /* CODESET */
 
+ /*
+  * If we don't have a character set by now, default to UTF-8...
+  */
+
+  if (!charset[0])
+    strcpy(charset, "UTF8");
+
  /*
   * Parse the language string passed in to a locale string. "C" is the
   * standard POSIX locale and is copied unchanged.  Otherwise the
@@ -560,6 +556,23 @@ cupsLangGet(const char *language)  /* I - Language or locale */
        encoding = (cups_encoding_t)i;
        break;
       }
+
+    if (encoding == CUPS_AUTO_ENCODING)
+    {
+     /*
+      * Map alternate names for various character sets...
+      */
+
+      if (!strcasecmp(charset, "iso-2022-jp") ||
+          !strcasecmp(charset, "sjis"))
+       encoding = CUPS_WINDOWS_932;
+      else if (!strcasecmp(charset, "iso-2022-cn"))
+       encoding = CUPS_WINDOWS_936;
+      else if (!strcasecmp(charset, "iso-2022-kr"))
+       encoding = CUPS_WINDOWS_949;
+      else if (!strcasecmp(charset, "big5"))
+       encoding = CUPS_WINDOWS_950;
+    }
   }
 
   DEBUG_printf(("cupsLangGet: encoding=%d(%s)\n", encoding,
@@ -587,7 +600,7 @@ cupsLangGet(const char *language)   /* I - Language or locale */
   pthread_mutex_lock(&lang_mutex);
 #endif /* HAVE_PTHREAD_H */
 
-  if ((lang = cups_cache_lookup(langname, encoding)) != NULL)
+  if ((lang = cups_cache_lookup(real, encoding)) != NULL)
   {
 #ifdef HAVE_PTHREAD_H
     pthread_mutex_unlock(&lang_mutex);
@@ -871,7 +884,7 @@ _cupsMessageLoad(const char *filename)      /* I - Message catalog to load */
       * Append to current string...
       */
 
-      length = strlen(m->str ? m->str : m->id);
+      length = (int)strlen(m->str ? m->str : m->id);
 
       if ((temp = realloc(m->str ? m->str : m->id,
                           length + strlen(ptr) + 1)) == NULL)
@@ -968,6 +981,22 @@ _cupsMessageLookup(cups_array_t *a,        /* I - Message array */
  */
 
 #  ifdef HAVE_CF_LOCALE_ID
+
+typedef struct
+{
+  const char * const name;             /* Language name */
+  const char * const locale;           /* Locale name */
+} _apple_name_locale_t;
+
+static const _apple_name_locale_t apple_name_locale[] =
+{
+  { "en"       , "en_US" },
+  { "nb"       , "no"    },
+  { "zh-Hans"  , "zh_CN" },
+  { "zh-Hant"  , "zh_TW" }
+};
+
+
 /*
  * 'appleLangDefault()' - Get the default locale string.
  */
@@ -975,10 +1004,14 @@ _cupsMessageLookup(cups_array_t *a,      /* I - Message array */
 static const char *                    /* O - Locale string */
 appleLangDefault(void)
 {
+  int                  i;              /* Looping var */
+  CFBundleRef          bundle;         /* Main bundle (if any) */
+  CFArrayRef           bundleList;     /* List of localizations in bundle */
   CFPropertyListRef    localizationList;
                                        /* List of localization data */
   CFStringRef          languageName;   /* Current name */
   CFStringRef          localeName;     /* Canonical from of name */
+  char                 *lang;          /* LANG environment variable */
   _cups_globals_t      *cg = _cupsGlobals();
                                        /* Pointer to library globals */
 
@@ -989,35 +1022,75 @@ appleLangDefault(void)
 
   if (!cg->language[0])
   {
-    localizationList =
-        CFPreferencesCopyAppValue(CFSTR("AppleLanguages"),
-                                  kCFPreferencesCurrentApplication);
+    if ((lang = getenv("LANG")))
+    {
+      strlcpy(cg->language, lang, sizeof(cg->language));
+      return (cg->language);
+    }
+    else if ((bundle = CFBundleGetMainBundle()) != NULL &&
+             (bundleList = CFBundleCopyBundleLocalizations(bundle)) != NULL)
+    {
+      localizationList =
+         CFBundleCopyPreferredLocalizationsFromArray(bundleList);
+
+      CFRelease(bundleList);
+    }
+    else
+      localizationList =
+         CFPreferencesCopyAppValue(CFSTR("AppleLanguages"),
+                                   kCFPreferencesCurrentApplication);
 
-    if (localizationList != NULL)
+    if (localizationList)
     {
       if (CFGetTypeID(localizationList) == CFArrayGetTypeID() &&
          CFArrayGetCount(localizationList) > 0)
       {
-        languageName = CFArrayGetValueAtIndex(localizationList, 0);
+       languageName = CFArrayGetValueAtIndex(localizationList, 0);
 
-        if (languageName != NULL &&
-            CFGetTypeID(languageName) == CFStringGetTypeID())
-        {
+       if (languageName &&
+           CFGetTypeID(languageName) == CFStringGetTypeID())
+       {
          localeName = CFLocaleCreateCanonicalLocaleIdentifierFromString(
-                          kCFAllocatorDefault, languageName);
+                          kCFAllocatorDefault, languageName);
 
-         if (localeName != NULL)
+         if (localeName)
          {
            CFStringGetCString(localeName, cg->language, sizeof(cg->language),
                               kCFStringEncodingASCII);
            CFRelease(localeName);
 
-           if (!strcmp(cg->language, "en"))
-             strlcpy(cg->language, "en_US.UTF-8", sizeof(cg->language));
-           else if (strchr(cg->language, '.') == NULL)
+           DEBUG_printf(("appleLangDefault: cg->language=\"%s\"\n",
+                         cg->language));
+
+          /*
+           * Map new language identifiers to locales...
+           */
+
+           for (i = 0;
+                i < sizeof(apple_name_locale) / sizeof(apple_name_locale[0]);
+                i++)
+           {
+             if (!strcmp(cg->language, apple_name_locale[i].name))
+             {
+               DEBUG_printf(("appleLangDefault: mapping \"%s\" to \"%s\"...\n",
+                             cg->language, apple_name_locale[i].locale));
+               strlcpy(cg->language, apple_name_locale[i].locale, 
+                       sizeof(cg->language));
+               break;
+             }
+           }
+
+          /*
+           * Convert language subtag into region subtag...
+           */
+
+           if (cg->language[2] == '-')
+             cg->language[2] = '_';
+
+           if (!strchr(cg->language, '.'))
              strlcat(cg->language, ".UTF-8", sizeof(cg->language));
          }
-        }
+       }
       }
 
       CFRelease(localizationList);
@@ -1128,59 +1201,66 @@ appleLangDefault(void)
   char                 buff[256];      /* Temporary buffer */
   _cups_globals_t      *cg = _cupsGlobals();
                                        /* Pointer to library globals */
+  char                 *lang;          /* LANG environment variable */
 
 
  /*
   * Only do the lookup and translation the first time.
   */
 
-  if (cg->language == NULL)
+  if (!cg->language[0])
   {
-    localizationList =
-        CFPreferencesCopyAppValue(CFSTR("AppleLanguages"),
-                                  kCFPreferencesCurrentApplication);
-
-    if (localizationList != NULL)
+    if ((lang = getenv("LANG")))
+      strlcpy(cg->language, lang, sizeof(cg->language));
+    else
     {
-      if (CFGetTypeID(localizationList) == CFArrayGetTypeID() &&
-         CFArrayGetCount(localizationList) > 0)
-      {
-       localizationName = CFArrayGetValueAtIndex(localizationList, 0);
+      localizationList =
+          CFPreferencesCopyAppValue(CFSTR("AppleLanguages"),
+                                    kCFPreferencesCurrentApplication);
 
-       if (localizationName != NULL &&
-            CFGetTypeID(localizationName) == CFStringGetTypeID())
+      if (localizationList != NULL)
+      {
+       if (CFGetTypeID(localizationList) == CFArrayGetTypeID() &&
+           CFArrayGetCount(localizationList) > 0)
        {
-         CFIndex length = CFStringGetLength(localizationName);
+         localizationName = CFArrayGetValueAtIndex(localizationList, 0);
 
-         if (length <= sizeof(buff) &&
-             CFStringGetCString(localizationName, buff, sizeof(buff),
-                                kCFStringEncodingASCII))
+         if (localizationName != NULL &&
+              CFGetTypeID(localizationName) == CFStringGetTypeID())
          {
-           buff[sizeof(buff) - 1] = '\0';
+           CFIndex length = CFStringGetLength(localizationName);
 
-           for (i = 0;
-                i < sizeof(apple_name_locale) / sizeof(apple_name_locale[0]);
-                i++)
+           if (length <= sizeof(buff) &&
+               CFStringGetCString(localizationName, buff, sizeof(buff),
+                                  kCFStringEncodingASCII))
            {
-             if (!strcasecmp(buff, apple_name_locale[i].name))
+             buff[sizeof(buff) - 1] = '\0';
+
+             for (i = 0;
+                  i < sizeof(apple_name_locale) / sizeof(apple_name_locale[0]);
+                  i++)
              {
-               cg->language = apple_name_locale[i].locale;
-               break;
+               if (!strcasecmp(buff, apple_name_locale[i].name))
+               {
+                 strlcpy(cg->language, apple_name_locale[i].locale, 
+                         sizeof(cg->language));
+                 break;
+               }
              }
            }
          }
        }
-      }
 
-      CFRelease(localizationList);
+       CFRelease(localizationList);
+      }
     }
   
    /*
     * If we didn't find the language, default to en_US...
     */
 
-    if (cg->language == NULL)
-      cg->language = apple_name_locale[0].locale;
+    if (!cg->language[0])
+      strlcpy(cg->language, apple_name_locale[0].locale, sizeof(cg->language));
   }
 
  /*
@@ -1271,6 +1351,8 @@ cups_unquote(char       *d,               /* O - Unquoted string */
          *d = *d * 8 + *s - '0';
          s ++;
        }
+
+       d ++;
       }
       else
       {
@@ -1295,5 +1377,5 @@ cups_unquote(char       *d,               /* O - Unquoted string */
 
 
 /*
- * End of "$Id$".
+ * End of "$Id: language.c 6916 2007-09-05 21:14:08Z mike $".
  */