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. 4 // 5 // This file defines utility functions for fetching localized resources. 6 7 #include "chrome/installer/util/l10n_string_util.h" 8 9 #include <atlbase.h> 10 11 #include <algorithm> 12 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" 18 19 namespace { 20 21 const installer::LanguageSelector& GetLanguageSelector() { 22 static const installer::LanguageSelector instance; 23 24 return instance; 25 } 26 27 installer::TranslationDelegate* g_translation_delegate = NULL; 28 29 } // namespace 30 31 namespace installer { 32 33 TranslationDelegate::~TranslationDelegate() { 34 } 35 36 void SetTranslationDelegate(TranslationDelegate* delegate) { 37 g_translation_delegate = delegate; 38 } 39 40 std::wstring GetLocalizedString(int base_message_id) { 41 if (g_translation_delegate) 42 return g_translation_delegate->GetLocalizedString(base_message_id); 43 44 std::wstring localized_string; 45 46 int message_id = base_message_id + GetLanguageSelector().offset(); 47 const ATLSTRINGRESOURCEIMAGE* image = AtlGetStringResourceImage( 48 _AtlBaseModule.GetModuleInstance(), message_id); 49 if (image) { 50 localized_string = std::wstring(image->achString, image->nLength); 51 } else { 52 NOTREACHED() << "Unable to find resource id " << message_id; 53 } 54 55 return localized_string; 56 } 57 58 base::string16 GetLocalizedStringF(int base_message_id, 59 const base::string16& a) { 60 return ReplaceStringPlaceholders(GetLocalizedString(base_message_id), 61 std::vector<base::string16>(1, a), 62 NULL); 63 } 64 65 // Here we generate the url spec with the Microsoft res:// scheme which is 66 // explained here : http://support.microsoft.com/kb/220830 67 std::wstring GetLocalizedEulaResource() { 68 wchar_t full_exe_path[MAX_PATH]; 69 int len = ::GetModuleFileName(NULL, full_exe_path, MAX_PATH); 70 if (len == 0 || len == MAX_PATH) 71 return L""; 72 73 // The resource names are more or less the upcased language names. 74 std::wstring language(GetLanguageSelector().selected_translation()); 75 std::replace(language.begin(), language.end(), L'-', L'_'); 76 StringToUpperASCII(&language); 77 78 std::wstring resource(L"IDR_OEMPG_"); 79 resource.append(language).append(L".HTML"); 80 81 // Fall back on "en" if we don't have a resource for this language. 82 if (NULL == FindResource(NULL, resource.c_str(), RT_HTML)) 83 resource = L"IDR_OEMPG_EN.HTML"; 84 85 // Spaces and DOS paths must be url encoded. 86 std::wstring url_path = 87 base::StringPrintf(L"res://%ls/#23/%ls", full_exe_path, resource.c_str()); 88 89 // The cast is safe because url_path has limited length 90 // (see the definition of full_exe_path and resource). 91 DCHECK(kuint32max > (url_path.size() * 3)); 92 DWORD count = static_cast<DWORD>(url_path.size() * 3); 93 scoped_ptr<wchar_t[]> url_canon(new wchar_t[count]); 94 HRESULT hr = ::UrlCanonicalizeW(url_path.c_str(), url_canon.get(), 95 &count, URL_ESCAPE_UNSAFE); 96 if (SUCCEEDED(hr)) 97 return std::wstring(url_canon.get()); 98 return url_path; 99 } 100 101 std::wstring GetCurrentTranslation() { 102 return GetLanguageSelector().selected_translation(); 103 } 104 105 } // namespace installer 106