如何动态导入语言环境?

时间:2018-01-22 23:13:59

标签: angular

我正在构建一个Angular 5应用。我的用户可以在我的应用中选择他们想要的任何文化。我真的应该把“locales”路径硬编码吗?如果是这样的话我应该把它们全部包括在内(以防万一)。

import localeFr from '@angular/common/locales/fr';
registerLocaleData(localeFr, 'fr');
import localePt from '@angular/common/locales/pt';
registerLocaleData(localePt, 'pt');
...

我想要这样的事情:

import locale from '@angular/common/locales/' + varUserLocale;
registerLocaleData(locale);

如何让它像我的例子一样更有活力?

由于

2 个答案:

答案 0 :(得分:1)

你试过这个吗?

导入(SELECT DISTINCT 'Actuals' as [Trans Type], ER101_ACCT_ORDER_DTL.ER101_START_DATE_ISO as [Order Date], ER101_ACCT_ORDER_DTL.ER101_ENT_DATE_ISO as [Ent Date], year(dateadd(month,6,ER101_ACCT_ORDER_DTL.ER101_START_DATE_ISO)) as [FYR], MONTH(DATEADD(M, 6, ER101_ACCT_ORDER_DTL.ER101_START_DATE_ISO)) as [FYP], DATEADD(mm, DATEDIFF(mm, 0, ER101_ACCT_ORDER_DTL.ER101_START_DATE_ISO), 0) as [PStart], DATEADD (dd, -1, DATEADD(mm, DATEDIFF(mm, 0, ER101_ACCT_ORDER_DTL.ER101_START_DATE_ISO) + 1, 0)) as [Pend], Revenue = Case When ER101_ACCT_ORDER_DTL.ER101_PHASE = '1' and ER101_ACCT_ORDER_DTL.ER101_COMPL_STS = 'N' then ER101_ACCT_ORDER_DTL.ER101_EXT_CHRG When ER101_ACCT_ORDER_DTL.ER101_PHASE = '5' and ER101_ACCT_ORDER_DTL.ER101_COMPL_STS = 'N' then ER101_ACCT_ORDER_DTL.ER101_EXT_CHRG Else 0 End, isnull(MM405_USER_NAME,'No SAM') AS [SAM], ER101_ACCT_ORDER_DTL.ER101_DESC as [Desc], EV200_EVENT_MASTER.EV200_EVT_ID as [EventID], isnull(SALESACCT.EV870_TYPE,'NoSec') as [Sector], isnull(CCRPF_ACCT_TYPE_DESC,'ZZ - No Sector') as [Sector Name], EV130_STATUS_MASTER.EV130_STATUS_DESC as [Event Status Desc], isnull(EV800_USER_FLD_6X,'N') as [Budgeted] FROM ER101_ACCT_ORDER_DTL WITH (NOLOCK) LEFT OUTER JOIN EV700_FUNC_MASTER WITH (NOLOCK) ON ER101_ACCT_ORDER_DTL.ER101_ORG_CODE = EV700_FUNC_MASTER.EV700_ORG_CODE AND ER101_ACCT_ORDER_DTL.ER101_EVT_ID = EV700_FUNC_MASTER.EV700_EVT_ID AND ER101_ACCT_ORDER_DTL.ER101_FUNC_ID = EV700_FUNC_MASTER.EV700_FUNC_ID LEFT OUTER JOIN EV800_SPACE_MASTER WITH (NOLOCK) ON EV700_FUNC_MASTER.EV700_SPACE = EV800_SPACE_MASTER.EV800_SPACE_CODE AND EV700_FUNC_MASTER.EV700_ORG_CODE = EV800_SPACE_MASTER.EV800_ORG_CODE LEFT OUTER JOIN EV200_EVENT_MASTER WITH (NOLOCK) ON ER101_ACCT_ORDER_DTL.ER101_ORG_CODE = EV200_EVENT_MASTER.EV200_ORG_CODE AND ER101_ACCT_ORDER_DTL.ER101_EVT_ID = EV200_EVENT_MASTER.EV200_EVT_ID LEFT OUTER JOIN EV870_ACCT_MASTER WITH (NOLOCK) ON EV200_EVENT_MASTER.EV200_ORG_CODE = EV870_ACCT_MASTER.EV870_ORG_CODE AND EV200_EVENT_MASTER.EV200_SLSPER = EV870_ACCT_MASTER.EV870_ACCT_CODE LEFT OUTER JOIN EV870_ACCT_MASTER SALESACCT WITH (NOLOCK) ON EV200_EVENT_MASTER.EV200_ORG_CODE = SALESACCT.EV870_ORG_CODE AND EV200_EVENT_MASTER.EV200_CUST_NBR = SALESACCT.EV870_ACCT_CODE LEFT OUTER JOIN CCRPF_ACCT_TYPES WITH (NOLOCK) ON SALESACCT.EV870_TYPE = CCRPF_ACCT_TYPE LEFT OUTER JOIN MM405_USER_MASTER_EXT WITH (NOLOCK) ON EV870_ACCT_MASTER.EV870_USERID = MM405_USER_MASTER_EXT.MM405_USER_ID LEFT OUTER JOIN EV130_STATUS_MASTER WITH (NOLOCK) ON EV200_EVENT_MASTER.EV200_EVT_STATUS = EV130_STATUS_MASTER.EV130_STATUS_CODE LEFT OUTER JOIN EV370_RES_MASTER WITH (NOLOCK) ON ER101_ACCT_ORDER_DTL.ER101_ORG_CODE = EV370_RES_MASTER.EV370_ORG_CODE AND ER101_ACCT_ORDER_DTL.ER101_NEW_RES_TYPE = EV370_RES_MASTER.EV370_NEW_RES_TYPE AND ER101_ACCT_ORDER_DTL.ER101_RES_CODE = EV370_RES_MASTER.EV370_RES_CODE WHERE EV200_ORG_CODE = '10' and EV200_EVT_STATUS in ('33', '34', '36', '49','50', '52') and NOT(EV200_EVT_TYPE IN ('MOS','MP','GB')) and ER101_RES_CLASS <> '8' and ER101_ORD_TYPE = 'SO' and not(EV370_RES_MASTER.EV370_MAJOR_GROUP in ('SRVCHR', 'EBOND', 'SCHARG','GIFTC')) UNION Select 'Targets' as [Trans Type], MM012_PERIOD_STDATE as [Order Date], GL010_ENT_STAMP as [Ent Date], MM012_FISCAL_YR as [FYR], MM012_FISCAL_PERIOD as [FYP], MM012_PERIOD_STDATE as [PStart], MM012_PERIOD_ENDATE as [Pend], GL010_BUD_PTD as [Revenue], 'Budget SAM' as [SAM], GL010_ACCOUNT as [Desc], null as [Event ID], GL001_SHORT_DESC as [Sector], [Sector Name] = Case When CCRPF_ACCT_TYPE_DESC is null then 'OLD - ' + GL001_DESC Else CCRPF_ACCT_TYPE_DESC End, 'Budget Event Status Desc' as [Event Status Desc], 'Targets' as [Budgeted] FROM GL010_BALANCES WITH (NOLOCK) LEFT OUTER JOIN GL001_ACCOUNT_MASTER WITH (NOLOCK) ON GL010_ORG_CODE = GL001_ORG_CODE AND GL010_ACCOUNT = GL001_ACCOUNT LEFT OUTER JOIN MM012_FISCAL_PERIODS WITH (NOLOCK) ON GL010_ORG_CODE = MM012_ORG_CODE AND GL010_FISCAL_YR = MM012_FISCAL_YR AND GL010_FISCAL_PERIOD = MM012_FISCAL_PERIOD LEFT OUTER JOIN dbo.CCRPF_ACCT_TYPES WITH (NOLOCK) ON GL001_SHORT_DESC = CCRPF_ACCT_TYPE Where GL001_ACCT_TYPE = 'SS' and LEFT(GL001_ACCOUNT_MASTER.GL001_ACCOUNT,3)='SEC' )。then(locale =&gt; {   registerLocaleData(locale.default); });

来源:Angular 5 loading locale at runtime with Angular CLI

答案 1 :(得分:1)

export interface ILanguageOption {
  languageId: number;
  name: string;
  value: string;
  priority: number;
  import: string;
}

  public static readonly LANGUAGES: ILanguageOption[] = [
    { languageId: 2, name: 'RU', value: 'ru-RU', priority: 2, import: 'ru' },
    { languageId: 1, name: 'EN', value: 'en-UK', priority: 1, import: 'en-GB' }
  ];

在app.modele.ts

import { registerLocaleData } from '@angular/common';

export function localeInitializerFactory(): () => Promise<any> {
  return () => {
    const all = Consts.LANGUAGES.map(l => {
      Promise.all([
        import(/* webpackInclude: /(ru|en-GB)\.js$/ */ `@angular/common/locales/${l.import}.js`),
        import(/* webpackInclude: /(ru|en-GB)\.js$/ */ `@angular/common/locales/extra/${l.import}.js`)
      ]).then(imp => registerLocaleData(imp[0].default, l.value, imp[1].default));
    });
    return Promise.all(all);
  }
}

{
  provide: APP_INITIALIZER,
  useFactory: localeInitializerFactory,
  multi: true
},