Javascript angular2 i18n的替代品

Javascript angular2 i18n的替代品,javascript,angular,typescript,Javascript,Angular,Typescript,我正在寻找一种方便的方法来使用angular2内置的i18n支持,以便处理API返回的服务器端消息。例如,我有一个大的动态菜单,其中包含来自服务器的每个项目的标签。我还有来自API的消息(错误、警告、结果代码) 因为我发现了我认为阻塞的问题,我也在寻找一个第三方库,允许简单的键/值转换消息,并且可以包含在ang2应用程序中。我是Angular2的新手,所以我想听听关于这个案例的社区认可图书馆的情况 谢谢 我正在寻找一个库,它允许简单的键/值转换消息,并且可以包含在angular2应用程序中 写起

我正在寻找一种方便的方法来使用angular2内置的i18n支持,以便处理API返回的服务器端消息。例如,我有一个大的动态菜单,其中包含来自服务器的每个项目的标签。我还有来自API的消息(错误、警告、结果代码)

因为我发现了我认为阻塞的问题,我也在寻找一个第三方库,允许简单的键/值转换消息,并且可以包含在ang2应用程序中。我是Angular2的新手,所以我想听听关于这个案例的社区认可图书馆的情况

谢谢

我正在寻找一个库,它允许简单的键/值转换消息,并且可以包含在angular2应用程序中

写起来很容易:

type Messages = {
  error: string,
  hello: string,
}

export function getMessage(message: keyof Messages, localization = 'en') {
  /** Define your messages */
  const messages: {
    [localization: string]: Messages;
  } = {
      en: {
        error: 'Unexpected error',
        hello: 'hello world'
      }
    }
  return messages[localization][message];
}

// Usage 
getMessage('error'); // OK
getMessage('notvalid'); // Compile time error
您可以根据需要将此模式扩展为功能强大的模式 我正在寻找一个库,它允许简单的键/值转换消息,并且可以包含在angular2应用程序中

写起来很容易:

type Messages = {
  error: string,
  hello: string,
}

export function getMessage(message: keyof Messages, localization = 'en') {
  /** Define your messages */
  const messages: {
    [localization: string]: Messages;
  } = {
      en: {
        error: 'Unexpected error',
        hello: 'hello world'
      }
    }
  return messages[localization][message];
}

// Usage 
getMessage('error'); // OK
getMessage('notvalid'); // Compile time error

您可以根据需要扩展此模式,使其功能强大我知道这不是您要寻找的答案,但我只想说明,使用Angular的静态翻译为服务器错误提供消息值是很简单的

@Component(
  template: `
    <ng-container [ngSwitch]="errorCode">
      <ng-container *ngSwitchCase="error1" i18n>A bad thing happend</ng-container>
      <ng-container *ngSwitchCase="5152" i18n>Error in AE35 unit</ng-container>
      <ng-container *ngSwitchCaseDefault i18n>Unknown Error</ng-container>
    </ng-container>
  `
)
export class ErrorMessageComponent {
  @Input errorCode: number | string;
}
@组件(
模板:`
发生了一件坏事
AE35单元中的错误
未知错误
`
)
导出类ErrorMessageComponent{
@输入错误代码:数字|字符串;
}

我知道这不是您要寻找的答案,但我只想证明,使用Angular的静态翻译为服务器错误提供消息值是微不足道的

@Component(
  template: `
    <ng-container [ngSwitch]="errorCode">
      <ng-container *ngSwitchCase="error1" i18n>A bad thing happend</ng-container>
      <ng-container *ngSwitchCase="5152" i18n>Error in AE35 unit</ng-container>
      <ng-container *ngSwitchCaseDefault i18n>Unknown Error</ng-container>
    </ng-container>
  `
)
export class ErrorMessageComponent {
  @Input errorCode: number | string;
}
@组件(
模板:`
发生了一件坏事
AE35单元中的错误
未知错误
`
)
导出类ErrorMessageComponent{
@输入错误代码:数字|字符串;
}

谢谢@developer033这是我要找的库,如果你愿意,可以作为答案发布。谢谢@developer033这是我要找的库,如果你愿意,可以作为答案发布。