1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426
|
// Copyright 2006 The Closure Library Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS-IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/**
* @fileoverview Functions for dealing with Date formatting & Parsing,
* County and language name, TimeZone list.
* @suppress {deprecated} Use goog.i18n instead.
*/
/**
* Namespace for locale related functions.
*/
goog.provide('goog.locale');
goog.require('goog.locale.nativeNameConstants');
/**
* Set current locale to the specified one.
* @param {string} localeName Locale name string. We are following the usage
* in CLDR, but can make a few compromise for existing name compatibility.
*/
goog.locale.setLocale = function(localeName) {
// it is common to see people use '-' as locale part separator, normalize it.
localeName = localeName.replace(/-/g, '_');
goog.locale.activeLocale_ = localeName;
};
/**
* Retrieve the current locale
* @return {string} Current locale name string.
* @deprecated Use goog.LOCALE and goog.i18n instead.
*/
goog.locale.getLocale = function() {
if (!goog.locale.activeLocale_) {
goog.locale.activeLocale_ = 'en';
}
return goog.locale.activeLocale_;
};
// Couple of constants to represent predefined Date/Time format type.
/**
* Enum of resources that can be registered.
* @enum {string}
*/
goog.locale.Resource = {
DATE_TIME_CONSTANTS: 'DateTimeConstants',
NUMBER_FORMAT_CONSTANTS: 'NumberFormatConstants',
TIME_ZONE_CONSTANTS: 'TimeZoneConstants',
LOCAL_NAME_CONSTANTS: 'LocaleNameConstants',
TIME_ZONE_SELECTED_IDS: 'TimeZoneSelectedIds',
TIME_ZONE_SELECTED_SHORT_NAMES: 'TimeZoneSelectedShortNames',
TIME_ZONE_SELECTED_LONG_NAMES: 'TimeZoneSelectedLongNames',
TIME_ZONE_ALL_LONG_NAMES: 'TimeZoneAllLongNames'
};
// BCP 47 language code:
//
// LanguageCode := LanguageSubtag
// ("-" ScriptSubtag)?
// ("-" RegionSubtag)?
// ("-" VariantSubtag)?
// ("@" Keyword "=" Value ("," Keyword "=" Value)* )?
//
// e.g. en-Latn-GB
//
// NOTICE:
// No special format checking is performed. If you pass an invalid
// language code as a parameter to the following functions,
// you might get an unexpected result.
/**
* Returns the language-subtag of the given language code.
*
* @param {string} languageCode Language code to extract language subtag from.
* @return {string} Language subtag (in lowercase).
*/
goog.locale.getLanguageSubTag = function(languageCode) {
var result = languageCode.match(/^\w{2,3}([-_]|$)/);
return result ? result[0].replace(/[_-]/g, '') : '';
};
/**
* Returns the region-sub-tag of the given language code.
*
* @param {string} languageCode Language code to extract region subtag from.
* @return {string} Region sub-tag (in uppercase).
*/
goog.locale.getRegionSubTag = function(languageCode) {
var result = languageCode.match(/[-_]([a-zA-Z]{2}|\d{3})([-_]|$)/);
return result ? result[0].replace(/[_-]/g, '') : '';
};
/**
* Returns the script subtag of the locale with the first alphabet in uppercase
* and the rest 3 characters in lower case.
*
* @param {string} languageCode Language Code to extract script subtag from.
* @return {string} Script subtag.
*/
goog.locale.getScriptSubTag = function(languageCode) {
var result = languageCode.split(/[-_]/g);
return result.length > 1 && result[1].match(/^[a-zA-Z]{4}$/) ? result[1] : '';
};
/**
* Returns the variant-sub-tag of the given language code.
*
* @param {string} languageCode Language code to extract variant subtag from.
* @return {string} Variant sub-tag.
*/
goog.locale.getVariantSubTag = function(languageCode) {
var result = languageCode.match(/[-_]([a-z]{2,})/);
return result ? result[1] : '';
};
/**
* Returns the country name of the provided language code in its native
* language.
*
* This method depends on goog.locale.nativeNameConstants available from
* nativenameconstants.js. User of this method has to add dependency to this.
*
* @param {string} countryCode Code to lookup the country name for.
*
* @return {string} Country name for the provided language code.
*/
goog.locale.getNativeCountryName = function(countryCode) {
var key = goog.locale.getLanguageSubTag(countryCode) + '_' +
goog.locale.getRegionSubTag(countryCode);
return key in goog.locale.nativeNameConstants['COUNTRY'] ?
goog.locale.nativeNameConstants['COUNTRY'][key] :
countryCode;
};
/**
* Returns the localized country name for the provided language code in the
* current or provided locale symbols set.
*
* This method depends on `goog.locale.LocaleNameConstants__<locale>` available
* from http://go/js_locale_data. User of this method has to add dependency to
* this.
*
* @param {string} languageCode Language code to lookup the country name for.
* @param {Object=} opt_localeSymbols If omitted the current locale symbol
* set is used.
*
* @return {string} Localized country name.
*/
goog.locale.getLocalizedCountryName = function(
languageCode, opt_localeSymbols) {
var code = goog.locale.getRegionSubTag(languageCode);
var name =
goog.locale.getLocalizedRegionNameFromRegionCode(code, opt_localeSymbols);
return name == code ? languageCode : name;
};
/**
* Returns the localized country name for the provided language code in the
* current or provided locale symbols set.
*
* This method depends on `goog.locale.LocaleNameConstants__<locale>` available
* from http://go/js_locale_data. User of this method has to add dependency to
* this.
*
* @param {string} regionCode Two character country code or three digit region
* code to look up the country name for.
* @param {?Object=} opt_localeSymbols If omitted the current locale symbol
* set is used.
*
* @return {string} Localized region name.
*/
goog.locale.getLocalizedRegionNameFromRegionCode = function(
regionCode, opt_localeSymbols) {
if (!opt_localeSymbols) {
opt_localeSymbols =
goog.locale.getResource('LocaleNameConstants', goog.locale.getLocale());
}
return regionCode in opt_localeSymbols['COUNTRY'] ?
opt_localeSymbols['COUNTRY'][regionCode] :
regionCode;
};
/**
* Returns the language name of the provided language code in its native
* language.
*
* This method depends on goog.locale.nativeNameConstants available from
* nativenameconstants.js. User of this method has to add dependency to this.
*
* @param {string} languageCode Language code to lookup the language name for.
*
* @return {string} Language name for the provided language code.
*/
goog.locale.getNativeLanguageName = function(languageCode) {
if (languageCode in goog.locale.nativeNameConstants['LANGUAGE'])
return goog.locale.nativeNameConstants['LANGUAGE'][languageCode];
var code = goog.locale.getLanguageSubTag(languageCode);
return code in goog.locale.nativeNameConstants['LANGUAGE'] ?
goog.locale.nativeNameConstants['LANGUAGE'][code] :
languageCode;
};
/**
* Returns the localized language name for the provided language code in
* the current or provided locale symbols set.
*
* This method depends on `goog.locale.LocaleNameConstants__<locale>` available
* from http://go/js_locale_data. User of this method has to add dependency to
* this.
*
* @param {string} languageCode Language code to lookup the language name for.
* @param {Object=} opt_localeSymbols locale symbol set if given.
*
* @return {string} Localized language name of the provided language code.
*/
goog.locale.getLocalizedLanguageName = function(
languageCode, opt_localeSymbols) {
if (!opt_localeSymbols) {
opt_localeSymbols =
goog.locale.getResource('LocaleNameConstants', goog.locale.getLocale());
}
if (languageCode in opt_localeSymbols['LANGUAGE'])
return opt_localeSymbols['LANGUAGE'][languageCode];
var code = goog.locale.getLanguageSubTag(languageCode);
return code in opt_localeSymbols['LANGUAGE'] ?
opt_localeSymbols['LANGUAGE'][code] :
languageCode;
};
/**
* Register a resource object for certain locale.
* @param {Object} dataObj The resource object being registered.
* @param {goog.locale.Resource|string} resourceName String that represents
* the type of resource.
* @param {string} localeName Locale ID.
*/
goog.locale.registerResource = function(dataObj, resourceName, localeName) {
if (!goog.locale.resourceRegistry_[resourceName]) {
goog.locale.resourceRegistry_[resourceName] = {};
}
goog.locale.resourceRegistry_[resourceName][localeName] = dataObj;
// the first registered locale becomes active one. Usually there will be
// only one locale per js binary bundle.
if (!goog.locale.activeLocale_) {
goog.locale.activeLocale_ = localeName;
}
};
/**
* Returns true if the required resource has already been registered.
* @param {goog.locale.Resource|string} resourceName String that represents
* the type of resource.
* @param {string} localeName Locale ID.
* @return {boolean} Whether the required resource has already been registered.
*/
goog.locale.isResourceRegistered = function(resourceName, localeName) {
return resourceName in goog.locale.resourceRegistry_ &&
localeName in goog.locale.resourceRegistry_[resourceName];
};
/**
* This object maps (resourceName, localeName) to a resourceObj.
* @type {Object}
* @private
*/
goog.locale.resourceRegistry_ = {};
/**
* Registers the timezone constants object for a given locale name.
* @param {Object} dataObj The resource object.
* @param {string} localeName Locale ID.
* @deprecated Use goog.i18n.TimeZone, no longer need this.
*/
goog.locale.registerTimeZoneConstants = function(dataObj, localeName) {
goog.locale.registerResource(
dataObj, goog.locale.Resource.TIME_ZONE_CONSTANTS, localeName);
};
/**
* Registers the LocaleNameConstants constants object for a given locale name.
* @param {Object} dataObj The resource object.
* @param {string} localeName Locale ID.
*/
goog.locale.registerLocaleNameConstants = function(dataObj, localeName) {
goog.locale.registerResource(
dataObj, goog.locale.Resource.LOCAL_NAME_CONSTANTS, localeName);
};
/**
* Registers the TimeZoneSelectedIds constants object for a given locale name.
* @param {Object} dataObj The resource object.
* @param {string} localeName Locale ID.
*/
goog.locale.registerTimeZoneSelectedIds = function(dataObj, localeName) {
goog.locale.registerResource(
dataObj, goog.locale.Resource.TIME_ZONE_SELECTED_IDS, localeName);
};
/**
* Registers the TimeZoneSelectedShortNames constants object for a given
* locale name.
* @param {Object} dataObj The resource object.
* @param {string} localeName Locale ID.
*/
goog.locale.registerTimeZoneSelectedShortNames = function(dataObj, localeName) {
goog.locale.registerResource(
dataObj, goog.locale.Resource.TIME_ZONE_SELECTED_SHORT_NAMES, localeName);
};
/**
* Registers the TimeZoneSelectedLongNames constants object for a given locale
* name.
* @param {Object} dataObj The resource object.
* @param {string} localeName Locale ID.
*/
goog.locale.registerTimeZoneSelectedLongNames = function(dataObj, localeName) {
goog.locale.registerResource(
dataObj, goog.locale.Resource.TIME_ZONE_SELECTED_LONG_NAMES, localeName);
};
/**
* Registers the TimeZoneAllLongNames constants object for a given locale name.
* @param {Object} dataObj The resource object.
* @param {string} localeName Locale ID.
*/
goog.locale.registerTimeZoneAllLongNames = function(dataObj, localeName) {
goog.locale.registerResource(
dataObj, goog.locale.Resource.TIME_ZONE_ALL_LONG_NAMES, localeName);
};
/**
* Retrieve specified resource for certain locale.
* @param {string} resourceName String that represents the type of resource.
* @param {string=} opt_locale Locale ID, if not given, current locale
* will be assumed.
* @return {Object|undefined} The resource object that hold all the resource
* data, or undefined if not available.
*/
goog.locale.getResource = function(resourceName, opt_locale) {
var locale = opt_locale ? opt_locale : goog.locale.getLocale();
if (!(resourceName in goog.locale.resourceRegistry_)) {
return undefined;
}
return goog.locale.resourceRegistry_[resourceName][locale];
};
/**
* Retrieve specified resource for certain locale with fallback. For example,
* request of 'zh_CN' will be resolved in following order: zh_CN, zh, en.
* If none of the above succeeds, of if the resource as indicated by
* resourceName does not exist at all, undefined will be returned.
*
* @param {string} resourceName String that represents the type of resource.
* @param {string=} opt_locale locale ID, if not given, current locale
* will be assumed.
* @return {Object|undefined} The resource object for desired locale.
*/
goog.locale.getResourceWithFallback = function(resourceName, opt_locale) {
var locale = opt_locale ? opt_locale : goog.locale.getLocale();
if (!(resourceName in goog.locale.resourceRegistry_)) {
return undefined;
}
if (locale in goog.locale.resourceRegistry_[resourceName]) {
return goog.locale.resourceRegistry_[resourceName][locale];
}
// if locale has multiple parts (2 atmost in reality), fallback to base part.
var locale_parts = locale.split('_');
if (locale_parts.length > 1 &&
locale_parts[0] in goog.locale.resourceRegistry_[resourceName]) {
return goog.locale.resourceRegistry_[resourceName][locale_parts[0]];
}
// otherwise, fallback to 'en'
return goog.locale.resourceRegistry_[resourceName]['en'];
};
// Export global functions that are used by the date time constants files.
// See http://go/js_locale_data
var registerLocalNameConstants = goog.locale.registerLocaleNameConstants;
var registerTimeZoneSelectedIds = goog.locale.registerTimeZoneSelectedIds;
var registerTimeZoneSelectedShortNames =
goog.locale.registerTimeZoneSelectedShortNames;
var registerTimeZoneSelectedLongNames =
goog.locale.registerTimeZoneSelectedLongNames;
var registerTimeZoneAllLongNames = goog.locale.registerTimeZoneAllLongNames;
|