OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2012 Google Inc. All rights reserved. | 2 * Copyright (C) 2012 Google 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 are | 5 * modification, are permitted provided that the following conditions are |
6 * met: | 6 * met: |
7 * | 7 * |
8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
(...skipping 28 matching lines...) Expand all Loading... |
39 #include "wtf/DateMath.h" | 39 #include "wtf/DateMath.h" |
40 #include "wtf/HashMap.h" | 40 #include "wtf/HashMap.h" |
41 #include "wtf/OwnPtr.h" | 41 #include "wtf/OwnPtr.h" |
42 #include "wtf/PassOwnPtr.h" | 42 #include "wtf/PassOwnPtr.h" |
43 #include "wtf/text/StringBuffer.h" | 43 #include "wtf/text/StringBuffer.h" |
44 #include "wtf/text/StringBuilder.h" | 44 #include "wtf/text/StringBuilder.h" |
45 #include "wtf/text/StringHash.h" | 45 #include "wtf/text/StringHash.h" |
46 | 46 |
47 namespace blink { | 47 namespace blink { |
48 | 48 |
49 typedef LCID (WINAPI* LocaleNameToLCIDPtr)(LPCWSTR, DWORD); | |
50 typedef HashMap<String, LCID> NameToLCIDMap; | |
51 | |
52 static String extractLanguageCode(const String& locale) | 49 static String extractLanguageCode(const String& locale) |
53 { | 50 { |
54 size_t dashPosition = locale.find('-'); | 51 size_t dashPosition = locale.find('-'); |
55 if (dashPosition == kNotFound) | 52 if (dashPosition == kNotFound) |
56 return locale; | 53 return locale; |
57 return locale.left(dashPosition); | 54 return locale.left(dashPosition); |
58 } | 55 } |
59 | 56 |
60 static String removeLastComponent(const String& name) | 57 static LCID LCIDFromLocaleInternal(LCID userDefaultLCID, const String& userDefau
ltLanguageCode, const String& locale) |
61 { | |
62 size_t lastSeparator = name.reverseFind('-'); | |
63 if (lastSeparator == kNotFound) | |
64 return emptyString(); | |
65 return name.left(lastSeparator); | |
66 } | |
67 | |
68 static void ensureNameToLCIDMap(NameToLCIDMap& map) | |
69 { | |
70 if (!map.isEmpty()) | |
71 return; | |
72 // http://www.microsoft.com/resources/msdn/goglobal/default.mspx | |
73 // We add only locales used in layout tests for now. | |
74 map.add("ar", 0x0001); | |
75 map.add("ar-eg", 0x0C01); | |
76 map.add("de", 0x0007); | |
77 map.add("de-de", 0x0407); | |
78 map.add("el", 0x0008); | |
79 map.add("el-gr", 0x0408); | |
80 map.add("en", 0x0009); | |
81 map.add("en-gb", 0x0809); | |
82 map.add("en-us", 0x0409); | |
83 map.add("fr", 0x000C); | |
84 map.add("fr-fr", 0x040C); | |
85 map.add("he", 0x000D); | |
86 map.add("he-il", 0x040D); | |
87 map.add("hi", 0x0039); | |
88 map.add("hi-in", 0x0439); | |
89 map.add("ja", 0x0011); | |
90 map.add("ja-jp", 0x0411); | |
91 map.add("ko", 0x0012); | |
92 map.add("ko-kr", 0x0412); | |
93 map.add("ru", 0x0019); | |
94 map.add("ru-ru", 0x0419); | |
95 map.add("zh-cn", 0x0804); | |
96 map.add("zh-tw", 0x0404); | |
97 } | |
98 | |
99 // Fallback implementation of LocaleNameToLCID API. This is used for | |
100 // testing on Windows XP. | |
101 // FIXME: Remove this, ensureNameToLCIDMap, and removeLastComponent when we drop | |
102 // Windows XP support. | |
103 static LCID WINAPI convertLocaleNameToLCID(LPCWSTR name, DWORD) | |
104 { | |
105 if (!name || !name[0]) | |
106 return LOCALE_USER_DEFAULT; | |
107 DEFINE_STATIC_LOCAL(NameToLCIDMap, map, ()); | |
108 ensureNameToLCIDMap(map); | |
109 String localeName = String(name).replace('_', '-'); | |
110 localeName = localeName.lower(); | |
111 do { | |
112 NameToLCIDMap::const_iterator iterator = map.find(localeName); | |
113 if (iterator != map.end()) | |
114 return iterator->value; | |
115 localeName = removeLastComponent(localeName); | |
116 } while (!localeName.isEmpty()); | |
117 return LOCALE_USER_DEFAULT; | |
118 } | |
119 | |
120 static LCID LCIDFromLocaleInternal(LCID userDefaultLCID, const String& userDefau
ltLanguageCode, LocaleNameToLCIDPtr localeNameToLCID, const String& locale) | |
121 { | 58 { |
122 String localeLanguageCode = extractLanguageCode(locale); | 59 String localeLanguageCode = extractLanguageCode(locale); |
123 if (equalIgnoringCase(localeLanguageCode, userDefaultLanguageCode)) | 60 if (equalIgnoringCase(localeLanguageCode, userDefaultLanguageCode)) |
124 return userDefaultLCID; | 61 return userDefaultLCID; |
125 return localeNameToLCID(locale.charactersWithNullTermination().data(), 0); | 62 return ::LocaleNameToLCID(locale.charactersWithNullTermination().data(), 0); |
126 } | 63 } |
127 | 64 |
128 static LCID LCIDFromLocale(const String& locale, bool defaultsForLocale) | 65 static LCID LCIDFromLocale(const String& locale, bool defaultsForLocale) |
129 { | 66 { |
130 // LocaleNameToLCID() is available since Windows Vista. | |
131 LocaleNameToLCIDPtr localeNameToLCID = reinterpret_cast<LocaleNameToLCIDPtr>
(::GetProcAddress(::GetModuleHandle(L"kernel32"), "LocaleNameToLCID")); | |
132 if (!localeNameToLCID) | |
133 localeNameToLCID = convertLocaleNameToLCID; | |
134 | |
135 // According to MSDN, 9 is enough for LOCALE_SISO639LANGNAME. | 67 // According to MSDN, 9 is enough for LOCALE_SISO639LANGNAME. |
136 const size_t languageCodeBufferSize = 9; | 68 const size_t languageCodeBufferSize = 9; |
137 WCHAR lowercaseLanguageCode[languageCodeBufferSize]; | 69 WCHAR lowercaseLanguageCode[languageCodeBufferSize]; |
138 ::GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SISO639LANGNAME | (defaultsForLo
cale ? LOCALE_NOUSEROVERRIDE : 0), lowercaseLanguageCode, languageCodeBufferSize
); | 70 ::GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SISO639LANGNAME | (defaultsForLo
cale ? LOCALE_NOUSEROVERRIDE : 0), lowercaseLanguageCode, languageCodeBufferSize
); |
139 String userDefaultLanguageCode = String(lowercaseLanguageCode); | 71 String userDefaultLanguageCode = String(lowercaseLanguageCode); |
140 | 72 |
141 LCID lcid = LCIDFromLocaleInternal(LOCALE_USER_DEFAULT, userDefaultLanguageC
ode, localeNameToLCID, locale); | 73 LCID lcid = LCIDFromLocaleInternal(LOCALE_USER_DEFAULT, userDefaultLanguageC
ode, locale); |
142 if (!lcid) | 74 if (!lcid) |
143 lcid = LCIDFromLocaleInternal(LOCALE_USER_DEFAULT, userDefaultLanguageCo
de, localeNameToLCID, defaultLanguage()); | 75 lcid = LCIDFromLocaleInternal(LOCALE_USER_DEFAULT, userDefaultLanguageCo
de, defaultLanguage()); |
144 return lcid; | 76 return lcid; |
145 } | 77 } |
146 | 78 |
147 PassOwnPtr<Locale> Locale::create(const String& locale) | 79 PassOwnPtr<Locale> Locale::create(const String& locale) |
148 { | 80 { |
149 // Whether the default settings for the locale should be used, ignoring user
overrides. | 81 // Whether the default settings for the locale should be used, ignoring user
overrides. |
150 bool defaultsForLocale = LayoutTestSupport::isRunningLayoutTest(); | 82 bool defaultsForLocale = LayoutTestSupport::isRunningLayoutTest(); |
151 return LocaleWin::create(LCIDFromLocale(locale, defaultsForLocale), defaults
ForLocale); | 83 return LocaleWin::create(LCIDFromLocale(locale, defaultsForLocale), defaults
ForLocale); |
152 } | 84 } |
153 | 85 |
(...skipping 420 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
574 case NegativeFormatSignPrefix: // Fall through. | 506 case NegativeFormatSignPrefix: // Fall through. |
575 default: | 507 default: |
576 negativePrefix = negativeSign; | 508 negativePrefix = negativeSign; |
577 break; | 509 break; |
578 } | 510 } |
579 m_didInitializeNumberData = true; | 511 m_didInitializeNumberData = true; |
580 setLocaleData(symbols, emptyString(), emptyString(), negativePrefix, negativ
eSuffix); | 512 setLocaleData(symbols, emptyString(), emptyString(), negativePrefix, negativ
eSuffix); |
581 } | 513 } |
582 | 514 |
583 } | 515 } |
OLD | NEW |