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

Side by Side Diff: Source/platform/Language.cpp

Issue 703043002: Language.{h,cpp} clean-up. (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Created 6 years, 1 month 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 | Annotate | Revision Log
« no previous file with comments | « Source/platform/Language.h ('k') | Source/web/resources/calendarPicker.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2010, 2013 Apple Inc. All rights reserved. 2 * Copyright (C) 2010, 2013 Apple Inc. All rights reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions 5 * modification, are permitted provided that the following conditions
6 * are met: 6 * are met:
7 * 1. Redistributions of source code must retain the above copyright 7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer. 8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright 9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the 10 * notice, this list of conditions and the following disclaimer in the
(...skipping 13 matching lines...) Expand all
24 */ 24 */
25 25
26 #include "config.h" 26 #include "config.h"
27 #include "platform/Language.h" 27 #include "platform/Language.h"
28 28
29 #include "public/platform/Platform.h" 29 #include "public/platform/Platform.h"
30 #include "wtf/text/WTFString.h" 30 #include "wtf/text/WTFString.h"
31 31
32 namespace blink { 32 namespace blink {
33 33
34 static String canonicalizeLanguageIdentifier(const String& languageCode)
35 {
36 String lowercaseLanguageCode = languageCode.lower();
37 // Platform::defaultLocale() might provide a language code with '_'.
38 lowercaseLanguageCode.replace('_', '-');
39 return lowercaseLanguageCode;
40 }
41
34 static const AtomicString& platformLanguage() 42 static const AtomicString& platformLanguage()
35 { 43 {
36 DEFINE_STATIC_LOCAL(AtomicString, computedDefaultLanguage, ()); 44 DEFINE_STATIC_LOCAL(AtomicString, computedDefaultLanguage, ());
37 if (computedDefaultLanguage.isEmpty()) { 45 if (computedDefaultLanguage.isEmpty()) {
38 computedDefaultLanguage = blink::Platform::current()->defaultLocale(); 46 computedDefaultLanguage = AtomicString(canonicalizeLanguageIdentifier(Pl atform::current()->defaultLocale()));
39 ASSERT(!computedDefaultLanguage.isEmpty()); 47 ASSERT(!computedDefaultLanguage.isEmpty());
40 } 48 }
41 return computedDefaultLanguage; 49 return computedDefaultLanguage;
42 } 50 }
43 51
44 AtomicString defaultLanguage()
45 {
46 Vector<AtomicString> languages = userPreferredLanguages();
47 if (!languages.isEmpty())
48 return languages[0];
49
50 return emptyAtom;
51 }
52
53 static Vector<AtomicString>& preferredLanguagesOverride() 52 static Vector<AtomicString>& preferredLanguagesOverride()
54 { 53 {
55 DEFINE_STATIC_LOCAL(Vector<AtomicString>, override, ()); 54 DEFINE_STATIC_LOCAL(Vector<AtomicString>, override, ());
56 return override; 55 return override;
57 } 56 }
58 57
59 Vector<AtomicString> userPreferredLanguagesOverride() 58 void overrideUserPreferredLanguages(const Vector<AtomicString>& override)
60 { 59 {
Kunihiko Sakamoto 2014/11/06 03:56:02 I don't understand why this implementation is bett
tkent 2014/11/06 04:02:21 This is not better than the original code. We nee
Kunihiko Sakamoto 2014/11/06 04:03:47 Oh, I missed that part. Got it, thanks!
61 return preferredLanguagesOverride(); 60 Vector<AtomicString>& canonicalized = preferredLanguagesOverride();
61 canonicalized.resize(0);
62 canonicalized.reserveCapacity(override.size());
63 for (const auto& lang : override)
64 canonicalized.append(canonicalizeLanguageIdentifier(lang));
62 } 65 }
63 66
64 void overrideUserPreferredLanguages(const Vector<AtomicString>& override) 67 AtomicString defaultLanguage()
65 { 68 {
66 preferredLanguagesOverride() = override; 69 Vector<AtomicString>& override = preferredLanguagesOverride();
70 if (!override.isEmpty())
71 return override[0];
72 return platformLanguage();
67 } 73 }
68 74
69 Vector<AtomicString> userPreferredLanguages() 75 Vector<AtomicString> userPreferredLanguages()
70 { 76 {
71 Vector<AtomicString>& override = preferredLanguagesOverride(); 77 Vector<AtomicString>& override = preferredLanguagesOverride();
72 if (!override.isEmpty()) 78 if (!override.isEmpty())
73 return override; 79 return override;
74 80
75 Vector<AtomicString> languages; 81 Vector<AtomicString> languages;
76 languages.reserveInitialCapacity(1); 82 languages.reserveInitialCapacity(1);
77 languages.append(platformLanguage()); 83 languages.append(platformLanguage());
78 return languages; 84 return languages;
79 } 85 }
80 86
81 static String canonicalLanguageIdentifier(const String& languageCode)
82 {
83 String lowercaseLanguageCode = languageCode.lower();
84
85 if (lowercaseLanguageCode.length() >= 3 && lowercaseLanguageCode[2] == '_')
86 lowercaseLanguageCode.replace(2, 1, "-");
87
88 return lowercaseLanguageCode;
89 }
90
91 size_t indexOfBestMatchingLanguageInList(const AtomicString& language, const Vec tor<AtomicString>& languageList) 87 size_t indexOfBestMatchingLanguageInList(const AtomicString& language, const Vec tor<AtomicString>& languageList)
92 { 88 {
93 AtomicString languageWithoutLocaleMatch; 89 AtomicString languageWithoutLocaleMatch;
94 AtomicString languageMatchButNotLocale; 90 AtomicString languageMatchButNotLocale;
95 size_t languageWithoutLocaleMatchIndex = 0; 91 size_t languageWithoutLocaleMatchIndex = 0;
96 size_t languageMatchButNotLocaleMatchIndex = 0; 92 size_t languageMatchButNotLocaleMatchIndex = 0;
97 bool canMatchLanguageOnly = (language.length() == 2 || (language.length() >= 3 && language[2] == '-')); 93 bool canMatchLanguageOnly = (language.length() == 2 || (language.length() >= 3 && language[2] == '-'));
98 94
99 for (size_t i = 0; i < languageList.size(); ++i) { 95 for (size_t i = 0; i < languageList.size(); ++i) {
100 String canonicalizedLanguageFromList = canonicalLanguageIdentifier(langu ageList[i]); 96 String canonicalizedLanguageFromList = canonicalizeLanguageIdentifier(la nguageList[i]);
101 97
102 if (language == canonicalizedLanguageFromList) 98 if (language == canonicalizedLanguageFromList)
103 return i; 99 return i;
104 100
105 if (canMatchLanguageOnly && canonicalizedLanguageFromList.length() >= 2) { 101 if (canMatchLanguageOnly && canonicalizedLanguageFromList.length() >= 2) {
106 if (language[0] == canonicalizedLanguageFromList[0] && language[1] = = canonicalizedLanguageFromList[1]) { 102 if (language[0] == canonicalizedLanguageFromList[0] && language[1] = = canonicalizedLanguageFromList[1]) {
107 if (!languageWithoutLocaleMatch.length() && canonicalizedLanguag eFromList.length() == 2) { 103 if (!languageWithoutLocaleMatch.length() && canonicalizedLanguag eFromList.length() == 2) {
108 languageWithoutLocaleMatch = languageList[i]; 104 languageWithoutLocaleMatch = languageList[i];
109 languageWithoutLocaleMatchIndex = i; 105 languageWithoutLocaleMatchIndex = i;
110 } 106 }
(...skipping 11 matching lines...) Expand all
122 if (languageWithoutLocaleMatch.length()) 118 if (languageWithoutLocaleMatch.length())
123 return languageWithoutLocaleMatchIndex; 119 return languageWithoutLocaleMatchIndex;
124 120
125 if (languageMatchButNotLocale.length()) 121 if (languageMatchButNotLocale.length())
126 return languageMatchButNotLocaleMatchIndex; 122 return languageMatchButNotLocaleMatchIndex;
127 123
128 return languageList.size(); 124 return languageList.size();
129 } 125 }
130 126
131 } 127 }
OLDNEW
« no previous file with comments | « Source/platform/Language.h ('k') | Source/web/resources/calendarPicker.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698