API  0.9.10
CPLocale.j
Go to the documentation of this file.
1 /* CPLocale.j
2 * Foundation
3 *
4 * Created by Alexandre Wilhelm
5 * Copyright 2012 <[email protected]>
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21 
22 
23 
24 CPLocaleIdentifier = @"CPLocaleIdentifier";
25 CPLocaleLanguageCode = @"CPLocaleLanguageCode";
26 CPLocaleCountryCode = @"CPLocaleCountryCode";
27 CPLocaleScriptCode = @"CPLocaleScriptCode";
28 CPLocaleVariantCode = @"CPLocaleVariantCode";
29 CPLocaleExemplarCharacterSet = @"CPLocaleExemplarCharacterSet";
30 CPLocaleCalendar = @"CPLocaleCalendar";
31 CPLocaleCollationIdentifier = @"CPLocaleCollationIdentifier";
32 CPLocaleUsesMetricSystem = @"CPLocaleUsesMetricSystem";
33 CPLocaleMeasurementSystem = @"CPLocaleMeasurementSystem";
34 CPLocaleDecimalSeparator = @"CPLocaleDecimalSeparator";
35 CPLocaleGroupingSeparator = @"CPLocaleGroupingSeparator";
36 CPLocaleCurrencySymbol = @"CPLocaleCurrencySymbol";
37 CPLocaleCurrencyCode = @"CPLocaleCurrencyCode";
38 CPLocaleCollatorIdentifier = @"CPLocaleCollatorIdentifier";
39 CPLocaleQuotationBeginDelimiterKey = @"CPLocaleQuotationBeginDelimiterKey";
40 CPLocaleQuotationEndDelimiterKey = @"CPLocaleQuotationEndDelimiterKey";
41 CPLocaleAlternateQuotationBeginDelimiterKey = @"CPLocaleAlternateQuotationBeginDelimiterKey";
42 CPLocaleAlternateQuotationEndDelimiterKey = @"CPLocaleAlternateQuotationEndDelimiterKey";
43 
44 CPGregorianCalendar = @"CPGregorianCalendar";
45 CPBuddhistCalendar = @"CPBuddhistCalendar";
46 CPChineseCalendar = @"CPChineseCalendar";
47 CPHebrewCalendar = @"CPHebrewCalendar";
48 CPIslamicCalendar = @"CPIslamicCalendar";
49 CPIslamicCivilCalendar = @"CPIslamicCivilCalendar";
50 CPJapaneseCalendar = @"CPJapaneseCalendar";
51 CPRepublicOfChinaCalendar = @"CPRepublicOfChinaCalendar";
52 CPPersianCalendar = @"CPPersianCalendar";
53 CPIndianCalendar = @"CPIndianCalendar";
54 CPISO8601Calendar = @"CPISO8601Calendar";
55 
56 CPLocaleLanguageDirectionUnknown = @"CPLocaleLanguageDirectionUnknown";
57 CPLocaleLanguageDirectionLeftToRight = @"CPLocaleLanguageDirectionLeftToRight";
58 CPLocaleLanguageDirectionRightToLeft = @"CPLocaleLanguageDirectionRightToLeft";
59 CPLocaleLanguageDirectionTopToBottom = @"CPLocaleLanguageDirectionTopToBottom";
60 CPLocaleLanguageDirectionBottomToTop = @"CPLocaleLanguageDirectionBottomToTop";
61 
62 var countryCodes = [@"DE", @"FR", @"ES", @"GB", @"US", @"SE"],
63  languageCodes = [@"en", @"de", @"es", @"fr", @"sv"],
64  availableLocaleIdentifiers = [@"de_DE", @"en_GB", @"en_US", @"es_ES", @"fr_FR", @"sv_SE"];
65 
68 
69 @implementation CPLocale : CPObject
70 {
71  CPDictionary _locale;
72 }
73 
77 {
78  if (!sharedSystemLocale)
80 
81  return sharedSystemLocale;
82 }
83 
87 {
89  {
90  var localeIdentifier = @"en_US",
91  language;
92 
93  if (typeof navigator !== "undefined")
94  {
95  // userLanguage is an IE only property.
96  language = (typeof navigator.language !== "undefined") ? navigator.language : navigator.userLanguage;
97 
98  if (language)
99  {
100  // Browsers use locale strings such as "en-US", but CPLocale uses "en_US".
101  language = language.replace("-", "_").substring(0, 5);
102  // Some browsers have "en_us" at this point, while we want "en_US".
103  language = language.substring(0, 3).toLowerCase() + language.substring(3, 5).toUpperCase();
104 
105  if ([availableLocaleIdentifiers indexOfObject:language] !== CPNotFound)
106  localeIdentifier = language;
107  }
108  }
109 
111  }
112 
113  return sharedCurrentLocale;
114 }
115 
119 {
121 }
122 
125 + (CPArray)ISOCountryCodes
126 {
127  return countryCodes;
128 }
129 
130 // + (CPArray)ISOCurrencyCodes
131 // {
132 // // TODO
133 // return;
134 // }
135 
139 {
140  return languageCodes;
141 }
142 
143 // + (CPArray)commonISOCurrencyCodes
144 // {
145 // // TODO
146 // return;
147 // }
148 
153 - (id)initWithLocaleIdentifier:(CPString)anIdentifier
154 {
155  if (self == [super init])
156  {
157  var parts = [anIdentifier componentsSeparatedByString:@"_"],
158  language = [parts objectAtIndex:0],
159  country = nil;
160 
161  if ([parts count] > 1)
162  country = [parts objectAtIndex:1];
163  else
164  country = anIdentifier;
165 
166  _locale = [[CPDictionary alloc] init];
167  [_locale setObject:anIdentifier forKey:CPLocaleIdentifier];
168  [_locale setObject:language forKey:CPLocaleLanguageCode];
169  [_locale setObject:country forKey:CPLocaleCountryCode];
170 
171  if ([[self class] respondsToSelector:@selector(_platformLocaleAdditionalDescriptionForIdentifier:)])
172  {
173  // Use any platform specific method to fill the locale info if one is defined
174  var info = [[self class] performSelector:@selector(_platformLocaleAdditionalDescriptionForIdentifier:) withObject:anIdentifier];
175  [_locale addEntriesFromDictionary:info];
176  }
177  else
178  {
179  [_locale setObject:CPGregorianCalendar forKey:CPLocaleCalendar];
180  }
181  }
182 
183  return self;
184 }
185 
186 // - (CPString)displayNameForKey:(id)aKey value:(id)aValue
187 // {
188 // // TODO
189 // return;
190 // }
191 
195 {
196  return [_locale objectForKey:CPLocaleIdentifier];
197 }
198 
203 - (id)objectForKey:(id)aKey
204 {
205  return [_locale objectForKey:aKey];
206 }
207 
208 @end
209 
210 
211 var CPLocaleIdentifierLocaleKey = @"CPLocaleIdentifierLocaleKey";
212 
213 @implementation CPLocale (CPCoding)
214 
215 - (id)initWithCoder:(CPCoder)aCoder
216 {
217  if (self)
218  {
219  _locale = [aCoder decodeObjectForKey:CPLocaleIdentifierLocaleKey];
220  }
221 
222  return self
223 }
224 
225 - (void)encodeWithCoder:(CPCoder)aCoder
226 {
227  //[super encodeWithCoder:aCoder];
228 
229  [aCoder encodeObject:_locale forKey:CPLocaleIdentifierLocaleKey];
230 }
231 
232 @end
CPPersianCalendar
Definition: CPLocale.j:52
var CPLocaleIdentifierLocaleKey
Definition: CPLocale.j:211
CPLocaleLanguageDirectionUnknown
Definition: CPLocale.j:56
CPGregorianCalendar
Definition: CPLocale.j:44
CPArray componentsSeparatedByString:(CPString aString)
Definition: CPString.j:271
id currentLocale()
Definition: CPLocale.j:86
CPLocaleLanguageDirectionLeftToRight
Definition: CPLocale.j:57
id systemLocale()
Definition: CPLocale.j:76
CPLocaleScriptCode
Definition: CPLocale.j:27
CPLocaleCountryCode
Definition: CPLocale.j:26
CPLocaleExemplarCharacterSet
Definition: CPLocale.j:29
CPRepublicOfChinaCalendar
Definition: CPLocale.j:51
CPIslamicCalendar
Definition: CPLocale.j:48
id initWithLocaleIdentifier:(CPString anIdentifier)
Definition: CPLocale.j:153
CPLocaleCollationIdentifier
Definition: CPLocale.j:31
CPBuddhistCalendar
Definition: CPLocale.j:45
CPLocaleMeasurementSystem
Definition: CPLocale.j:33
var availableLocaleIdentifiers
Definition: CPLocale.j:64
var sharedSystemLocale
Definition: CPLocale.j:66
CPISO8601Calendar
Definition: CPLocale.j:54
CPArray availableLocaleIdentifiers()
Definition: CPLocale.j:118
A mutable key-value pair collection.
Definition: CPDictionary.h:2
CPLocaleLanguageCode
Definition: CPLocale.j:25
CPIndianCalendar
Definition: CPLocale.j:53
CPLocaleVariantCode
Definition: CPLocale.j:28
CPLocaleDecimalSeparator
Definition: CPLocale.j:34
CPLocaleAlternateQuotationBeginDelimiterKey
Definition: CPLocale.j:41
An immutable string (collection of characters).
Definition: CPString.h:2
CPJapaneseCalendar
Definition: CPLocale.j:50
if(CPFeatureIsCompatible(CPHTMLCanvasFeature))
CPLocaleIdentifier
Definition: CPLocale.j:24
CPLocaleLanguageDirectionBottomToTop
Definition: CPLocale.j:60
CPLocaleUsesMetricSystem
Definition: CPLocale.j:32
CPLocaleLanguageDirectionRightToLeft
Definition: CPLocale.j:58
CPLocaleCollatorIdentifier
Definition: CPLocale.j:38
var sharedCurrentLocale
Definition: CPLocale.j:67
CPLocaleQuotationEndDelimiterKey
Definition: CPLocale.j:40
Defines methods for use when archiving & restoring (enc/decoding).
Definition: CPCoder.h:2
CPNotFound
Definition: CPObjJRuntime.j:62
id init()
Definition: CPObject.j:145
CPIslamicCivilCalendar
Definition: CPLocale.j:49
CPLocaleQuotationBeginDelimiterKey
Definition: CPLocale.j:39
var languageCodes
Definition: CPLocale.j:63
CPLocaleCurrencyCode
Definition: CPLocale.j:37
CPChineseCalendar
Definition: CPLocale.j:46
CPLocaleAlternateQuotationEndDelimiterKey
Definition: CPLocale.j:42
CPArray ISOLanguageCodes()
Definition: CPLocale.j:138
var countryCodes
Definition: CPLocale.j:62
Class class()
Definition: CPObject.j:179
CPHebrewCalendar
Definition: CPLocale.j:47
CPLocaleCurrencySymbol
Definition: CPLocale.j:36
CPLocaleGroupingSeparator
Definition: CPLocale.j:35
CPString localeIdentifier()
Definition: CPLocale.j:194
CPArray ISOCountryCodes()
Definition: CPLocale.j:125
CPLocaleCalendar
Definition: CPLocale.j:30
id alloc()
Definition: CPObject.j:130
CPLocaleLanguageDirectionTopToBottom
Definition: CPLocale.j:59