API  0.9.7
 All Classes Files Functions Variables Macros Groups Pages
CPLocale.j
Go to the documentation of this file.
1 /* CPLocale.j
2 * Foundation
3 *
4 * Created by Alexandre Wilhelm
5 * Copyright 2012 <alexandre.wilhelmfr@gmail.com>
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 @class CPDictionary
24 
25 CPLocaleIdentifier = @"CPLocaleIdentifier";
26 CPLocaleLanguageCode = @"CPLocaleLanguageCode";
27 CPLocaleCountryCode = @"CPLocaleCountryCode";
28 CPLocaleScriptCode = @"CPLocaleScriptCode";
29 CPLocaleVariantCode = @"CPLocaleVariantCode";
30 CPLocaleExemplarCharacterSet = @"CPLocaleExemplarCharacterSet";
31 CPLocaleCalendar = @"CPLocaleCalendar";
32 CPLocaleCollationIdentifier = @"CPLocaleCollationIdentifier";
33 CPLocaleUsesMetricSystem = @"CPLocaleUsesMetricSystem";
34 CPLocaleMeasurementSystem = @"CPLocaleMeasurementSystem";
35 CPLocaleDecimalSeparator = @"CPLocaleDecimalSeparator";
36 CPLocaleGroupingSeparator = @"CPLocaleGroupingSeparator";
37 CPLocaleCurrencySymbol = @"CPLocaleCurrencySymbol";
38 CPLocaleCurrencyCode = @"CPLocaleCurrencyCode";
39 CPLocaleCollatorIdentifier = @"CPLocaleCollatorIdentifier";
40 CPLocaleQuotationBeginDelimiterKey = @"CPLocaleQuotationBeginDelimiterKey";
41 CPLocaleQuotationEndDelimiterKey = @"CPLocaleQuotationEndDelimiterKey";
42 CPLocaleAlternateQuotationBeginDelimiterKey = @"CPLocaleAlternateQuotationBeginDelimiterKey";
43 CPLocaleAlternateQuotationEndDelimiterKey = @"CPLocaleAlternateQuotationEndDelimiterKey";
44 
45 CPGregorianCalendar = @"CPGregorianCalendar";
46 CPBuddhistCalendar = @"CPBuddhistCalendar";
47 CPChineseCalendar = @"CPChineseCalendar";
48 CPHebrewCalendar = @"CPHebrewCalendar";
49 CPIslamicCalendar = @"CPIslamicCalendar";
50 CPIslamicCivilCalendar = @"CPIslamicCivilCalendar";
51 CPJapaneseCalendar = @"CPJapaneseCalendar";
52 CPRepublicOfChinaCalendar = @"CPRepublicOfChinaCalendar";
53 CPPersianCalendar = @"CPPersianCalendar";
54 CPIndianCalendar = @"CPIndianCalendar";
55 CPISO8601Calendar = @"CPISO8601Calendar";
56 
57 CPLocaleLanguageDirectionUnknown = @"CPLocaleLanguageDirectionUnknown";
58 CPLocaleLanguageDirectionLeftToRight = @"CPLocaleLanguageDirectionLeftToRight";
59 CPLocaleLanguageDirectionRightToLeft = @"CPLocaleLanguageDirectionRightToLeft";
60 CPLocaleLanguageDirectionTopToBottom = @"CPLocaleLanguageDirectionTopToBottom";
61 CPLocaleLanguageDirectionBottomToTop = @"CPLocaleLanguageDirectionBottomToTop";
62 
63 var countryCodes = [@"DE", @"FR", @"ES", @"GB", @"US"],
64  languageCodes = [@"en", @"de", @"es", @"fr"],
65  availableLocaleIdentifiers = [@"de_DE", @"en_GB", @"en_US", @"es_ES", @"fr_FR"];
66 
67 var sharedSystemLocale = nil,
68  sharedCurrentLocale = nil;
69 
70 @implementation CPLocale : CPObject
71 {
72  CPDictionary _locale;
73 }
74 
77 + (id)systemLocale
78 {
79  if (!sharedSystemLocale)
80  sharedSystemLocale = [[CPLocale alloc] initWithLocaleIdentifier:@"en_US"];
81 
82  return sharedSystemLocale;
83 }
84 
87 + (id)currentLocale
88 {
89  if (!sharedCurrentLocale)
90  {
91  var localeIdentifier = @"en_US",
92  language;
93 
94  if (typeof navigator !== "undefined")
95  {
96  // userLanguage is an IE only property.
97  language = (typeof navigator.language !== "undefined") ? navigator.language : navigator.userLanguage;
98 
99  if (language)
100  {
101  // Browsers use locale strings such as "en-US", but CPLocale uses "en_US".
102  language = language.replace("-", "_").substring(0, 5);
103  // Some browsers have "en_us" at this point, while we want "en_US".
104  language = language.substring(0, 3).toLowerCase() + language.substring(3, 5).toUpperCase();
105 
106  if ([availableLocaleIdentifiers indexOfObject:language] !== CPNotFound)
107  localeIdentifier = language;
108  }
109  }
110 
111  sharedCurrentLocale = [[CPLocale alloc] initWithLocaleIdentifier:localeIdentifier];
112  }
113 
114  return sharedCurrentLocale;
115 }
116 
119 + (CPArray)availableLocaleIdentifiers
120 {
121  return availableLocaleIdentifiers;
122 }
123 
126 + (CPArray)ISOCountryCodes
127 {
128  return countryCodes;
129 }
130 
131 // + (CPArray)ISOCurrencyCodes
132 // {
133 // // TODO
134 // return;
135 // }
136 
139 + (CPArray)ISOLanguageCodes
140 {
141  return languageCodes;
142 }
143 
144 // + (CPArray)commonISOCurrencyCodes
145 // {
146 // // TODO
147 // return;
148 // }
149 
154 - (id)initWithLocaleIdentifier:(CPString)anIdentifier
155 {
156  if (self == [super init])
157  {
158  var parts = [anIdentifier componentsSeparatedByString:@"_"],
159  language = [parts objectAtIndex:0],
160  country = nil;
161 
162  if ([parts count] > 1)
163  country = [parts objectAtIndex:1];
164  else
165  country = anIdentifier;
166 
167  _locale = [[CPDictionary alloc] init];
168  [_locale setObject:anIdentifier forKey:CPLocaleIdentifier];
169  [_locale setObject:language forKey:CPLocaleLanguageCode];
170  [_locale setObject:country forKey:CPLocaleCountryCode];
171 
172  if ([[self class] respondsToSelector:@selector(_platformLocaleAdditionalDescriptionForIdentifier:)])
173  {
174  // Use any platform specific method to fill the locale info if one is defined
175  var info = [[self class] performSelector:@selector(_platformLocaleAdditionalDescriptionForIdentifier:) withObject:anIdentifier];
176  [_locale addEntriesFromDictionary:info];
177  }
178  else
179  {
180  [_locale setObject:CPGregorianCalendar forKey:CPLocaleCalendar];
181  }
182  }
183 
184  return self;
185 }
186 
187 // - (CPString)displayNameForKey:(id)aKey value:(id)aValue
188 // {
189 // // TODO
190 // return;
191 // }
192 
195 - (CPString)localeIdentifier
196 {
197  return [_locale objectForKey:CPLocaleIdentifier];
198 }
199 
204 - (id)objectForKey:(id)aKey
205 {
206  return [_locale objectForKey:aKey];
207 }
208 
209 @end
210 
211 
212 var CPLocaleIdentifierLocaleKey = @"CPLocaleIdentifierLocaleKey";
213 
214 @implementation CPLocale (CPCoding)
215 
216 - (id)initWithCoder:(CPCoder)aCoder
217 {
218  if (self)
219  {
220  _locale = [aCoder decodeObjectForKey:CPLocaleIdentifierLocaleKey];
221  }
222 
223  return self
224 }
225 
226 - (void)encodeWithCoder:(CPCoder)aCoder
227 {
228  //[super encodeWithCoder:aCoder];
229 
230  [aCoder encodeObject:_locale forKey:CPLocaleIdentifierLocaleKey];
231 }
232 
233 @end