diff options
author | thomasvl@chromium.org <thomasvl@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-08-11 13:32:21 +0000 |
---|---|---|
committer | thomasvl@chromium.org <thomasvl@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-08-11 13:32:21 +0000 |
commit | 92e1212fd2d33d4bf6489c51ae0d81e195ff3a5a (patch) | |
tree | 0a23a320e88bcc56175b164a5e0675f2702030a0 /chrome/browser/cocoa/ui_localizer.mm | |
parent | 04bbad202d07b9c32f47aa9332ef95d3686e68ef (diff) | |
download | chromium_src-92e1212fd2d33d4bf6489c51ae0d81e195ff3a5a.zip chromium_src-92e1212fd2d33d4bf6489c51ae0d81e195ff3a5a.tar.gz chromium_src-92e1212fd2d33d4bf6489c51ae0d81e195ff3a5a.tar.bz2 |
ObjC classes generated by the build and used in Xib files is already getting ugly, if they aren't built at build start, IB sometimes throws warnings about unknown things, so...
- Use one class for the localizer and generate the table that drives it.
- Update the generator script to process a list of xib files and generate one header.
- Update the data within the GYP file to do this.
- This might actually help overall size since it helps force one set of strings for each different window.
- Switch to bsearch for table lookup since we have one, larger table now.
TEST=none
BUG=none
Review URL: http://codereview.chromium.org/164260
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@23017 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/cocoa/ui_localizer.mm')
-rw-r--r-- | chrome/browser/cocoa/ui_localizer.mm | 98 |
1 files changed, 55 insertions, 43 deletions
diff --git a/chrome/browser/cocoa/ui_localizer.mm b/chrome/browser/cocoa/ui_localizer.mm index 7fd4d44..77e9154 100644 --- a/chrome/browser/cocoa/ui_localizer.mm +++ b/chrome/browser/cocoa/ui_localizer.mm @@ -6,53 +6,33 @@ #import <Foundation/Foundation.h> +#include <stdlib.h> #include "app/l10n_util_mac.h" #include "base/sys_string_conversions.h" #include "base/logging.h" +#include "grit/app_strings.h" +#include "grit/chromium_strings.h" +#include "grit/generated_resources.h" -namespace ui_localizer { +struct UILocalizerResourceMap { + const char* const name; + unsigned int label_id; + unsigned int label_arg_id; +}; -NSString* LocalizedStringForKeyFromMapList(NSString* key, - const ResourceMap* map_list, - size_t map_list_len) { - DCHECK(key != nil); - DCHECK(map_list != NULL); - // Look up the string for the resource id to fetch. - const char* utf8_key = [key UTF8String]; - if (utf8_key) { - // If we end up with enough string constants in here, look at using bsearch - // to speed up the searching. - for (size_t i = 0; i < map_list_len; ++i) { - int strcmp_result = strcmp(utf8_key, map_list[i].name); - if (strcmp_result == 0) { - // Do we need to build the string, or just fetch it? - if (map_list[i].label_arg_id != 0) { - const string16 label_arg( - l10n_util::GetStringUTF16(map_list[i].label_arg_id)); - return l10n_util::GetNSStringFWithFixup(map_list[i].label_id, - label_arg); - } - - return l10n_util::GetNSStringWithFixup(map_list[i].label_id); - } - - // If we've passed where the string would be, give up. - if (strcmp_result < 0) - break; - } - } +namespace { - // Sanity check, there shouldn't be any strings with this id that aren't - // in our map. - DLOG_IF(WARNING, [key hasPrefix:@"^ID"]) << "Key '" << utf8_key - << "' wasn't in the resource map?"; - - // If we didn't find anything, this string doesn't need localizing. - return nil; +// Utility function for bsearch on a ResourceMap table +int ResourceMapCompare(const void* utf8Void, + const void* resourceMapVoid) { + const char* utf8_key = reinterpret_cast<const char*>(utf8Void); + const UILocalizerResourceMap* res_map = + reinterpret_cast<const UILocalizerResourceMap*> (resourceMapVoid); + return strcmp(utf8_key, res_map->name); } -} // namespace ui_localizer +} // namespace @interface GTMUILocalizer (PrivateAdditions) - (void)localizedObjects; @@ -69,17 +49,49 @@ NSString* LocalizedStringForKeyFromMapList(NSString* key, @end @implementation ChromeUILocalizer + - (void)awakeFromNib { // The GTM base is bundle based, since don't need the bundle, use this // override to bypass the bundle lookup and directly do the localization // calls. [self localizedObjects]; } -#ifndef NDEBUG -// Catch anyone that uses this directly. + - (NSString *)localizedStringForString:(NSString *)string { - LOG(FATAL) << "Don't use ChromeUILocalizer directly."; - return @"Don't use ChromeUILocalizer directly."; + + // Include the table here so it is a local static. This header provides + // kUIResources and kUIResourcesSize. +#include "ui_localizer_table.h" + + // Look up the string for the resource id to fetch. + const char* utf8_key = [string UTF8String]; + if (utf8_key) { + const void* valVoid = bsearch(utf8_key, + kUIResources, + kUIResourcesSize, + sizeof(UILocalizerResourceMap), + ResourceMapCompare); + const UILocalizerResourceMap* val = + reinterpret_cast<const UILocalizerResourceMap*>(valVoid); + if (val) { + // Do we need to build the string, or just fetch it? + if (val->label_arg_id != 0) { + const string16 label_arg(l10n_util::GetStringUTF16(val->label_arg_id)); + return l10n_util::GetNSStringFWithFixup(val->label_id, + label_arg); + } + + return l10n_util::GetNSStringWithFixup(val->label_id); + } + + // Sanity check, there shouldn't be any strings with this id that aren't + // in our map. + DLOG_IF(WARNING, [string hasPrefix:@"^ID"]) << "Key '" << utf8_key + << "' wasn't in the resource map?"; + } + + // If we didn't find anything, this string doesn't need localizing. + return nil; } -#endif + @end |