Skip to content

Commit

Permalink
feat(module:locale): support change locale at the runtime (globally)
Browse files Browse the repository at this point in the history
  • Loading branch information
wilsoncook committed Oct 28, 2017
1 parent 5ec27d3 commit af6c926
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 14 deletions.
7 changes: 1 addition & 6 deletions src/components/locale/locales/en-US.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
import { NzLocale } from '../nz-locale.class';
import * as moment from 'moment';

const LOCALE = 'en-US';

moment.locale(LOCALE);

export const enUS: NzLocale = {
locale: LOCALE,
locale: 'en-US',

Pagination: {
itemsPerPage: '%num% / page',
Expand Down
7 changes: 1 addition & 6 deletions src/components/locale/locales/zh-CN.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
import { NzLocale } from '../nz-locale.class';
import * as moment from 'moment';

const LOCALE = 'zh-CN';

moment.locale(LOCALE);

export const zhCN: NzLocale = {
locale: LOCALE,
locale: 'zh-CN',

Pagination: {
itemsPerPage: '%num% 条/页',
Expand Down
7 changes: 7 additions & 0 deletions src/components/locale/nz-locale.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Injectable, Inject, Provider, Optional, SkipSelf } from '@angular/core'
import { LoggerService } from '../util/logger/index';
import { NZ_LOCALE } from './nz-locale.token';
import { NzLocale } from './nz-locale.class';
import * as moment from 'moment';

@Injectable()
export class NzLocaleService {
Expand All @@ -25,7 +26,13 @@ export class NzLocaleService {
return path;
}

/**
* Set/Change current locale globally throughout the WHOLE application
* [NOTE] If called at runtime, rendered interface may not change along with the locale change (because this do not trigger another render schedule)
* @param locale The translating letters
*/
setLocale(locale: NzLocale) {
moment.locale(locale.locale);
this._locale = locale;
}

Expand Down
2 changes: 0 additions & 2 deletions src/showcase/nz-demo-locale/nz-demo-locale-all.component.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Component, OnInit } from '@angular/core';
import * as moment from 'moment';
import { zhCN, enUS, NzModalService, NzLocaleService } from '../../../index.showcase';

@Component({
Expand Down Expand Up @@ -38,7 +37,6 @@ export class NzDemoLocaleAllComponent implements OnInit {
set locale(locale) {
this._locale = locale;
/* Switch locale manually (DEMO ONLY) */
moment.locale(locale.locale);
this._localeService.setLocale(locale);
}

Expand Down

0 comments on commit af6c926

Please sign in to comment.