|
OLD | NEW |
---|---|
(Empty) | |
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/cocoa/ui_localizer.h" | |
6 | |
7 #import <Foundation/Foundation.h> | |
8 | |
9 #include "app/l10n_util.h" | |
10 #include "base/sys_string_conversions.h" | |
11 #include "base/logging.h" | |
12 | |
13 namespace ui_localizer { | |
14 | |
15 NSString* FixUpWindowsStyleLabel(const string16& label) { | |
16 const char16 ellipsis_utf16 = 0x2026; | |
Mark Mentovai
2009/07/21 21:23:52
pink suggested kEllipsisUTF16 because it's konstan
| |
17 string16 ret; | |
18 size_t label_len = label.length(); | |
19 ret.reserve(label_len); | |
20 for (size_t i = 0; i < label_len; ++i) { | |
21 char16 c = label[i]; | |
22 if (c == '&') { | |
23 if (i + 1 < label_len && '&' == label[i + 1]) { | |
Mark Mentovai
2009/07/21 21:23:52
label[i + 1] == '&' to placate pink again.
| |
24 ret.push_back(c); | |
25 ++i; | |
26 } | |
27 } else if (c == '.' && i + 2 < label_len && label[i + 1] == '.' | |
28 && label[i + 2] == '.') { | |
29 ret.push_back(ellipsis_utf16); | |
30 i += 2; | |
31 } else { | |
32 ret.push_back(c); | |
33 } | |
34 } | |
35 | |
36 return base::SysUTF16ToNSString(ret); | |
37 } | |
38 | |
39 NSString* LocalizedStringForKeyFromMapList(NSString* key, | |
40 const ResourceMap* map_list, | |
41 size_t map_list_len) { | |
42 DCHECK(key != nil); | |
43 DCHECK(map_list != NULL); | |
44 | |
45 // Look up the string for the resource id to fetch. | |
46 const char* utf8_key = [key UTF8String]; | |
47 if (utf8_key) { | |
48 // If we end up with enough string constants in here, look at using bsearch | |
49 // to speed up the searching. | |
50 for (size_t i = 0; i < map_list_len; ++i) { | |
51 int strcmp_result = strcmp(utf8_key, map_list[i].name); | |
52 if (strcmp_result == 0) { | |
53 // Do we need to build the string, or just fetch it? | |
54 if (map_list[i].label_arg_id != 0) { | |
55 const string16 label_arg( | |
56 l10n_util::GetStringUTF16(map_list[i].label_arg_id)); | |
57 return FixUpWindowsStyleLabel( | |
58 l10n_util::GetStringFUTF16(map_list[i].label_id, label_arg)); | |
59 } | |
60 | |
61 return FixUpWindowsStyleLabel( | |
62 l10n_util::GetStringUTF16(map_list[i].label_id)); | |
63 | |
Mark Mentovai
2009/07/21 21:23:52
This blank line is not helpful.
| |
64 } | |
65 | |
66 // If we've passed where the string would be, give up. | |
67 if (strcmp_result < 0) | |
68 break; | |
69 } | |
70 } | |
71 | |
72 // Sanity check, there shouldn't be any strings with this id that aren't | |
73 // in our map. | |
74 DLOG_IF(WARNING, [key hasPrefix:@"^ID"]) << "Key '" << utf8_key | |
75 << "' wasn't in the resource map?"; | |
76 | |
77 // If we didn't find anything, this string doesn't need localizing. | |
78 return nil; | |
79 } | |
80 | |
81 } // namespace ui_localizer | |
OLD | NEW |