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
3 // found in the LICENSE file.
5 // This file defines utility functions for fetching localized resources.
7 #include "chrome/installer/util/l10n_string_util.h"
13 #include "base/logging.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/strings/string_util.h"
16 #include "base/strings/stringprintf.h"
17 #include "chrome/installer/util/language_selector.h"
21 const installer::LanguageSelector
& GetLanguageSelector() {
22 static const installer::LanguageSelector instance
;
27 installer::TranslationDelegate
* g_translation_delegate
= NULL
;
33 TranslationDelegate::~TranslationDelegate() {
36 void SetTranslationDelegate(TranslationDelegate
* delegate
) {
37 g_translation_delegate
= delegate
;
40 std::wstring
GetLocalizedString(int base_message_id
) {
41 if (g_translation_delegate
)
42 return g_translation_delegate
->GetLocalizedString(base_message_id
);
44 std::wstring localized_string
;
46 int message_id
= base_message_id
+ GetLanguageSelector().offset();
47 const ATLSTRINGRESOURCEIMAGE
* image
= AtlGetStringResourceImage(
48 _AtlBaseModule
.GetModuleInstance(), message_id
);
50 localized_string
= std::wstring(image
->achString
, image
->nLength
);
52 NOTREACHED() << "Unable to find resource id " << message_id
;
55 return localized_string
;
58 // Here we generate the url spec with the Microsoft res:// scheme which is
59 // explained here : http://support.microsoft.com/kb/220830
60 std::wstring
GetLocalizedEulaResource() {
61 wchar_t full_exe_path
[MAX_PATH
];
62 int len
= ::GetModuleFileName(NULL
, full_exe_path
, MAX_PATH
);
63 if (len
== 0 || len
== MAX_PATH
)
66 // The resource names are more or less the upcased language names.
67 std::wstring
language(GetLanguageSelector().selected_translation());
68 std::replace(language
.begin(), language
.end(), L
'-', L
'_');
69 StringToUpperASCII(&language
);
71 std::wstring
resource(L
"IDR_OEMPG_");
72 resource
.append(language
).append(L
".HTML");
74 // Fall back on "en" if we don't have a resource for this language.
75 if (NULL
== FindResource(NULL
, resource
.c_str(), RT_HTML
))
76 resource
= L
"IDR_OEMPG_EN.HTML";
78 // Spaces and DOS paths must be url encoded.
79 std::wstring url_path
=
80 base::StringPrintf(L
"res://%ls/#23/%ls", full_exe_path
, resource
.c_str());
82 // The cast is safe because url_path has limited length
83 // (see the definition of full_exe_path and resource).
84 DCHECK(kuint32max
> (url_path
.size() * 3));
85 DWORD count
= static_cast<DWORD
>(url_path
.size() * 3);
86 scoped_ptr
<wchar_t[]> url_canon(new wchar_t[count
]);
87 HRESULT hr
= ::UrlCanonicalizeW(url_path
.c_str(), url_canon
.get(),
88 &count
, URL_ESCAPE_UNSAFE
);
90 return std::wstring(url_canon
.get());
94 std::wstring
GetCurrentTranslation() {
95 return GetLanguageSelector().selected_translation();
98 } // namespace installer