Skip to content

Commit

Permalink
Remove wstrings from l10n_util/ResourceBundle locale functions.
Browse files Browse the repository at this point in the history
Review URL: http://codereview.chromium.org/3069026

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@54917 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
evan@chromium.org committed Aug 4, 2010
1 parent 568f33d commit ff622aa
Show file tree
Hide file tree
Showing 21 changed files with 62 additions and 64 deletions.
12 changes: 5 additions & 7 deletions app/l10n_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -366,9 +366,8 @@ void SplitAndNormalizeLanguageList(const std::string& env_language,

namespace l10n_util {

std::string GetApplicationLocale(const std::wstring& pref_locale) {
std::string GetApplicationLocale(const std::string& pref_locale) {
#if !defined(OS_MACOSX)

FilePath locale_path;
PathService::Get(app::DIR_LOCALES, &locale_path);
std::string resolved_locale;
Expand All @@ -382,15 +381,15 @@ std::string GetApplicationLocale(const std::wstring& pref_locale) {
#if defined(OS_WIN)
// First, try the preference value.
if (!pref_locale.empty())
candidates.push_back(WideToASCII(pref_locale));
candidates.push_back(pref_locale);

// Next, try the system locale.
candidates.push_back(system_locale);

#elif defined(OS_CHROMEOS)
// On ChromeOS, use the application locale preference.
if (!pref_locale.empty())
candidates.push_back(WideToASCII(pref_locale));
candidates.push_back(pref_locale);

#elif defined(OS_POSIX)
// On POSIX, we also check LANGUAGE environment variable, which is supported
Expand Down Expand Up @@ -432,9 +431,8 @@ std::string GetApplicationLocale(const std::wstring& pref_locale) {
// Use any override (Cocoa for the browser), otherwise use the preference
// passed to the function.
std::string app_locale = l10n_util::GetLocaleOverride();
if (app_locale.empty()) {
app_locale = WideToASCII(pref_locale);
}
if (app_locale.empty())
app_locale = pref_locale;

// The above should handle all of the cases Chrome normally hits, but for some
// unit tests, we need something to fall back too.
Expand Down
2 changes: 1 addition & 1 deletion app/l10n_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ namespace l10n_util {
// as |pref_locale|), finally, we fall back on the system locale. We only return
// a value if there's a corresponding resource DLL for the locale. Otherwise,
// we fall back to en-us.
std::string GetApplicationLocale(const std::wstring& pref_locale);
std::string GetApplicationLocale(const std::string& pref_locale);

// Given a locale code, return true if the OS is capable of supporting it.
// For instance, Oriya is not well supported on Windows XP and we return
Expand Down
66 changes: 33 additions & 33 deletions app/l10n_util_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -145,110 +145,110 @@ TEST_F(L10nUtilTest, GetAppLocale) {
// Test the support of LANGUAGE environment variable.
SetICUDefaultLocale("en-US");
env->SetEnv("LANGUAGE", "xx:fr_CA");
EXPECT_EQ("fr", l10n_util::GetApplicationLocale(L""));
EXPECT_EQ("fr", l10n_util::GetApplicationLocale(""));

env->SetEnv("LANGUAGE", "xx:yy:en_gb.utf-8@quot");
EXPECT_EQ("en-GB", l10n_util::GetApplicationLocale(L""));
EXPECT_EQ("en-GB", l10n_util::GetApplicationLocale(""));

env->SetEnv("LANGUAGE", "xx:zh-hk");
EXPECT_EQ("zh-TW", l10n_util::GetApplicationLocale(L""));
EXPECT_EQ("zh-TW", l10n_util::GetApplicationLocale(""));

// We emulate gettext's behavior here, which ignores LANG/LC_MESSAGES/LC_ALL
// when LANGUAGE is specified. If no language specified in LANGUAGE is valid,
// then just fallback to the default language, which is en-US for us.
SetICUDefaultLocale("fr-FR");
env->SetEnv("LANGUAGE", "xx:yy");
EXPECT_EQ("en-US", l10n_util::GetApplicationLocale(L""));
EXPECT_EQ("en-US", l10n_util::GetApplicationLocale(""));

env->SetEnv("LANGUAGE", "/fr:zh_CN");
EXPECT_EQ("zh-CN", l10n_util::GetApplicationLocale(L""));
EXPECT_EQ("zh-CN", l10n_util::GetApplicationLocale(""));

// Make sure the follow tests won't be affected by LANGUAGE environment
// variable.
env->UnSetVar("LANGUAGE");
#endif // defined(OS_POSIX) && !defined(OS_CHROMEOS)

SetICUDefaultLocale("en-US");
EXPECT_EQ("en-US", l10n_util::GetApplicationLocale(L""));
EXPECT_EQ("en-US", l10n_util::GetApplicationLocale(""));

SetICUDefaultLocale("xx");
EXPECT_EQ("en-US", l10n_util::GetApplicationLocale(L""));
EXPECT_EQ("en-US", l10n_util::GetApplicationLocale(""));

#if defined(OS_CHROMEOS)
// ChromeOS honors preferred locale first in GetApplicationLocale(),
// defaulting to en-US, while other targets first honor other signals.
SetICUDefaultLocale("en-GB");
EXPECT_EQ("en-US", l10n_util::GetApplicationLocale(L""));
EXPECT_EQ("en-US", l10n_util::GetApplicationLocale(""));

SetICUDefaultLocale("en-US");
EXPECT_EQ("en-GB", l10n_util::GetApplicationLocale(L"en-GB"));
EXPECT_EQ("en-GB", l10n_util::GetApplicationLocale("en-GB"));

#else // defined(OS_CHROMEOS)
SetICUDefaultLocale("en-GB");
EXPECT_EQ("en-GB", l10n_util::GetApplicationLocale(L""));
EXPECT_EQ("en-GB", l10n_util::GetApplicationLocale(""));

SetICUDefaultLocale("fr-CA");
EXPECT_EQ("fr", l10n_util::GetApplicationLocale(L""));
EXPECT_EQ("fr", l10n_util::GetApplicationLocale(""));

SetICUDefaultLocale("es-MX");
EXPECT_EQ("es-419", l10n_util::GetApplicationLocale(L""));
EXPECT_EQ("es-419", l10n_util::GetApplicationLocale(""));

SetICUDefaultLocale("es-AR");
EXPECT_EQ("es-419", l10n_util::GetApplicationLocale(L""));
EXPECT_EQ("es-419", l10n_util::GetApplicationLocale(""));

SetICUDefaultLocale("es-ES");
EXPECT_EQ("es", l10n_util::GetApplicationLocale(L""));
EXPECT_EQ("es", l10n_util::GetApplicationLocale(""));

SetICUDefaultLocale("es");
EXPECT_EQ("es", l10n_util::GetApplicationLocale(L""));
EXPECT_EQ("es", l10n_util::GetApplicationLocale(""));

SetICUDefaultLocale("zh-HK");
EXPECT_EQ("zh-TW", l10n_util::GetApplicationLocale(L""));
EXPECT_EQ("zh-TW", l10n_util::GetApplicationLocale(""));

SetICUDefaultLocale("zh-MK");
EXPECT_EQ("zh-TW", l10n_util::GetApplicationLocale(L""));
EXPECT_EQ("zh-TW", l10n_util::GetApplicationLocale(""));

SetICUDefaultLocale("zh-SG");
EXPECT_EQ("zh-CN", l10n_util::GetApplicationLocale(L""));
EXPECT_EQ("zh-CN", l10n_util::GetApplicationLocale(""));
#endif // defined (OS_CHROMEOS)

#if defined(OS_WIN)
// We don't allow user prefs for locale on linux/mac.
SetICUDefaultLocale("en-US");
EXPECT_EQ("fr", l10n_util::GetApplicationLocale(L"fr"));
EXPECT_EQ("fr", l10n_util::GetApplicationLocale(L"fr-CA"));
EXPECT_EQ("fr", l10n_util::GetApplicationLocale("fr"));
EXPECT_EQ("fr", l10n_util::GetApplicationLocale("fr-CA"));

SetICUDefaultLocale("en-US");
// Aliases iw, no, tl to he, nb, fil.
EXPECT_EQ("he", l10n_util::GetApplicationLocale(L"iw"));
EXPECT_EQ("nb", l10n_util::GetApplicationLocale(L"no"));
EXPECT_EQ("fil", l10n_util::GetApplicationLocale(L"tl"));
EXPECT_EQ("he", l10n_util::GetApplicationLocale("iw"));
EXPECT_EQ("nb", l10n_util::GetApplicationLocale("no"));
EXPECT_EQ("fil", l10n_util::GetApplicationLocale("tl"));
// es-419 and es-XX (where XX is not Spain) should be
// mapped to es-419 (Latin American Spanish).
EXPECT_EQ("es-419", l10n_util::GetApplicationLocale(L"es-419"));
EXPECT_EQ("es", l10n_util::GetApplicationLocale(L"es-ES"));
EXPECT_EQ("es-419", l10n_util::GetApplicationLocale(L"es-AR"));
EXPECT_EQ("es-419", l10n_util::GetApplicationLocale("es-419"));
EXPECT_EQ("es", l10n_util::GetApplicationLocale("es-ES"));
EXPECT_EQ("es-419", l10n_util::GetApplicationLocale("es-AR"));

SetICUDefaultLocale("es-AR");
EXPECT_EQ("es", l10n_util::GetApplicationLocale(L"es"));
EXPECT_EQ("es", l10n_util::GetApplicationLocale("es"));

SetICUDefaultLocale("zh-HK");
EXPECT_EQ("zh-CN", l10n_util::GetApplicationLocale(L"zh-CN"));
EXPECT_EQ("zh-CN", l10n_util::GetApplicationLocale("zh-CN"));

SetICUDefaultLocale("he");
EXPECT_EQ("en-US", l10n_util::GetApplicationLocale(L"en"));
EXPECT_EQ("en-US", l10n_util::GetApplicationLocale("en"));

// Amharic should be blocked unless OS is Vista or newer.
if (win_util::GetWinVersion() < win_util::WINVERSION_VISTA) {
SetICUDefaultLocale("am");
EXPECT_EQ("en-US", l10n_util::GetApplicationLocale(L""));
EXPECT_EQ("en-US", l10n_util::GetApplicationLocale(""));
SetICUDefaultLocale("en-GB");
EXPECT_EQ("en-GB", l10n_util::GetApplicationLocale(L"am"));
EXPECT_EQ("en-GB", l10n_util::GetApplicationLocale("am"));
} else {
SetICUDefaultLocale("am");
EXPECT_EQ("am", l10n_util::GetApplicationLocale(L""));
EXPECT_EQ("am", l10n_util::GetApplicationLocale(""));
SetICUDefaultLocale("en-GB");
EXPECT_EQ("am", l10n_util::GetApplicationLocale(L"am"));
EXPECT_EQ("am", l10n_util::GetApplicationLocale("am"));
}
#endif // defined(OS_WIN)

Expand Down
4 changes: 2 additions & 2 deletions app/resource_bundle.cc
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const SkColor ResourceBundle::toolbar_separator_color =

/* static */
std::string ResourceBundle::InitSharedInstance(
const std::wstring& pref_locale) {
const std::string& pref_locale) {
DCHECK(g_shared_instance_ == NULL) << "ResourceBundle initialized twice";
g_shared_instance_ = new ResourceBundle();

Expand All @@ -47,7 +47,7 @@ std::string ResourceBundle::InitSharedInstance(

/* static */
std::string ResourceBundle::ReloadSharedInstance(
const std::wstring& pref_locale) {
const std::string& pref_locale) {
DCHECK(g_shared_instance_ != NULL) << "ResourceBundle not initialized";

g_shared_instance_->UnloadLocaleResources();
Expand Down
6 changes: 3 additions & 3 deletions app/resource_bundle.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,15 @@ class ResourceBundle {
// selected.
// NOTE: Mac ignores this and always loads up resources for the language
// defined by the Cocoa UI (ie-NSBundle does the langange work).
static std::string InitSharedInstance(const std::wstring& pref_locale);
static std::string InitSharedInstance(const std::string& pref_locale);

// Changes the locale for an already-initialized ResourceBundle. Future
// calls to get strings will return the strings for this new locale. This
// has no effect on existing or future image resources. This has no effect
// on existing or future image resources, and thus does not use the lock to
// guarantee thread-safety, since all string access is expected to happen on
// the UI thread.
static std::string ReloadSharedInstance(const std::wstring& pref_locale);
static std::string ReloadSharedInstance(const std::string& pref_locale);

// Registers additional data pack files with the global ResourceBundle. When
// looking for a DataResource, we will search these files after searching the
Expand Down Expand Up @@ -200,7 +200,7 @@ class ResourceBundle {

// Try to load the locale specific strings from an external data module.
// Returns the locale that is loaded.
std::string LoadLocaleResources(const std::wstring& pref_locale);
std::string LoadLocaleResources(const std::string& pref_locale);

// Unload the locale specific strings and prepares to load new ones. See
// comments for ReloadSharedInstance().
Expand Down
2 changes: 1 addition & 1 deletion app/resource_bundle_dummy.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ namespace gfx {

/* static */
std::string ResourceBundle::InitSharedInstance(
const std::wstring& pref_locale) {
const std::string& pref_locale) {
DCHECK(g_shared_instance_ == NULL) << "ResourceBundle initialized twice";
g_shared_instance_ = new ResourceBundle();
return std::string();
Expand Down
2 changes: 1 addition & 1 deletion app/resource_bundle_posix.cc
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ void ResourceBundle::LoadCommonResources() {
}

std::string ResourceBundle::LoadLocaleResources(
const std::wstring& pref_locale) {
const std::string& pref_locale) {
DCHECK(!locale_resources_data_) << "locale.pak already loaded";
std::string app_locale = l10n_util::GetApplicationLocale(pref_locale);
FilePath locale_file_path = GetLocaleFilePath(app_locale);
Expand Down
2 changes: 1 addition & 1 deletion app/resource_bundle_win.cc
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ void ResourceBundle::LoadCommonResources() {
}

std::string ResourceBundle::LoadLocaleResources(
const std::wstring& pref_locale) {
const std::string& pref_locale) {
DCHECK(NULL == locale_resources_data_) << "locale dll already loaded";
const std::string app_locale = l10n_util::GetApplicationLocale(pref_locale);
const FilePath& locale_path = GetLocaleFilePath(app_locale);
Expand Down
2 changes: 1 addition & 1 deletion app/test_suite.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class AppTestSuite : public TestSuite {

// Force unittests to run using en-US so if we test against string
// output, it'll pass regardless of the system language.
ResourceBundle::InitSharedInstance(L"en-US");
ResourceBundle::InitSharedInstance("en-US");
}

virtual void Shutdown() {
Expand Down
4 changes: 2 additions & 2 deletions chrome/app/chrome_dll_main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -742,8 +742,8 @@ int ChromeMain(int argc, char** argv) {
// via the preference prefs::kApplicationLocale. The browser process uses
// the --lang flag to passe the value of the PrefService in here. Maybe this
// value could be passed in a different way.
ResourceBundle::InitSharedInstance(ASCIIToWide(
parsed_command_line.GetSwitchValueASCII(switches::kLang)));
ResourceBundle::InitSharedInstance(
parsed_command_line.GetSwitchValueASCII(switches::kLang));

#if defined(OS_MACOSX)
// Update the process name (need resources to get the strings, so
Expand Down
2 changes: 1 addition & 1 deletion chrome/browser/browser_main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -856,7 +856,7 @@ int BrowserMain(const MainFunctionParams& parameters) {
// On a POSIX OS other than ChromeOS, the parameter that is passed to the
// method InitSharedInstance is ignored.
std::string app_locale = ResourceBundle::InitSharedInstance(
ASCIIToWide(local_state->GetString(prefs::kApplicationLocale)));
local_state->GetString(prefs::kApplicationLocale));
g_browser_process->SetApplicationLocale(app_locale);

FilePath resources_pack_path;
Expand Down
2 changes: 1 addition & 1 deletion chrome/browser/browser_main_mac.mm
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ virtual void PreMainMessageLoopStart() {

// Before we load the nib, we need to start up the resource bundle so we
// have the strings avaiable for localization.
std::wstring pref_locale;
std::string pref_locale;
// TODO(markusheintz): Read preference pref::kApplicationLocale in order
// to enforce the application locale.
ResourceBundle::InitSharedInstance(pref_locale);
Expand Down
2 changes: 1 addition & 1 deletion chrome/browser/chromeos/input_method/candidate_window.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1053,7 +1053,7 @@ int main(int argc, char** argv) {
// ResourceBundle code now ignores the --Lang flag.
// In order to support the --Lang flag here the preference
// prefs::kApplicationLocale must be read and passed instead of L"en-US".
ResourceBundle::InitSharedInstance(L"en-US");
ResourceBundle::InitSharedInstance("en-US");

// Write logs to a file for debugging, if --logtofile=FILE_NAME is given.
const CommandLine& command_line = *CommandLine::ForCurrentProcess();
Expand Down
2 changes: 1 addition & 1 deletion chrome/browser/chromeos/login/language_switch_menu.cc
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ void LanguageSwitchMenu::SwitchLanguage(const std::string& locale) {
prefs->SavePersistentPrefs();

// Switch the locale.
ResourceBundle::ReloadSharedInstance(UTF8ToWide(locale));
ResourceBundle::ReloadSharedInstance(locale);

// Enable the keyboard layouts that are necessary for the new locale.
chromeos::input_method::EnableInputMethods(
Expand Down
2 changes: 1 addition & 1 deletion chrome/browser/chromeos/login/wizard_controller.cc
Original file line number Diff line number Diff line change
Expand Up @@ -829,7 +829,7 @@ void ShowLoginWizard(const std::string& first_screen_name,
locale = controller->GetCustomization()->initial_locale();
LOG(INFO) << "initial locale: " << locale;
if (!locale.empty()) {
ResourceBundle::ReloadSharedInstance(UTF8ToWide(locale));
ResourceBundle::ReloadSharedInstance(locale);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion chrome/common/extensions/extension_resource_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ TEST(ExtensionResourceTest, CreateWithAllResourcesOnDisk) {
ASSERT_TRUE(file_util::CreateDirectory(l10n_path));

std::vector<std::string> locales;
extension_l10n_util::GetParentLocales(l10n_util::GetApplicationLocale(L""),
extension_l10n_util::GetParentLocales(l10n_util::GetApplicationLocale(""),
&locales);
ASSERT_FALSE(locales.empty());
for (size_t i = 0; i < locales.size(); i++) {
Expand Down
2 changes: 1 addition & 1 deletion chrome/common/extensions/extension_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ TEST(ExtensionTest, InitFromValueValidNameInRTL) {
GtkTextDirection gtk_dir = gtk_widget_get_default_direction();
gtk_widget_set_default_direction(GTK_TEXT_DIR_RTL);
#else
std::string locale = l10n_util::GetApplicationLocale(std::wstring());
std::string locale = l10n_util::GetApplicationLocale("");
base::i18n::SetICUDefaultLocale("he");
#endif

Expand Down
2 changes: 1 addition & 1 deletion chrome/test/unit/chrome_test_suite.h
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ class ChromeTestSuite : public TestSuite {

// Force unittests to run using en-US so if we test against string
// output, it'll pass regardless of the system language.
ResourceBundle::InitSharedInstance(L"en-US");
ResourceBundle::InitSharedInstance("en-US");

// initialize the global StatsTable for unit_tests (make sure the file
// doesn't exist before opening it so the test gets a clean slate)
Expand Down
2 changes: 1 addition & 1 deletion chrome_frame/test/net/fake_external_tab.cc
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ void FakeExternalTab::Initialize() {
DCHECK(res_mod);
_AtlBaseModule.SetResourceInstance(res_mod);

ResourceBundle::InitSharedInstance(L"en-US");
ResourceBundle::InitSharedInstance("en-US");

CommandLine* cmd = CommandLine::ForCurrentProcess();
cmd->AppendSwitch(switches::kDisableWebResources);
Expand Down
4 changes: 2 additions & 2 deletions views/controls/label_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,7 @@ TEST(LabelTest, DrawSingleLineStringInRTL) {
Label label;
label.SetFocusable(false);

std::string locale = l10n_util::GetApplicationLocale(std::wstring());
std::string locale = l10n_util::GetApplicationLocale("");
base::i18n::SetICUDefaultLocale("he");

std::wstring test_text(L"Here's a string with no returns.");
Expand Down Expand Up @@ -672,7 +672,7 @@ TEST(LabelTest, DrawMultiLineStringInRTL) {
label.SetFocusable(false);

// Test for RTL.
std::string locale = l10n_util::GetApplicationLocale(std::wstring());
std::string locale = l10n_util::GetApplicationLocale("");
base::i18n::SetICUDefaultLocale("he");

std::wstring test_text(L"Another string\nwith returns\n\n!");
Expand Down
2 changes: 1 addition & 1 deletion views/examples/examples_main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ void ExamplesMain::Run() {

icu_util::Initialize();

ResourceBundle::InitSharedInstance(L"en-US");
ResourceBundle::InitSharedInstance("en-US");

MessageLoop main_message_loop(MessageLoop::TYPE_UI);

Expand Down

0 comments on commit ff622aa

Please sign in to comment.