summaryrefslogtreecommitdiffstats
path: root/chrome/browser
diff options
context:
space:
mode:
Diffstat (limited to 'chrome/browser')
-rw-r--r--chrome/browser/DEPS1
-rw-r--r--chrome/browser/app_controller_mac.mm8
-rw-r--r--chrome/browser/cocoa/menu_localizer.h19
-rw-r--r--chrome/browser/cocoa/menu_localizer.mm33
-rw-r--r--chrome/browser/cocoa/ui_localizer.h31
-rw-r--r--chrome/browser/cocoa/ui_localizer.mm80
-rw-r--r--chrome/browser/cocoa/ui_localizer_unittest.mm46
-rw-r--r--chrome/browser/tab_contents/render_view_context_menu_mac.mm7
8 files changed, 165 insertions, 60 deletions
diff --git a/chrome/browser/DEPS b/chrome/browser/DEPS
index d5be2b8..a4b5411 100644
--- a/chrome/browser/DEPS
+++ b/chrome/browser/DEPS
@@ -10,6 +10,7 @@ include_rules = [
"+skia/include",
"+webkit/default_plugin",
"+webkit/glue", # Defines some types that are marshalled over IPC.
+ "+xib_localizers", # For generated mac localization helpers
# Other libraries.
"+chrome/third_party/hunspell",
diff --git a/chrome/browser/app_controller_mac.mm b/chrome/browser/app_controller_mac.mm
index 5205114..5cdaa1c 100644
--- a/chrome/browser/app_controller_mac.mm
+++ b/chrome/browser/app_controller_mac.mm
@@ -18,7 +18,6 @@
#import "chrome/browser/cocoa/bookmark_menu_bridge.h"
#import "chrome/browser/cocoa/clear_browsing_data_controller.h"
#import "chrome/browser/cocoa/encoding_menu_controller_delegate_mac.h"
-#import "chrome/browser/cocoa/menu_localizer.h"
#import "chrome/browser/cocoa/preferences_window_controller.h"
#import "chrome/browser/cocoa/tab_strip_controller.h"
#import "chrome/browser/cocoa/tab_window_controller.h"
@@ -28,6 +27,7 @@
#include "chrome/common/pref_service.h"
#include "chrome/browser/profile_manager.h"
#include "chrome/common/temp_scaffolding_stubs.h"
+#import "xib_localizers/main_menu_localizer.h"
@interface AppController(PRIVATE)
- (void)initMenuState;
@@ -215,12 +215,14 @@
DCHECK(g_browser_process);
g_browser_process->AddRefModule();
+ // TODO: move this into the MainMenu.xib once we clean up the startup order
+ // dependencies so that works. http://crbug.com/17380
// Create the localizer for the main menu. We can't do this in the nib
// because it's too early. Do it before we create any bookmark menus as well,
// just in case one has a title that matches any of our strings (unlikely,
// but technically possible).
- scoped_nsobject<MenuLocalizer> localizer(
- [[MenuLocalizer alloc] initWithBundle:nil]);
+ scoped_nsobject<MainMenuLocalizer> localizer(
+ [[MainMenuLocalizer alloc] initWithBundle:nil]);
[localizer localizeObject:[NSApplication sharedApplication]
recursively:YES];
diff --git a/chrome/browser/cocoa/menu_localizer.h b/chrome/browser/cocoa/menu_localizer.h
deleted file mode 100644
index 862ba20..0000000
--- a/chrome/browser/cocoa/menu_localizer.h
+++ /dev/null
@@ -1,19 +0,0 @@
-// Copyright (c) 2009 The Chromium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-#ifndef CHROME_BROWSER_COCOA_MENU_LOCALIZER_H_
-#define CHROME_BROWSER_COCOA_MENU_LOCALIZER_H_
-
-#import <Cocoa/Cocoa.h>
-
-#import "third_party/GTM/AppKit/GTMUILocalizer.h"
-
-// A subclass of GTMUILocalizer that handles localizing our main menus. It maps
-// from the ^-prefixed strings in the nib into the strings bundles.
-
-@interface MenuLocalizer : GTMUILocalizer {
-}
-@end
-
-#endif // CHROME_BROWSER_COCOA_MENU_LOCALIZER_H_
diff --git a/chrome/browser/cocoa/menu_localizer.mm b/chrome/browser/cocoa/menu_localizer.mm
deleted file mode 100644
index 7b09520..0000000
--- a/chrome/browser/cocoa/menu_localizer.mm
+++ /dev/null
@@ -1,33 +0,0 @@
-// Copyright (c) 2009 The Chromium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-#import "chrome/browser/cocoa/menu_localizer.h"
-
-#include "app/l10n_util.h"
-#include "base/sys_string_conversions.h"
-#include "grit/chromium_strings.h"
-
-@implementation MenuLocalizer
-
-// Override to map into our string bundles instead of strings plists.
-// TODO(pinkerton): This should use a string lookup table to map the string to
-// a constant.
-- (NSString *)localizedStringForString:(NSString *)string {
- if ([string isEqualToString:@"^Quit Chromium"]) {
- std::wstring quitString = l10n_util::GetString(IDS_EXIT_MAC);
- return base::SysWideToNSString(quitString);
- } else if ([string isEqualToString:@"^About Chromium"]) {
- std::wstring quitString = l10n_util::GetString(IDS_ABOUT_CHROME_TITLE);
- return base::SysWideToNSString(quitString);
- } else if ([string isEqualToString:@"^Hide Chromium"]) {
- std::wstring quitString = l10n_util::GetString(IDS_HIDE_MAC);
- return base::SysWideToNSString(quitString);
- } else if ([string isEqualToString:@"^Chromium Help"]) {
- std::wstring quitString = l10n_util::GetString(IDS_HELP_MAC);
- return base::SysWideToNSString(quitString);
- }
- return [super localizedStringForString:string];
-}
-
-@end
diff --git a/chrome/browser/cocoa/ui_localizer.h b/chrome/browser/cocoa/ui_localizer.h
new file mode 100644
index 0000000..b4702ad
--- /dev/null
+++ b/chrome/browser/cocoa/ui_localizer.h
@@ -0,0 +1,31 @@
+// Copyright (c) 2009 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef CHROME_BROWSER_COCOA_UI_LOCALIZER_H_
+#define CHROME_BROWSER_COCOA_UI_LOCALIZER_H_
+
+#include "base/basictypes.h"
+#include "base/string16.h"
+
+@class NSString;
+
+namespace ui_localizer {
+
+// Remove the Windows-style accelerator marker and change "..." into an
+// ellipsis. Returns the result in an autoreleased NSString.
+NSString* FixUpWindowsStyleLabel(const string16& label);
+
+struct ResourceMap {
+ 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);
+
+} // namespace ui_localizer
+
+#endif // CHROME_BROWSER_COCOA_UI_LOCALIZER_H_
diff --git a/chrome/browser/cocoa/ui_localizer.mm b/chrome/browser/cocoa/ui_localizer.mm
new file mode 100644
index 0000000..e9be6dc
--- /dev/null
+++ b/chrome/browser/cocoa/ui_localizer.mm
@@ -0,0 +1,80 @@
+// Copyright (c) 2009 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#import "chrome/browser/cocoa/ui_localizer.h"
+
+#import <Foundation/Foundation.h>
+
+#include "app/l10n_util.h"
+#include "base/sys_string_conversions.h"
+#include "base/logging.h"
+
+namespace ui_localizer {
+
+NSString* FixUpWindowsStyleLabel(const string16& label) {
+ const char16 kEllipsisUTF16 = 0x2026;
+ string16 ret;
+ size_t label_len = label.length();
+ ret.reserve(label_len);
+ for (size_t i = 0; i < label_len; ++i) {
+ char16 c = label[i];
+ if (c == '&') {
+ if (i + 1 < label_len && label[i + 1] == '&') {
+ ret.push_back(c);
+ ++i;
+ }
+ } else if (c == '.' && i + 2 < label_len && label[i + 1] == '.'
+ && label[i + 2] == '.') {
+ ret.push_back(kEllipsisUTF16);
+ i += 2;
+ } else {
+ ret.push_back(c);
+ }
+ }
+
+ return base::SysUTF16ToNSString(ret);
+}
+
+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 FixUpWindowsStyleLabel(
+ l10n_util::GetStringFUTF16(map_list[i].label_id, label_arg));
+ }
+
+ return FixUpWindowsStyleLabel(
+ l10n_util::GetStringUTF16(map_list[i].label_id));
+ }
+
+ // If we've passed where the string would be, give up.
+ if (strcmp_result < 0)
+ break;
+ }
+ }
+
+ // 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;
+}
+
+} // namespace ui_localizer
diff --git a/chrome/browser/cocoa/ui_localizer_unittest.mm b/chrome/browser/cocoa/ui_localizer_unittest.mm
new file mode 100644
index 0000000..a6756a9
--- /dev/null
+++ b/chrome/browser/cocoa/ui_localizer_unittest.mm
@@ -0,0 +1,46 @@
+// Copyright (c) 2009 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#import <Foundation/Foundation.h>
+
+#include "base/sys_string_conversions.h"
+#include "chrome/browser/cocoa/ui_localizer.h"
+#include "testing/gtest/include/gtest/gtest.h"
+#include "testing/platform_test.h"
+
+typedef PlatformTest UILocalizerTest;
+
+TEST_F(UILocalizerTest, FixUpWindowsStyleLabel) {
+ struct TestData {
+ NSString* input;
+ NSString* output;
+ };
+
+ TestData data[] = {
+ { @"", @"" },
+ { @"nothing", @"nothing" },
+ { @"foo &bar", @"foo bar" },
+ { @"foo &&bar", @"foo &bar" },
+ { @"foo &&&bar", @"foo &bar" },
+ { @"&foo &&bar", @"foo &bar" },
+ { @"&foo &bar", @"foo bar" },
+ { @"foo bar.", @"foo bar." },
+ { @"foo bar..", @"foo bar.." },
+ { @"foo bar...", @"foo bar\u2026" },
+ { @"foo.bar", @"foo.bar" },
+ { @"foo..bar", @"foo..bar" },
+ { @"foo...bar", @"foo\u2026bar" },
+ { @"foo...bar...", @"foo\u2026bar\u2026" },
+ };
+ for (size_t idx = 0; idx < ARRAYSIZE_UNSAFE(data); ++idx) {
+ string16 input16(base::SysNSStringToUTF16(data[idx].input));
+
+ NSString* result = ui_localizer::FixUpWindowsStyleLabel(input16);
+ EXPECT_TRUE(result != nil) << "Fixup Failed, idx = " << idx;
+
+ EXPECT_TRUE([data[idx].output isEqualTo:result])
+ << "For idx " << idx << ", expected '" << [data[idx].output UTF8String]
+ << "', got '" << [result UTF8String] << "'";
+ }
+}
diff --git a/chrome/browser/tab_contents/render_view_context_menu_mac.mm b/chrome/browser/tab_contents/render_view_context_menu_mac.mm
index bdb3dd7..0004750 100644
--- a/chrome/browser/tab_contents/render_view_context_menu_mac.mm
+++ b/chrome/browser/tab_contents/render_view_context_menu_mac.mm
@@ -7,6 +7,7 @@
#include "app/l10n_util.h"
#include "base/compiler_specific.h"
#include "base/sys_string_conversions.h"
+#import "chrome/browser/cocoa/ui_localizer.h"
#include "chrome/browser/profile.h"
#include "grit/generated_resources.h"
#include "base/scoped_nsobject.h"
@@ -70,12 +71,8 @@ void RenderViewContextMenuMac::DoInit() {
// and middle-truncate?
NSString* RenderViewContextMenuMac::PrepareLabelForDisplay(
const string16& label) {
- // Strip out any "&"'s that are windows accelerators and we don't use.
- NSMutableString* title =
- [NSMutableString stringWithString:base::SysUTF16ToNSString(label)];
+ NSString* title = ui_localizer::FixUpWindowsStyleLabel(label);
DCHECK(title);
- NSRange range = NSMakeRange(0, [title length]);
- [title replaceOccurrencesOfString:@"&" withString:@"" options:0 range:range];
return title ? title : @"";
}