diff options
Diffstat (limited to 'app')
-rw-r--r-- | app/app.gyp | 3 | ||||
-rw-r--r-- | app/l10n_util.h | 8 | ||||
-rw-r--r-- | app/l10n_util_mac.h | 68 | ||||
-rw-r--r-- | app/l10n_util_mac.mm | 106 | ||||
-rw-r--r-- | app/l10n_util_mac_unittest.mm | 46 |
5 files changed, 231 insertions, 0 deletions
diff --git a/app/app.gyp b/app/app.gyp index 5c989ae..e63ff82 100644 --- a/app/app.gyp +++ b/app/app.gyp @@ -88,6 +88,8 @@ 'gfx/text_elider.h', 'l10n_util.cc', 'l10n_util.h', + 'l10n_util_mac.h', + 'l10n_util_mac.mm', 'l10n_util_posix.cc', 'l10n_util_win.cc', 'l10n_util_win.h', @@ -170,6 +172,7 @@ 'gfx/font_unittest.cc', 'gfx/icon_util_unittest.cc', 'gfx/text_elider_unittest.cc', + 'l10n_util_mac_unittest.mm', 'l10n_util_unittest.cc', 'os_exchange_data_win_unittest.cc', 'run_all_unittests.cc', diff --git a/app/l10n_util.h b/app/l10n_util.h index 464c3b8..0944912 100644 --- a/app/l10n_util.h +++ b/app/l10n_util.h @@ -26,6 +26,10 @@ #include "unicode/ubidi.h" #include "unicode/uchar.h" +#if defined(OS_MACOSX) +#include "app/l10n_util.h" +#endif // OS_MACOSX + class FilePath; class PrefService; @@ -66,6 +70,10 @@ string16 GetDisplayNameForLocale(const std::string& locale_code, const std::string& display_locale, bool is_for_ui); +// +// Mac Note: See l10n_util_mac.h for some NSString versions and other support. +// + // Pulls resource string from the string bundle and returns it. std::wstring GetString(int message_id); std::string GetStringUTF8(int message_id); diff --git a/app/l10n_util_mac.h b/app/l10n_util_mac.h new file mode 100644 index 0000000..87ccb05 --- /dev/null +++ b/app/l10n_util_mac.h @@ -0,0 +1,68 @@ +// 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 APP_L10N_UTIL_MAC_H_ +#define APP_L10N_UTIL_MAC_H_ + +#include "app/l10n_util.h" +#include "base/basictypes.h" +#include "base/string16.h" + +#ifdef __OBJC__ +@class NSString; +#else +class NSString; +#endif + +namespace l10n_util { + +// Remove the Windows-style accelerator marker (for labels, menuitems, etc.) +// and change "..." into an ellipsis. +// Returns the result in an autoreleased NSString. +NSString* FixUpWindowsStyleLabel(const string16& label); + +// Pulls resource string from the string bundle and returns it. +NSString* GetNSString(int message_id); + +// Get a resource string and replace $1-$2-$3 with |a| and |b| +// respectively. Additionally, $$ is replaced by $. +NSString* GetNSStringF(int message_id, + const string16& a); +NSString* GetNSStringF(int message_id, + const string16& a, + const string16& b); +NSString* GetNSStringF(int message_id, + const string16& a, + const string16& b, + const string16& c); +NSString* GetNSStringF(int message_id, + const string16& a, + const string16& b, + const string16& c, + const string16& d); + +// Same as GetNSString, but runs the result through FixUpWindowsStyleLabel +// before returning it. +NSString* GetNSStringWithFixup(int message_id); + +// Same as GetNSStringF, but runs the result through FixUpWindowsStyleLabel +// before returning it. +NSString* GetNSStringFWithFixup(int message_id, + const string16& a); +NSString* GetNSStringFWithFixup(int message_id, + const string16& a, + const string16& b); +NSString* GetNSStringFWithFixup(int message_id, + const string16& a, + const string16& b, + const string16& c); +NSString* GetNSStringFWithFixup(int message_id, + const string16& a, + const string16& b, + const string16& c, + const string16& d); + +} // namespace l10n_util + +#endif // APP_L10N_UTIL_MAC_H_ diff --git a/app/l10n_util_mac.mm b/app/l10n_util_mac.mm new file mode 100644 index 0000000..a664eb3 --- /dev/null +++ b/app/l10n_util_mac.mm @@ -0,0 +1,106 @@ +// 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 "app/l10n_util_mac.h" +#include "base/sys_string_conversions.h" + +namespace l10n_util { + +// Remove the Windows-style accelerator marker and change "..." into an +// ellipsis. Returns the result in an autoreleased NSString. +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* GetNSString(int message_id) { + return base::SysUTF16ToNSString(l10n_util::GetStringUTF16(message_id)); +} + +NSString* GetNSStringF(int message_id, + const string16& a) { + return base::SysUTF16ToNSString(l10n_util::GetStringFUTF16(message_id, + a)); +} + +NSString* GetNSStringF(int message_id, + const string16& a, + const string16& b) { + return base::SysUTF16ToNSString(l10n_util::GetStringFUTF16(message_id, + a, b)); +} + +NSString* GetNSStringF(int message_id, + const string16& a, + const string16& b, + const string16& c) { + return base::SysUTF16ToNSString(l10n_util::GetStringFUTF16(message_id, + a, b, c)); +} + +NSString* GetNSStringF(int message_id, + const string16& a, + const string16& b, + const string16& c, + const string16& d) { + return base::SysUTF16ToNSString(l10n_util::GetStringFUTF16(message_id, + a, b, c, d)); +} + +NSString* GetNSStringWithFixup(int message_id) { + return FixUpWindowsStyleLabel(l10n_util::GetStringUTF16(message_id)); +} + +NSString* GetNSStringFWithFixup(int message_id, + const string16& a) { + return FixUpWindowsStyleLabel(l10n_util::GetStringFUTF16(message_id, + a)); +} + +NSString* GetNSStringFWithFixup(int message_id, + const string16& a, + const string16& b) { + return FixUpWindowsStyleLabel(l10n_util::GetStringFUTF16(message_id, + a, b)); +} + +NSString* GetNSStringFWithFixup(int message_id, + const string16& a, + const string16& b, + const string16& c) { + return FixUpWindowsStyleLabel(l10n_util::GetStringFUTF16(message_id, + a, b, c)); +} + +NSString* GetNSStringFWithFixup(int message_id, + const string16& a, + const string16& b, + const string16& c, + const string16& d) { + return FixUpWindowsStyleLabel(l10n_util::GetStringFUTF16(message_id, + a, b, c, d)); +} + + +} // namespace l10n_util diff --git a/app/l10n_util_mac_unittest.mm b/app/l10n_util_mac_unittest.mm new file mode 100644 index 0000000..e6c4485 --- /dev/null +++ b/app/l10n_util_mac_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 "app/l10n_util_mac.h" +#include "testing/gtest/include/gtest/gtest.h" +#include "testing/platform_test.h" + +typedef PlatformTest L10nUtilMacTest; + +TEST_F(L10nUtilMacTest, 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 = l10n_util::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] << "'"; + } +} |