From 70ce425e6a5c9d18caab754d03f6494b5174774e Mon Sep 17 00:00:00 2001 From: Peter Bacon Darwin Date: Thu, 16 Jul 2015 18:28:55 +0100 Subject: [PATCH] refactor($locale): use en-us as generic built-in locale Previously there was a custom built en-us locale that was included with angular.js. This made likely that it would get out of sync with the real en-us locale that is generated from the closure library. This change removes that custom one and uses the generated one instead. This also has the benefit of preventing the unwanted caught error on trying to load `ngLocale` during angular bootstrap. Closes #12134 Closes #8174 --- Gruntfile.js | 3 +- angularFiles.js | 16 ++++--- docs/content/guide/i18n.ngdoc | 6 +-- src/AngularPublic.js | 6 --- src/ng/locale.js | 81 ----------------------------------- src/ngLocale/index.js | 18 ++++++++ test/ng/localeSpec.js | 48 --------------------- 7 files changed, 33 insertions(+), 145 deletions(-) delete mode 100644 src/ng/locale.js create mode 100644 src/ngLocale/index.js delete mode 100644 test/ng/localeSpec.js diff --git a/Gruntfile.js b/Gruntfile.js index db263ea911bc..3ba868e931dc 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -175,7 +175,8 @@ module.exports = function(grunt) { }, angular: { dest: 'build/angular.js', - src: util.wrap([files['angularSrc']], 'angular'), + src: util.wrap([files['angularSrc']], 'angular') + .concat('src/ngLocale/angular-locale_en-us.js'), styles: { css: ['css/angular.css'], generateCspCssFile: true, diff --git a/angularFiles.js b/angularFiles.js index 702d5efd6290..524b53c49929 100755 --- a/angularFiles.js +++ b/angularFiles.js @@ -25,7 +25,6 @@ var angularFiles = { 'src/ng/httpBackend.js', 'src/ng/interpolate.js', 'src/ng/interval.js', - 'src/ng/locale.js', 'src/ng/location.js', 'src/ng/log.js', 'src/ng/parse.js', @@ -182,7 +181,8 @@ var angularFiles = { 'src/publishExternalApis.js', '@angularSrcModules', '@angularScenario', - '@angularTest' + '@angularTest', + 'src/ngLocale/angular-locale_en-us.js' // we need an ngLocale module ], 'karmaExclude': [ @@ -193,12 +193,13 @@ var angularFiles = { 'karmaScenario': [ 'build/angular-scenario.js', - 'build/docs/docs-scenario.js' + 'build/docs/docs-scenario.js', + 'src/ngLocale/angular-locale_en-us.js' // we need an ngLocale module ], "karmaModules": [ 'build/angular.js', - '@angularSrcModules', + '@angularSrcModules',, 'src/ngScenario/browserTrigger.js', 'test/helpers/*.js', 'test/ngMessageFormat/*.js', @@ -208,7 +209,8 @@ var angularFiles = { 'test/ngResource/*.js', 'test/ngSanitize/**/*.js', 'test/ngTouch/**/*.js', - 'test/ngAria/*.js' + 'test/ngAria/*.js', + 'src/ngLocale/angular-locale_en-us.js' // we need an ngLocale module ], 'karmaJquery': [ @@ -218,7 +220,9 @@ var angularFiles = { 'src/publishExternalApis.js', '@angularSrcModules', '@angularScenario', - '@angularTest' + '@angularTest', + 'src/ngLocale/angular-locale_en-us.js' // we need an ngLocale module + ], 'karmaJqueryExclude': [ diff --git a/docs/content/guide/i18n.ngdoc b/docs/content/guide/i18n.ngdoc index 7e70d5196cf3..2a81aef2d816 100644 --- a/docs/content/guide/i18n.ngdoc +++ b/docs/content/guide/i18n.ngdoc @@ -23,8 +23,8 @@ directive}. Additionally, you can use {@link guide/i18n#messageformat-extension `$interpolate` for localizable pluralization and gender support in all interpolations via the `ngMessageFormat` module. -All localizable Angular components depend on locale-specific rule sets managed by the {@link -ng.$locale `$locale` service}. +All localizable Angular components depend on locale-specific rule sets managed by the +{@link ngLocale `$locale` service}. There a few examples that showcase how to use Angular filters with various locale rule sets in the [`i18n/e2e` directory](https://github.com/angular/angular.js/tree/master/i18n/e2e) of the Angular @@ -99,7 +99,7 @@ develop your app. ### Currency symbol Angular's {@link ng.filter:currency currency filter} allows you to use the default currency symbol -from the {@link ng.$locale locale service}, or you can provide the filter with a custom currency +from the {@link ngLocale `locale` service}, or you can provide the filter with a custom currency symbol.
diff --git a/src/AngularPublic.js b/src/AngularPublic.js index 9afb79c16d6a..44f9932ece39 100644 --- a/src/AngularPublic.js +++ b/src/AngularPublic.js @@ -3,7 +3,6 @@ /* global angularModule: true, version: true, - $LocaleProvider, $CompileProvider, htmlAnchorDirective, @@ -150,11 +149,6 @@ function publishExternalAPI(angular) { }); angularModule = setupModuleLoader(window); - try { - angularModule('ngLocale'); - } catch (e) { - angularModule('ngLocale', []).provider('$locale', $LocaleProvider); - } angularModule('ng', ['ngLocale'], ['$provide', function ngModule($provide) { diff --git a/src/ng/locale.js b/src/ng/locale.js deleted file mode 100644 index e9c4dc7f72b8..000000000000 --- a/src/ng/locale.js +++ /dev/null @@ -1,81 +0,0 @@ -'use strict'; - -/** - * @ngdoc service - * @name $locale - * - * @description - * $locale service provides localization rules for various Angular components. As of right now the - * only public api is: - * - * * `id` – `{string}` – locale id formatted as `languageId-countryId` (e.g. `en-us`) - */ -function $LocaleProvider() { - this.$get = function() { - return { - id: 'en-us', - - NUMBER_FORMATS: { - DECIMAL_SEP: '.', - GROUP_SEP: ',', - PATTERNS: [ - { // Decimal Pattern - minInt: 1, - minFrac: 0, - maxFrac: 3, - posPre: '', - posSuf: '', - negPre: '-', - negSuf: '', - gSize: 3, - lgSize: 3 - },{ //Currency Pattern - minInt: 1, - minFrac: 2, - maxFrac: 2, - posPre: '\u00A4', - posSuf: '', - negPre: '(\u00A4', - negSuf: ')', - gSize: 3, - lgSize: 3 - } - ], - CURRENCY_SYM: '$' - }, - - DATETIME_FORMATS: { - MONTH: - 'January,February,March,April,May,June,July,August,September,October,November,December' - .split(','), - SHORTMONTH: 'Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec'.split(','), - DAY: 'Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday'.split(','), - SHORTDAY: 'Sun,Mon,Tue,Wed,Thu,Fri,Sat'.split(','), - AMPMS: ['AM','PM'], - medium: 'MMM d, y h:mm:ss a', - 'short': 'M/d/yy h:mm a', - fullDate: 'EEEE, MMMM d, y', - longDate: 'MMMM d, y', - mediumDate: 'MMM d, y', - shortDate: 'M/d/yy', - mediumTime: 'h:mm:ss a', - shortTime: 'h:mm a', - ERANAMES: [ - "Before Christ", - "Anno Domini" - ], - ERAS: [ - "BC", - "AD" - ] - }, - - pluralCat: function(num) { - if (num === 1) { - return 'one'; - } - return 'other'; - } - }; - }; -} diff --git a/src/ngLocale/index.js b/src/ngLocale/index.js new file mode 100644 index 000000000000..fbb4377dbe47 --- /dev/null +++ b/src/ngLocale/index.js @@ -0,0 +1,18 @@ +/** + * @ngdoc module + * @name ngLocale + * + * @description + * There is a different `ngLocale` module for each of the locales found in the closure library. + * See {@link i18n} + * Each module exposes a `$locale` service, which provides localization rules for various Angular components. + * Right now the only required property of the `$locale` service is: + * + * * `id` – `{string}` – locale id formatted as `languageId-countryId` (e.g. `en-us`) + * + * But most locales expose a number of other useful members such as: + * + * * `DATETIME_FORMATS` property + * * `NUMBER_FORMATS` property + * * `pluralCat` member + */ \ No newline at end of file diff --git a/test/ng/localeSpec.js b/test/ng/localeSpec.js deleted file mode 100644 index 1da9cf611062..000000000000 --- a/test/ng/localeSpec.js +++ /dev/null @@ -1,48 +0,0 @@ -'use strict'; - -describe('$locale', function() { - /* global $LocaleProvider: false */ - - var $locale = new $LocaleProvider().$get(); - - it('should have locale id set to en-us', function() { - expect($locale.id).toBe('en-us'); - }); - - - it('should have NUMBER_FORMATS', function() { - var numberFormats = $locale.NUMBER_FORMATS; - expect(numberFormats).toBeDefined(); - expect(numberFormats.PATTERNS.length).toBe(2); - angular.forEach(numberFormats.PATTERNS, function(pattern) { - expect(pattern.minInt).toBeDefined(); - expect(pattern.minFrac).toBeDefined(); - expect(pattern.maxFrac).toBeDefined(); - expect(pattern.posPre).toBeDefined(); - expect(pattern.posSuf).toBeDefined(); - expect(pattern.negPre).toBeDefined(); - expect(pattern.negSuf).toBeDefined(); - expect(pattern.gSize).toBeDefined(); - expect(pattern.lgSize).toBeDefined(); - }); - }); - - - it('should have DATETIME_FORMATS', function() { - var datetime = $locale.DATETIME_FORMATS; - expect(datetime).toBeDefined(); - expect(datetime.DAY.length).toBe(7); - expect(datetime.SHORTDAY.length).toBe(7); - expect(datetime.SHORTMONTH.length).toBe(12); - expect(datetime.MONTH.length).toBe(12); - expect(datetime.AMPMS.length).toBe(2); - }); - - - it('should return correct plural types', function() { - expect($locale.pluralCat(-1)).toBe('other'); - expect($locale.pluralCat(0)).toBe('other'); - expect($locale.pluralCat(2)).toBe('other'); - expect($locale.pluralCat(1)).toBe('one'); - }); -});