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

Side by Side Diff: ui/base/l10n/l10n_util.cc

Issue 23095006: If user profile doesn't contain language setting, default to his Google account settings. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix shared build. Created 7 years, 4 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
« chrome/test/base/testing_profile.h ('K') | « ui/base/l10n/l10n_util.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 #include "ui/base/l10n/l10n_util.h" 5 #include "ui/base/l10n/l10n_util.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <cstdlib> 8 #include <cstdlib>
9 #include <iterator> 9 #include <iterator>
10 #include <string> 10 #include <string>
(...skipping 227 matching lines...) Expand 10 before | Expand all | Expand 10 after
238 // If the ResourceBundle is not yet initialized, return false to avoid the 238 // If the ResourceBundle is not yet initialized, return false to avoid the
239 // CHECK failure in ResourceBundle::GetSharedInstance(). 239 // CHECK failure in ResourceBundle::GetSharedInstance().
240 if (!ResourceBundle::HasSharedInstance()) 240 if (!ResourceBundle::HasSharedInstance())
241 return false; 241 return false;
242 242
243 // TODO(hshi): make ResourceBundle::LocaleDataPakExists() a static function 243 // TODO(hshi): make ResourceBundle::LocaleDataPakExists() a static function
244 // so that this can be invoked without initializing the global instance. 244 // so that this can be invoked without initializing the global instance.
245 // See crbug.com/230432: CHECK failure in GetUserDataDir(). 245 // See crbug.com/230432: CHECK failure in GetUserDataDir().
246 return ResourceBundle::GetSharedInstance().LocaleDataPakExists(locale); 246 return ResourceBundle::GetSharedInstance().LocaleDataPakExists(locale);
247 } 247 }
248 #endif
248 249
250 // On Linux, the text layout engine Pango determines paragraph directionality
251 // by looking at the first strongly-directional character in the text. This
252 // means text such as "Google Chrome foo bar..." will be layed out LTR even
253 // if "foo bar" is RTL. So this function prepends the necessary RLM in such
254 // cases.
255 void AdjustParagraphDirectionality(string16* paragraph) {
256 #if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_ANDROID)
257 if (base::i18n::IsRTL() &&
258 base::i18n::StringContainsStrongRTLChars(*paragraph)) {
259 paragraph->insert(0, 1, static_cast<char16>(base::i18n::kRightToLeftMark));
260 }
261 #endif
262 }
263
264 #if defined(OS_WIN)
265 std::string GetCanonicalLocale(const std::string& locale) {
266 return base::i18n::GetCanonicalLocale(locale.c_str());
267 }
268 #endif
269
270 struct AvailableLocalesTraits :
271 base::DefaultLazyInstanceTraits<std::vector<std::string> > {
272 static std::vector<std::string>* New(void* instance) {
273 std::vector<std::string>* locales =
274 base::DefaultLazyInstanceTraits<std::vector<std::string> >::New(
275 instance);
276 int num_locales = uloc_countAvailable();
277 for (int i = 0; i < num_locales; ++i) {
278 std::string locale_name = uloc_getAvailable(i);
279 // Filter out the names that have aliases.
280 if (IsDuplicateName(locale_name))
281 continue;
282 // Filter out locales for which we have only partially populated data
283 // and to which Chrome is not localized.
284 if (IsLocalePartiallyPopulated(locale_name))
285 continue;
286 if (!l10n_util::IsLocaleSupportedByOS(locale_name))
287 continue;
288 // Normalize underscores to hyphens because that's what our locale files
289 // use.
290 std::replace(locale_name.begin(), locale_name.end(), '_', '-');
291
292 // Map the Chinese locale names over to zh-CN and zh-TW.
293 if (LowerCaseEqualsASCII(locale_name, "zh-hans")) {
294 locale_name = "zh-CN";
295 } else if (LowerCaseEqualsASCII(locale_name, "zh-hant")) {
296 locale_name = "zh-TW";
297 }
298 locales->push_back(locale_name);
299 }
300
301 // Manually add 'es-419' to the list. See the comment in IsDuplicateName().
302 locales->push_back("es-419");
303 return locales;
304 }
305 };
306
307 base::LazyInstance<std::vector<std::string>, AvailableLocalesTraits >
308 g_available_locales = LAZY_INSTANCE_INITIALIZER;
309
310 } // namespace
311
312 namespace l10n_util {
313
314 #if !defined(OS_MACOSX)
249 bool CheckAndResolveLocale(const std::string& locale, 315 bool CheckAndResolveLocale(const std::string& locale,
250 std::string* resolved_locale) { 316 std::string* resolved_locale) {
251 if (IsLocaleAvailable(locale)) { 317 if (IsLocaleAvailable(locale)) {
252 *resolved_locale = locale; 318 *resolved_locale = locale;
253 return true; 319 return true;
254 } 320 }
255 321
256 // If there's a variant, skip over it so we can try without the region 322 // If there's a variant, skip over it so we can try without the region
257 // code. For example, ca_ES@valencia should cause us to try ca@valencia 323 // code. For example, ca_ES@valencia should cause us to try ca@valencia
258 // before ca. 324 // before ca.
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
321 resolved_locale->swap(tmp_locale); 387 resolved_locale->swap(tmp_locale);
322 return true; 388 return true;
323 } 389 }
324 } 390 }
325 } 391 }
326 392
327 return false; 393 return false;
328 } 394 }
329 #endif 395 #endif
330 396
331 // On Linux, the text layout engine Pango determines paragraph directionality
332 // by looking at the first strongly-directional character in the text. This
333 // means text such as "Google Chrome foo bar..." will be layed out LTR even
334 // if "foo bar" is RTL. So this function prepends the necessary RLM in such
335 // cases.
336 void AdjustParagraphDirectionality(string16* paragraph) {
337 #if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_ANDROID)
338 if (base::i18n::IsRTL() &&
339 base::i18n::StringContainsStrongRTLChars(*paragraph)) {
340 paragraph->insert(0, 1, static_cast<char16>(base::i18n::kRightToLeftMark));
341 }
342 #endif
343 }
344
345 #if defined(OS_WIN)
346 std::string GetCanonicalLocale(const std::string& locale) {
347 return base::i18n::GetCanonicalLocale(locale.c_str());
348 }
349 #endif
350
351 struct AvailableLocalesTraits :
352 base::DefaultLazyInstanceTraits<std::vector<std::string> > {
353 static std::vector<std::string>* New(void* instance) {
354 std::vector<std::string>* locales =
355 base::DefaultLazyInstanceTraits<std::vector<std::string> >::New(
356 instance);
357 int num_locales = uloc_countAvailable();
358 for (int i = 0; i < num_locales; ++i) {
359 std::string locale_name = uloc_getAvailable(i);
360 // Filter out the names that have aliases.
361 if (IsDuplicateName(locale_name))
362 continue;
363 // Filter out locales for which we have only partially populated data
364 // and to which Chrome is not localized.
365 if (IsLocalePartiallyPopulated(locale_name))
366 continue;
367 if (!l10n_util::IsLocaleSupportedByOS(locale_name))
368 continue;
369 // Normalize underscores to hyphens because that's what our locale files
370 // use.
371 std::replace(locale_name.begin(), locale_name.end(), '_', '-');
372
373 // Map the Chinese locale names over to zh-CN and zh-TW.
374 if (LowerCaseEqualsASCII(locale_name, "zh-hans")) {
375 locale_name = "zh-CN";
376 } else if (LowerCaseEqualsASCII(locale_name, "zh-hant")) {
377 locale_name = "zh-TW";
378 }
379 locales->push_back(locale_name);
380 }
381
382 // Manually add 'es-419' to the list. See the comment in IsDuplicateName().
383 locales->push_back("es-419");
384 return locales;
385 }
386 };
387
388 base::LazyInstance<std::vector<std::string>, AvailableLocalesTraits >
389 g_available_locales = LAZY_INSTANCE_INITIALIZER;
390
391 } // namespace
392
393 namespace l10n_util {
394
395 std::string GetApplicationLocale(const std::string& pref_locale) { 397 std::string GetApplicationLocale(const std::string& pref_locale) {
396 #if defined(OS_MACOSX) 398 #if defined(OS_MACOSX)
397 399
398 // Use any override (Cocoa for the browser), otherwise use the preference 400 // Use any override (Cocoa for the browser), otherwise use the preference
399 // passed to the function. 401 // passed to the function.
400 std::string app_locale = l10n_util::GetLocaleOverride(); 402 std::string app_locale = l10n_util::GetLocaleOverride();
401 if (app_locale.empty()) 403 if (app_locale.empty())
402 app_locale = pref_locale; 404 app_locale = pref_locale;
403 405
404 // The above should handle all of the cases Chrome normally hits, but for some 406 // The above should handle all of the cases Chrome normally hits, but for some
(...skipping 448 matching lines...) Expand 10 before | Expand all | Expand 10 after
853 } 855 }
854 856
855 int GetLocalizedContentsWidthInPixels(int pixel_resource_id) { 857 int GetLocalizedContentsWidthInPixels(int pixel_resource_id) {
856 int width = 0; 858 int width = 0;
857 base::StringToInt(l10n_util::GetStringUTF8(pixel_resource_id), &width); 859 base::StringToInt(l10n_util::GetStringUTF8(pixel_resource_id), &width);
858 DCHECK_GT(width, 0); 860 DCHECK_GT(width, 0);
859 return width; 861 return width;
860 } 862 }
861 863
862 } // namespace l10n_util 864 } // namespace l10n_util
OLDNEW
« chrome/test/base/testing_profile.h ('K') | « ui/base/l10n/l10n_util.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698