Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(153)

Side by Side Diff: chrome/installer/util/l10n_string_util.cc

Issue 2791593002: Allow installer::GetLocalizedString to return mode-specific strings. (Closed)
Patch Set: manzagop review part the deux Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 // This file defines utility functions for fetching localized resources. 5 // This file defines utility functions for fetching localized resources.
6 6
7 #include "chrome/installer/util/l10n_string_util.h" 7 #include "chrome/installer/util/l10n_string_util.h"
8 8
9 #include <atlbase.h> 9 #include <atlbase.h>
10 #include <stdint.h> 10 #include <stdint.h>
11 11
12 #include <algorithm> 12 #include <algorithm>
13 #include <limits> 13 #include <limits>
14 #include <memory> 14 #include <memory>
15 15
16 #include "base/logging.h" 16 #include "base/logging.h"
17 #include "base/strings/string_util.h" 17 #include "base/strings/string_util.h"
18 #include "base/strings/stringprintf.h" 18 #include "base/strings/stringprintf.h"
19 #include "chrome/install_static/install_details.h"
20 #include "chrome/install_static/install_modes.h"
21 #include "chrome/installer/util/installer_util_strings.h"
19 #include "chrome/installer/util/language_selector.h" 22 #include "chrome/installer/util/language_selector.h"
20 23
21 namespace { 24 namespace {
22 25
23 const installer::LanguageSelector& GetLanguageSelector() { 26 const installer::LanguageSelector& GetLanguageSelector() {
24 static const installer::LanguageSelector instance; 27 static const installer::LanguageSelector instance;
25 28
26 return instance; 29 return instance;
27 } 30 }
28 31
29 installer::TranslationDelegate* g_translation_delegate = NULL; 32 installer::TranslationDelegate* g_translation_delegate = NULL;
30 33
31 } // namespace 34 } // namespace
32 35
33 namespace installer { 36 namespace installer {
34 37
35 TranslationDelegate::~TranslationDelegate() { 38 TranslationDelegate::~TranslationDelegate() {
36 } 39 }
37 40
38 void SetTranslationDelegate(TranslationDelegate* delegate) { 41 void SetTranslationDelegate(TranslationDelegate* delegate) {
39 g_translation_delegate = delegate; 42 g_translation_delegate = delegate;
40 } 43 }
41 44
42 std::wstring GetLocalizedString(int base_message_id) { 45 std::wstring GetLocalizedString(int base_message_id) {
46 // Map |base_message_id| to the base id for the current install mode.
47 base_message_id = GetBaseMessageIdForMode(base_message_id);
48
43 if (g_translation_delegate) 49 if (g_translation_delegate)
44 return g_translation_delegate->GetLocalizedString(base_message_id); 50 return g_translation_delegate->GetLocalizedString(base_message_id);
45 51
46 std::wstring localized_string; 52 std::wstring localized_string;
47 53
48 int message_id = base_message_id + GetLanguageSelector().offset(); 54 int message_id = base_message_id + GetLanguageSelector().offset();
49 const ATLSTRINGRESOURCEIMAGE* image = AtlGetStringResourceImage( 55 const ATLSTRINGRESOURCEIMAGE* image = AtlGetStringResourceImage(
50 _AtlBaseModule.GetModuleInstance(), message_id); 56 _AtlBaseModule.GetModuleInstance(), message_id);
51 if (image) { 57 if (image) {
52 localized_string = std::wstring(image->achString, image->nLength); 58 localized_string = std::wstring(image->achString, image->nLength);
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
98 &count, URL_ESCAPE_UNSAFE); 104 &count, URL_ESCAPE_UNSAFE);
99 if (SUCCEEDED(hr)) 105 if (SUCCEEDED(hr))
100 return std::wstring(url_canon.get()); 106 return std::wstring(url_canon.get());
101 return url_path; 107 return url_path;
102 } 108 }
103 109
104 std::wstring GetCurrentTranslation() { 110 std::wstring GetCurrentTranslation() {
105 return GetLanguageSelector().selected_translation(); 111 return GetLanguageSelector().selected_translation();
106 } 112 }
107 113
114 int GetBaseMessageIdForMode(int base_message_id) {
115 // Generate the constants holding the mode-specific resource ID arrays.
116 #define HANDLE_MODE_STRING(id, ...) \
117 static constexpr int k##id##Strings[] = {__VA_ARGS__}; \
118 static_assert( \
119 arraysize(k##id##Strings) == install_static::NUM_INSTALL_MODES, \
120 "resource " #id \
121 " has the wrong number of mode-specific " \
122 "strings.");
123 DO_MODE_STRINGS
124 #undef HANDLE_MODE_STRING
125
126 const int* mode_strings = nullptr;
127 switch (base_message_id) {
128 // Generate the cases mapping each mode-specific resource ID to its array.
129 #define HANDLE_MODE_STRING(id, ...) \
130 case id: \
131 mode_strings = &k##id##Strings[0]; \
132 break;
133 DO_MODE_STRINGS
134 #undef HANDLE_MODE_STRING
135 default:
136 // This ID has no per-mode variants.
137 return base_message_id;
138 }
139
140 // Return the variant of |base_message_id| for the current mode.
141 return mode_strings[install_static::InstallDetails::Get()
142 .install_mode_index()];
143 }
144
108 } // namespace installer 145 } // namespace installer
OLDNEW
« no previous file with comments | « chrome/installer/util/l10n_string_util.h ('k') | chrome/installer/util/l10n_string_util_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698