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

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

Issue 2791593002: Allow installer::GetLocalizedString to return mode-specific strings. (Closed)
Patch Set: 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
34 // Returns the mode-specific message id for |base_message_id| given the current
35 // install mode. Returns |base_message_id| itself if it does not have per-mode
36 // variants. See MODE_SPECIFIC_STRINGS in create_string_rc.py for details..
37 int GetBaseMessageIdForMode(int base_message_id) {
38 // Generate the constants holding the mode-specific resource ID arrays.
39 #define HANDLE_MODE_STRING(id, ...) \
40 static constexpr int k##id##Strings[] = {__VA_ARGS__}; \
41 static_assert( \
42 arraysize(k##id##Strings) == install_static::NUM_INSTALL_MODES, \
43 "resource " #id \
44 " has the wrong number of mode-specific " \
45 "strings.");
46 DO_MODE_STRINGS
47 #undef HANDLE_MODE_STRING
48
49 const int* mode_strings = nullptr;
50 switch (base_message_id) {
51 // Generate the cases mapping each mode-specific resource ID to its array.
52 #define HANDLE_MODE_STRING(id, ...) \
53 case id: \
54 mode_strings = &k##id##Strings[0]; \
55 break;
56 DO_MODE_STRINGS
57 #undef HANDLE_MODE_STRING
58 default:
59 // This ID has no per-mode variants.
60 return base_message_id;
61 }
62
63 // Return the variant of |base_message_id| for the current mode.
64 return mode_strings[install_static::InstallDetails::Get()
65 .install_mode_index()];
66 }
67
31 } // namespace 68 } // namespace
32 69
33 namespace installer { 70 namespace installer {
34 71
35 TranslationDelegate::~TranslationDelegate() { 72 TranslationDelegate::~TranslationDelegate() {
36 } 73 }
37 74
38 void SetTranslationDelegate(TranslationDelegate* delegate) { 75 void SetTranslationDelegate(TranslationDelegate* delegate) {
39 g_translation_delegate = delegate; 76 g_translation_delegate = delegate;
40 } 77 }
41 78
42 std::wstring GetLocalizedString(int base_message_id) { 79 std::wstring GetLocalizedString(int base_message_id) {
80 // Map |base_message_id| to the base id for the current install mode.
81 base_message_id = GetBaseMessageIdForMode(base_message_id);
82
43 if (g_translation_delegate) 83 if (g_translation_delegate)
44 return g_translation_delegate->GetLocalizedString(base_message_id); 84 return g_translation_delegate->GetLocalizedString(base_message_id);
45 85
46 std::wstring localized_string; 86 std::wstring localized_string;
47 87
48 int message_id = base_message_id + GetLanguageSelector().offset(); 88 int message_id = base_message_id + GetLanguageSelector().offset();
49 const ATLSTRINGRESOURCEIMAGE* image = AtlGetStringResourceImage( 89 const ATLSTRINGRESOURCEIMAGE* image = AtlGetStringResourceImage(
50 _AtlBaseModule.GetModuleInstance(), message_id); 90 _AtlBaseModule.GetModuleInstance(), message_id);
51 if (image) { 91 if (image) {
52 localized_string = std::wstring(image->achString, image->nLength); 92 localized_string = std::wstring(image->achString, image->nLength);
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
99 if (SUCCEEDED(hr)) 139 if (SUCCEEDED(hr))
100 return std::wstring(url_canon.get()); 140 return std::wstring(url_canon.get());
101 return url_path; 141 return url_path;
102 } 142 }
103 143
104 std::wstring GetCurrentTranslation() { 144 std::wstring GetCurrentTranslation() {
105 return GetLanguageSelector().selected_translation(); 145 return GetLanguageSelector().selected_translation();
106 } 146 }
107 147
108 } // namespace installer 148 } // namespace installer
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698