OLD | NEW |
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 Loading... |
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 WebCore { | 32 namespace WebCore { |
33 | 33 |
34 static String platformLanguage() | 34 static const AtomicString& platformLanguage() |
35 { | 35 { |
36 DEFINE_STATIC_LOCAL(String, computedDefaultLanguage, ()); | 36 DEFINE_STATIC_LOCAL(AtomicString, computedDefaultLanguage, ()); |
37 if (computedDefaultLanguage.isEmpty()) { | 37 if (computedDefaultLanguage.isEmpty()) { |
38 computedDefaultLanguage.append(blink::Platform::current()->defaultLocale
()); | 38 computedDefaultLanguage = blink::Platform::current()->defaultLocale(); |
39 ASSERT(!computedDefaultLanguage.isEmpty()); | 39 ASSERT(!computedDefaultLanguage.isEmpty()); |
40 } | 40 } |
41 return computedDefaultLanguage; | 41 return computedDefaultLanguage; |
42 } | 42 } |
43 | 43 |
44 String defaultLanguage() | 44 AtomicString defaultLanguage() |
45 { | 45 { |
46 Vector<String> languages = userPreferredLanguages(); | 46 Vector<AtomicString> languages = userPreferredLanguages(); |
47 if (languages.size()) | 47 if (!languages.isEmpty()) |
48 return languages[0]; | 48 return languages[0]; |
49 | 49 |
50 return emptyString(); | 50 return emptyAtom; |
51 } | 51 } |
52 | 52 |
53 static Vector<String>& preferredLanguagesOverride() | 53 static Vector<AtomicString>& preferredLanguagesOverride() |
54 { | 54 { |
55 DEFINE_STATIC_LOCAL(Vector<String>, override, ()); | 55 DEFINE_STATIC_LOCAL(Vector<AtomicString>, override, ()); |
56 return override; | 56 return override; |
57 } | 57 } |
58 | 58 |
59 Vector<String> userPreferredLanguagesOverride() | 59 Vector<AtomicString> userPreferredLanguagesOverride() |
60 { | 60 { |
61 return preferredLanguagesOverride(); | 61 return preferredLanguagesOverride(); |
62 } | 62 } |
63 | 63 |
64 void overrideUserPreferredLanguages(const Vector<String>& override) | 64 void overrideUserPreferredLanguages(const Vector<AtomicString>& override) |
65 { | 65 { |
66 preferredLanguagesOverride() = override; | 66 preferredLanguagesOverride() = override; |
67 } | 67 } |
68 | 68 |
69 Vector<String> userPreferredLanguages() | 69 Vector<AtomicString> userPreferredLanguages() |
70 { | 70 { |
71 Vector<String>& override = preferredLanguagesOverride(); | 71 Vector<AtomicString>& override = preferredLanguagesOverride(); |
72 if (!override.isEmpty()) | 72 if (!override.isEmpty()) |
73 return override; | 73 return override; |
74 | 74 |
75 Vector<String> languages; | 75 Vector<AtomicString> languages; |
76 languages.reserveInitialCapacity(1); | 76 languages.reserveInitialCapacity(1); |
77 languages.append(platformLanguage()); | 77 languages.append(platformLanguage()); |
78 return languages; | 78 return languages; |
79 } | 79 } |
80 | 80 |
81 static String canonicalLanguageIdentifier(const String& languageCode) | 81 static String canonicalLanguageIdentifier(const String& languageCode) |
82 { | 82 { |
83 String lowercaseLanguageCode = languageCode.lower(); | 83 String lowercaseLanguageCode = languageCode.lower(); |
84 | 84 |
85 if (lowercaseLanguageCode.length() >= 3 && lowercaseLanguageCode[2] == '_') | 85 if (lowercaseLanguageCode.length() >= 3 && lowercaseLanguageCode[2] == '_') |
86 lowercaseLanguageCode.replace(2, 1, "-"); | 86 lowercaseLanguageCode.replace(2, 1, "-"); |
87 | 87 |
88 return lowercaseLanguageCode; | 88 return lowercaseLanguageCode; |
89 } | 89 } |
90 | 90 |
91 size_t indexOfBestMatchingLanguageInList(const String& language, const Vector<St
ring>& languageList) | 91 size_t indexOfBestMatchingLanguageInList(const AtomicString& language, const Vec
tor<AtomicString>& languageList) |
92 { | 92 { |
93 String languageWithoutLocaleMatch; | 93 AtomicString languageWithoutLocaleMatch; |
94 String languageMatchButNotLocale; | 94 AtomicString languageMatchButNotLocale; |
95 size_t languageWithoutLocaleMatchIndex = 0; | 95 size_t languageWithoutLocaleMatchIndex = 0; |
96 size_t languageMatchButNotLocaleMatchIndex = 0; | 96 size_t languageMatchButNotLocaleMatchIndex = 0; |
97 bool canMatchLanguageOnly = (language.length() == 2 || (language.length() >=
3 && language[2] == '-')); | 97 bool canMatchLanguageOnly = (language.length() == 2 || (language.length() >=
3 && language[2] == '-')); |
98 | 98 |
99 for (size_t i = 0; i < languageList.size(); ++i) { | 99 for (size_t i = 0; i < languageList.size(); ++i) { |
100 String canonicalizedLanguageFromList = canonicalLanguageIdentifier(langu
ageList[i]); | 100 String canonicalizedLanguageFromList = canonicalLanguageIdentifier(langu
ageList[i]); |
101 | 101 |
102 if (language == canonicalizedLanguageFromList) | 102 if (language == canonicalizedLanguageFromList) |
103 return i; | 103 return i; |
104 | 104 |
(...skipping 17 matching lines...) Expand all Loading... |
122 if (languageWithoutLocaleMatch.length()) | 122 if (languageWithoutLocaleMatch.length()) |
123 return languageWithoutLocaleMatchIndex; | 123 return languageWithoutLocaleMatchIndex; |
124 | 124 |
125 if (languageMatchButNotLocale.length()) | 125 if (languageMatchButNotLocale.length()) |
126 return languageMatchButNotLocaleMatchIndex; | 126 return languageMatchButNotLocaleMatchIndex; |
127 | 127 |
128 return languageList.size(); | 128 return languageList.size(); |
129 } | 129 } |
130 | 130 |
131 } | 131 } |
OLD | NEW |