]> git.ipfire.org Git - thirdparty/cups.git/blobdiff - ppdc/ppdc-catalog.cxx
Update all references to OS X to macOS.
[thirdparty/cups.git] / ppdc / ppdc-catalog.cxx
index 1bf46566f0d6f8895bd6b81392c91be23c4d2e86..1ec051d73a68d2dd4a6e0209de39868de0cf84a2 100644 (file)
@@ -1,9 +1,7 @@
 //
-// "$Id$"
-//
 // Shared message catalog class for the CUPS PPD Compiler.
 //
-// Copyright 2007-2014 by Apple Inc.
+// Copyright 2007-2016 by Apple Inc.
 // Copyright 2002-2006 by Easy Software Products.
 //
 // These coded instructions, statements, and computer programs are the
@@ -37,6 +35,9 @@ typedef enum
 // Local functions...
 //
 
+#if defined(__APPLE__) && defined(CUPS_BUNDLEDIR)
+static void    apple_add_message(CFStringRef key, CFStringRef val, ppdcCatalog *c);
+#endif /* __APPLE__ && CUPS_BUNDLEDIR */
 static int     get_utf8(char *&ptr);
 static int     get_utf16(cups_file_t *fp, ppdc_cs_t &cs);
 static int     put_utf8(int ch, char *&ptr, char *end);
@@ -51,10 +52,6 @@ ppdcCatalog::ppdcCatalog(const char *l,      // I - Locale
                          const char *f)        // I - Message catalog file
   : ppdcShared()
 {
-  _cups_globals_t      *cg = _cupsGlobals();
-                                       // Global information
-
-
   PPDC_NEW;
 
   locale   = new ppdcString(l);
@@ -67,6 +64,69 @@ ppdcCatalog::ppdcCatalog(const char *l,      // I - Locale
     char       pofile[1024];           // Message catalog file
 
 
+#if defined(__APPLE__) && defined(CUPS_BUNDLEDIR)
+    char               applelang[256]; // Apple language ID
+    CFURLRef           url;            // URL to cups.strings file
+    CFReadStreamRef    stream = NULL;  // File stream
+    CFPropertyListRef  plist = NULL;   // Localization file
+
+    snprintf(pofile, sizeof(pofile), CUPS_BUNDLEDIR "/Resources/%s.lproj/cups.strings", _cupsAppleLanguage(l, applelang, sizeof(applelang)));
+    if (access(pofile, 0))
+    {
+      // Try alternate lproj directory names...
+      const char *tl = l;              // Temporary locale string
+
+      if (!strncmp(l, "en", 2))
+       tl = "English";
+      else if (!strncmp(l, "nb", 2))
+        tl = "no";
+      else if (!strncmp(l, "nl", 2))
+       tl = "Dutch";
+      else if (!strncmp(l, "fr", 2))
+       tl = "French";
+      else if (!strncmp(l, "de", 2))
+       tl = "German";
+      else if (!strncmp(l, "it", 2))
+       tl = "Italian";
+      else if (!strncmp(l, "ja", 2))
+       tl = "Japanese";
+      else if (!strncmp(l, "es", 2))
+       tl = "Spanish";
+
+      snprintf(pofile, sizeof(pofile), CUPS_BUNDLEDIR "/Resources/%s.lproj/cups.strings", tl);
+    }
+
+    url = CFURLCreateFromFileSystemRepresentation(kCFAllocatorDefault, (UInt8 *)pofile, (CFIndex)strlen(pofile), false);
+    if (url)
+    {
+      stream = CFReadStreamCreateWithFile(kCFAllocatorDefault, url);
+
+      if (stream)
+      {
+       /*
+       * Read the property list containing the localization data.
+       */
+
+       CFReadStreamOpen(stream);
+
+       plist = CFPropertyListCreateWithStream(kCFAllocatorDefault, stream, 0, kCFPropertyListImmutable, NULL, NULL);
+
+       if (plist && CFGetTypeID(plist) == CFDictionaryGetTypeID())
+         CFDictionaryApplyFunction((CFDictionaryRef)plist, (CFDictionaryApplierFunction)apple_add_message, this);
+
+       if (plist)
+         CFRelease(plist);
+
+       CFRelease(stream);
+      }
+
+      CFRelease(url);
+    }
+
+#else
+    _cups_globals_t    *cg = _cupsGlobals();
+                                       // Global information
+
     snprintf(pofile, sizeof(pofile), "%s/%s/cups_%s.po", cg->localedir, l, l);
 
     if (load_messages(pofile) && strchr(l, '_'))
@@ -81,9 +141,10 @@ ppdcCatalog::ppdcCatalog(const char *l,     // I - Locale
 
       load_messages(pofile);
     }
+#endif /* __APPLE__ && CUPS_BUNDLEDIR */
   }
 
-  if (f)
+  if (f && *f)
     load_messages(f);
 }
 
@@ -193,8 +254,8 @@ ppdcCatalog::load_messages(
   else if (!strcmp(ptr, ".strings"))
   {
    /*
-    * Read messages in OS X ".strings" format, which are UTF-16 text files of
-    * the format:
+    * Read messages in macOS ".strings" format, which are either UTF-8/UTF-16
+    * text files of the format:
     *
     *     "id" = "str";
     *
@@ -597,6 +658,27 @@ ppdcCatalog::save_messages(
 }
 
 
+#if defined(__APPLE__) && defined(CUPS_BUNDLEDIR)
+//
+// 'apple_add_message()' - Add a message from a localization dictionary.
+//
+
+static void
+apple_add_message(CFStringRef key,     // I - Localization key
+                  CFStringRef val,     // I - Localized value
+                  ppdcCatalog *c)      // I - Message catalog
+{
+  char id[1024],                       // Message id
+       str[1024];                      // Localized message
+
+
+  if (CFStringGetCString(key, id, sizeof(id), kCFStringEncodingUTF8) &&
+      CFStringGetCString(val, str, sizeof(str), kCFStringEncodingUTF8))
+    c->add_message(id, str);
+}
+#endif /* __APPLE__ && CUPS_BUNDLEDIR */
+
+
 //
 // 'get_utf8()' - Get a UTF-8 character.
 //
@@ -877,8 +959,3 @@ put_utf16(cups_file_t *fp,          // I - File to write to
 
   return (-1);
 }
-
-
-//
-// End of "$Id$".
-//