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