Home | History | Annotate | Download | only in cocoa
      1 // Copyright (c) 2009 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #import "chrome/browser/ui/cocoa/ui_localizer.h"
      6 
      7 #import <Foundation/Foundation.h>
      8 
      9 #include <stdlib.h>
     10 #include "base/sys_string_conversions.h"
     11 #include "base/logging.h"
     12 #include "grit/app_strings.h"
     13 #include "grit/chromium_strings.h"
     14 #include "grit/generated_resources.h"
     15 #include "ui/base/l10n/l10n_util.h"
     16 #include "ui/base/l10n/l10n_util_mac.h"
     17 
     18 struct UILocalizerResourceMap {
     19   const char* const name;
     20   unsigned int label_id;
     21   unsigned int label_arg_id;
     22 };
     23 
     24 
     25 namespace {
     26 
     27 // Utility function for bsearch on a ResourceMap table
     28 int ResourceMapCompare(const void* utf8Void,
     29                        const void* resourceMapVoid) {
     30   const char* utf8_key = reinterpret_cast<const char*>(utf8Void);
     31   const UILocalizerResourceMap* res_map =
     32       reinterpret_cast<const UILocalizerResourceMap*> (resourceMapVoid);
     33   return strcmp(utf8_key, res_map->name);
     34 }
     35 
     36 }  // namespace
     37 
     38 @interface GTMUILocalizer (PrivateAdditions)
     39 - (void)localizedObjects;
     40 @end
     41 
     42 @implementation GTMUILocalizer (PrivateAdditions)
     43 - (void)localizedObjects {
     44   // The ivars are private, so this method lets us trigger the localization
     45   // from -[ChromeUILocalizer awakeFromNib].
     46   [self localizeObject:owner_ recursively:YES];
     47   [self localizeObject:otherObjectToLocalize_ recursively:YES];
     48   [self localizeObject:yetAnotherObjectToLocalize_ recursively:YES];
     49 }
     50  @end
     51 
     52 @implementation ChromeUILocalizer
     53 
     54 - (void)awakeFromNib {
     55   // The GTM base is bundle based, since don't need the bundle, use this
     56   // override to bypass the bundle lookup and directly do the localization
     57   // calls.
     58   [self localizedObjects];
     59 }
     60 
     61 - (NSString *)localizedStringForString:(NSString *)string {
     62 
     63   // Include the table here so it is a local static.  This header provides
     64   // kUIResources and kUIResourcesSize.
     65 #include "ui_localizer_table.h"
     66 
     67   // Look up the string for the resource id to fetch.
     68   const char* utf8_key = [string UTF8String];
     69   if (utf8_key) {
     70     const void* valVoid = bsearch(utf8_key,
     71                                   kUIResources,
     72                                   kUIResourcesSize,
     73                                   sizeof(UILocalizerResourceMap),
     74                                   ResourceMapCompare);
     75     const UILocalizerResourceMap* val =
     76         reinterpret_cast<const UILocalizerResourceMap*>(valVoid);
     77     if (val) {
     78       // Do we need to build the string, or just fetch it?
     79       if (val->label_arg_id != 0) {
     80         const string16 label_arg(l10n_util::GetStringUTF16(val->label_arg_id));
     81         return l10n_util::GetNSStringFWithFixup(val->label_id,
     82                                                 label_arg);
     83       }
     84 
     85       return l10n_util::GetNSStringWithFixup(val->label_id);
     86     }
     87 
     88     // Sanity check, there shouldn't be any strings with this id that aren't
     89     // in our map.
     90     DLOG_IF(WARNING, [string hasPrefix:@"^ID"]) << "Key '" << utf8_key
     91         << "' wasn't in the resource map?";
     92   }
     93 
     94   // If we didn't find anything, this string doesn't need localizing.
     95   return nil;
     96 }
     97 
     98 @end
     99