Path: blob/trunk/third_party/closure/goog/locale/locale.js
2868 views
// Copyright 2006 The Closure Library Authors. All Rights Reserved.1//2// Licensed under the Apache License, Version 2.0 (the "License");3// you may not use this file except in compliance with the License.4// You may obtain a copy of the License at5//6// http://www.apache.org/licenses/LICENSE-2.07//8// Unless required by applicable law or agreed to in writing, software9// distributed under the License is distributed on an "AS-IS" BASIS,10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.11// See the License for the specific language governing permissions and12// limitations under the License.1314/**15* @fileoverview Functions for dealing with Date formatting & Parsing,16* County and language name, TimeZone list.17* @suppress {deprecated} Use goog.i18n instead.18*/192021/**22* Namespace for locale related functions.23*/24goog.provide('goog.locale');2526goog.require('goog.locale.nativeNameConstants');272829/**30* Set current locale to the specified one.31* @param {string} localeName Locale name string. We are following the usage32* in CLDR, but can make a few compromise for existing name compatibility.33*/34goog.locale.setLocale = function(localeName) {35// it is common to see people use '-' as locale part separator, normalize it.36localeName = localeName.replace(/-/g, '_');37goog.locale.activeLocale_ = localeName;38};394041/**42* Retrieve the current locale43* @return {string} Current locale name string.44* @deprecated Use goog.LOCALE and goog.i18n instead.45*/46goog.locale.getLocale = function() {47if (!goog.locale.activeLocale_) {48goog.locale.activeLocale_ = 'en';49}50return goog.locale.activeLocale_;51};525354// Couple of constants to represent predefined Date/Time format type.55/**56* Enum of resources that can be registered.57* @enum {string}58*/59goog.locale.Resource = {60DATE_TIME_CONSTANTS: 'DateTimeConstants',61NUMBER_FORMAT_CONSTANTS: 'NumberFormatConstants',62TIME_ZONE_CONSTANTS: 'TimeZoneConstants',63LOCAL_NAME_CONSTANTS: 'LocaleNameConstants',6465TIME_ZONE_SELECTED_IDS: 'TimeZoneSelectedIds',66TIME_ZONE_SELECTED_SHORT_NAMES: 'TimeZoneSelectedShortNames',67TIME_ZONE_SELECTED_LONG_NAMES: 'TimeZoneSelectedLongNames',68TIME_ZONE_ALL_LONG_NAMES: 'TimeZoneAllLongNames'69};707172// BCP 47 language code:73//74// LanguageCode := LanguageSubtag75// ("-" ScriptSubtag)?76// ("-" RegionSubtag)?77// ("-" VariantSubtag)?78// ("@" Keyword "=" Value ("," Keyword "=" Value)* )?79//80// e.g. en-Latn-GB81//82// NOTICE:83// No special format checking is performed. If you pass a none valid84// language code as parameter to the following functions,85// you might get an unexpected result.868788/**89* Returns the language-subtag of the given language code.90*91* @param {string} languageCode Language code to extract language subtag from.92* @return {string} Language subtag (in lowercase).93*/94goog.locale.getLanguageSubTag = function(languageCode) {95var result = languageCode.match(/^\w{2,3}([-_]|$)/);96return result ? result[0].replace(/[_-]/g, '') : '';97};9899100/**101* Returns the region-sub-tag of the given language code.102*103* @param {string} languageCode Language code to extract region subtag from.104* @return {string} Region sub-tag (in uppercase).105*/106goog.locale.getRegionSubTag = function(languageCode) {107var result = languageCode.match(/[-_]([a-zA-Z]{2}|\d{3})([-_]|$)/);108return result ? result[0].replace(/[_-]/g, '') : '';109};110111112/**113* Returns the script subtag of the locale with the first alphabet in uppercase114* and the rest 3 characters in lower case.115*116* @param {string} languageCode Language Code to extract script subtag from.117* @return {string} Script subtag.118*/119goog.locale.getScriptSubTag = function(languageCode) {120var result = languageCode.split(/[-_]/g);121return result.length > 1 && result[1].match(/^[a-zA-Z]{4}$/) ? result[1] : '';122};123124125/**126* Returns the variant-sub-tag of the given language code.127*128* @param {string} languageCode Language code to extract variant subtag from.129* @return {string} Variant sub-tag.130*/131goog.locale.getVariantSubTag = function(languageCode) {132var result = languageCode.match(/[-_]([a-z]{2,})/);133return result ? result[1] : '';134};135136137/**138* Returns the country name of the provided language code in its native139* language.140*141* This method depends on goog.locale.nativeNameConstants available from142* nativenameconstants.js. User of this method has to add dependency to this.143*144* @param {string} countryCode Code to lookup the country name for.145*146* @return {string} Country name for the provided language code.147*/148goog.locale.getNativeCountryName = function(countryCode) {149var key = goog.locale.getLanguageSubTag(countryCode) + '_' +150goog.locale.getRegionSubTag(countryCode);151return key in goog.locale.nativeNameConstants['COUNTRY'] ?152goog.locale.nativeNameConstants['COUNTRY'][key] :153countryCode;154};155156157/**158* Returns the localized country name for the provided language code in the159* current or provided locale symbols set.160*161* This method depends on `goog.locale.LocaleNameConstants__<locale>` available162* from http://go/js_locale_data. User of this method has to add dependency to163* this.164*165* @param {string} languageCode Language code to lookup the country name for.166* @param {Object=} opt_localeSymbols If omitted the current locale symbol167* set is used.168*169* @return {string} Localized country name.170*/171goog.locale.getLocalizedCountryName = function(172languageCode, opt_localeSymbols) {173var code = goog.locale.getRegionSubTag(languageCode);174var name =175goog.locale.getLocalizedRegionNameFromRegionCode(code, opt_localeSymbols);176return name == code ? languageCode : name;177};178179/**180* Returns the localized country name for the provided language code in the181* current or provided locale symbols set.182*183* This method depends on `goog.locale.LocaleNameConstants__<locale>` available184* from http://go/js_locale_data. User of this method has to add dependency to185* this.186*187* @param {string} regionCode Two character country code or three digit region188* code to look up the country name for.189* @param {?Object=} opt_localeSymbols If omitted the current locale symbol190* set is used.191*192* @return {string} Localized region name.193*/194goog.locale.getLocalizedRegionNameFromRegionCode = function(195regionCode, opt_localeSymbols) {196if (!opt_localeSymbols) {197opt_localeSymbols =198goog.locale.getResource('LocaleNameConstants', goog.locale.getLocale());199}200return regionCode in opt_localeSymbols['COUNTRY'] ?201opt_localeSymbols['COUNTRY'][regionCode] :202regionCode;203};204205/**206* Returns the language name of the provided language code in its native207* language.208*209* This method depends on goog.locale.nativeNameConstants available from210* nativenameconstants.js. User of this method has to add dependency to this.211*212* @param {string} languageCode Language code to lookup the language name for.213*214* @return {string} Language name for the provided language code.215*/216goog.locale.getNativeLanguageName = function(languageCode) {217if (languageCode in goog.locale.nativeNameConstants['LANGUAGE'])218return goog.locale.nativeNameConstants['LANGUAGE'][languageCode];219var code = goog.locale.getLanguageSubTag(languageCode);220return code in goog.locale.nativeNameConstants['LANGUAGE'] ?221goog.locale.nativeNameConstants['LANGUAGE'][code] :222languageCode;223};224225226/**227* Returns the localized language name for the provided language code in228* the current or provided locale symbols set.229*230* This method depends on `goog.locale.LocaleNameConstants__<locale>` available231* from http://go/js_locale_data. User of this method has to add dependency to232* this.233*234* @param {string} languageCode Language code to lookup the language name for.235* @param {Object=} opt_localeSymbols locale symbol set if given.236*237* @return {string} Localized language name of the provided language code.238*/239goog.locale.getLocalizedLanguageName = function(240languageCode, opt_localeSymbols) {241if (!opt_localeSymbols) {242opt_localeSymbols =243goog.locale.getResource('LocaleNameConstants', goog.locale.getLocale());244}245if (languageCode in opt_localeSymbols['LANGUAGE'])246return opt_localeSymbols['LANGUAGE'][languageCode];247var code = goog.locale.getLanguageSubTag(languageCode);248return code in opt_localeSymbols['LANGUAGE'] ?249opt_localeSymbols['LANGUAGE'][code] :250languageCode;251};252253254/**255* Register a resource object for certain locale.256* @param {Object} dataObj The resource object being registered.257* @param {goog.locale.Resource|string} resourceName String that represents258* the type of resource.259* @param {string} localeName Locale ID.260*/261goog.locale.registerResource = function(dataObj, resourceName, localeName) {262if (!goog.locale.resourceRegistry_[resourceName]) {263goog.locale.resourceRegistry_[resourceName] = {};264}265goog.locale.resourceRegistry_[resourceName][localeName] = dataObj;266// the first registered locale becomes active one. Usually there will be267// only one locale per js binary bundle.268if (!goog.locale.activeLocale_) {269goog.locale.activeLocale_ = localeName;270}271};272273274/**275* Returns true if the required resource has already been registered.276* @param {goog.locale.Resource|string} resourceName String that represents277* the type of resource.278* @param {string} localeName Locale ID.279* @return {boolean} Whether the required resource has already been registered.280*/281goog.locale.isResourceRegistered = function(resourceName, localeName) {282return resourceName in goog.locale.resourceRegistry_ &&283localeName in goog.locale.resourceRegistry_[resourceName];284};285286287/**288* This object maps (resourceName, localeName) to a resourceObj.289* @type {Object}290* @private291*/292goog.locale.resourceRegistry_ = {};293294295/**296* Registers the timezone constants object for a given locale name.297* @param {Object} dataObj The resource object.298* @param {string} localeName Locale ID.299* @deprecated Use goog.i18n.TimeZone, no longer need this.300*/301goog.locale.registerTimeZoneConstants = function(dataObj, localeName) {302goog.locale.registerResource(303dataObj, goog.locale.Resource.TIME_ZONE_CONSTANTS, localeName);304};305306307/**308* Registers the LocaleNameConstants constants object for a given locale name.309* @param {Object} dataObj The resource object.310* @param {string} localeName Locale ID.311*/312goog.locale.registerLocaleNameConstants = function(dataObj, localeName) {313goog.locale.registerResource(314dataObj, goog.locale.Resource.LOCAL_NAME_CONSTANTS, localeName);315};316317318/**319* Registers the TimeZoneSelectedIds constants object for a given locale name.320* @param {Object} dataObj The resource object.321* @param {string} localeName Locale ID.322*/323goog.locale.registerTimeZoneSelectedIds = function(dataObj, localeName) {324goog.locale.registerResource(325dataObj, goog.locale.Resource.TIME_ZONE_SELECTED_IDS, localeName);326};327328329/**330* Registers the TimeZoneSelectedShortNames constants object for a given331* locale name.332* @param {Object} dataObj The resource object.333* @param {string} localeName Locale ID.334*/335goog.locale.registerTimeZoneSelectedShortNames = function(dataObj, localeName) {336goog.locale.registerResource(337dataObj, goog.locale.Resource.TIME_ZONE_SELECTED_SHORT_NAMES, localeName);338};339340341/**342* Registers the TimeZoneSelectedLongNames constants object for a given locale343* name.344* @param {Object} dataObj The resource object.345* @param {string} localeName Locale ID.346*/347goog.locale.registerTimeZoneSelectedLongNames = function(dataObj, localeName) {348goog.locale.registerResource(349dataObj, goog.locale.Resource.TIME_ZONE_SELECTED_LONG_NAMES, localeName);350};351352353/**354* Registers the TimeZoneAllLongNames constants object for a given locale name.355* @param {Object} dataObj The resource object.356* @param {string} localeName Locale ID.357*/358goog.locale.registerTimeZoneAllLongNames = function(dataObj, localeName) {359goog.locale.registerResource(360dataObj, goog.locale.Resource.TIME_ZONE_ALL_LONG_NAMES, localeName);361};362363364/**365* Retrieve specified resource for certain locale.366* @param {string} resourceName String that represents the type of resource.367* @param {string=} opt_locale Locale ID, if not given, current locale368* will be assumed.369* @return {Object|undefined} The resource object that hold all the resource370* data, or undefined if not available.371*/372goog.locale.getResource = function(resourceName, opt_locale) {373var locale = opt_locale ? opt_locale : goog.locale.getLocale();374375if (!(resourceName in goog.locale.resourceRegistry_)) {376return undefined;377}378return goog.locale.resourceRegistry_[resourceName][locale];379};380381382/**383* Retrieve specified resource for certain locale with fallback. For example,384* request of 'zh_CN' will be resolved in following order: zh_CN, zh, en.385* If none of the above succeeds, of if the resource as indicated by386* resourceName does not exist at all, undefined will be returned.387*388* @param {string} resourceName String that represents the type of resource.389* @param {string=} opt_locale locale ID, if not given, current locale390* will be assumed.391* @return {Object|undefined} The resource object for desired locale.392*/393goog.locale.getResourceWithFallback = function(resourceName, opt_locale) {394var locale = opt_locale ? opt_locale : goog.locale.getLocale();395396if (!(resourceName in goog.locale.resourceRegistry_)) {397return undefined;398}399400if (locale in goog.locale.resourceRegistry_[resourceName]) {401return goog.locale.resourceRegistry_[resourceName][locale];402}403404// if locale has multiple parts (2 atmost in reality), fallback to base part.405var locale_parts = locale.split('_');406if (locale_parts.length > 1 &&407locale_parts[0] in goog.locale.resourceRegistry_[resourceName]) {408return goog.locale.resourceRegistry_[resourceName][locale_parts[0]];409}410411// otherwise, fallback to 'en'412return goog.locale.resourceRegistry_[resourceName]['en'];413};414415416// Export global functions that are used by the date time constants files.417// See http://go/js_locale_data418var registerLocalNameConstants = goog.locale.registerLocaleNameConstants;419420var registerTimeZoneSelectedIds = goog.locale.registerTimeZoneSelectedIds;421var registerTimeZoneSelectedShortNames =422goog.locale.registerTimeZoneSelectedShortNames;423var registerTimeZoneSelectedLongNames =424goog.locale.registerTimeZoneSelectedLongNames;425var registerTimeZoneAllLongNames = goog.locale.registerTimeZoneAllLongNames;426427428