如何通过 i18n 获取 Angular 项目中配置的所有翻译?

How to get all translations configured in Angular project by i18n?

我在 Angular 7 中使用以下语言使用 i18n(国际化):en、es、fr、it、pl、pt 和 ru。

我需要在项目中配置所有语言,以便使用它们的值设置下拉列表。我怎样才能获得这些在 JSON 文件中设置的语言,以便在下拉列表中使用它们?

我可以至少循环文件夹中的文件并恢复文件名吗?

到目前为止我能提供给你的是这样的。 直到我实现你正在寻找的东西,希望这会奏效。 这只是一个例子。

<ng-container *ngFor="let language of languageList">
 <a href="/{{language.code}}/">
 <button class="button">{{language.label}}</button>
 </a>
</ng-container>

这是在 .TS

中声明的列表
languageList = [
   { code: 'en', label: 'English' },
   { code: 'de', label: 'German' },
   { code: 'es', label: 'Espanol' }
];

您无法访问 i18n 文件夹以获取 angular 中的文件列表。

您需要创建 API 才能阅读。这种方式我在项目中实现,分享给关注的人

[Route("api/webconfiguration/getlanguages")]
        [AcceptVerbs("GET")]
        public dynamic GetLanguages()
        {    
            string path = ConfigurationManager.AppSettings["LanguageFolder"];

            DirectoryInfo dir = new DirectoryInfo(path);
            FileInfo[] files = dir.GetFiles("*.json");
            if (files.Length > 0)
            {
                var languages = files.Select(file => Path.GetFileNameWithoutExtension(file.Name));
                return languages.Select(c => new { code = c, name = new CultureInfo(c).DisplayName }).ToList();
            }

            return null;
        }

在web.config

中配置
<add key="LanguageFolder" value="C:\src\assets\i18n"/>

创建服务以获取语言列表并在组件中使用。

getLanguages() {
    return this.httpClient.get<any>('/api/webconfiguration/getlanguages').pipe(
      map(response => <any>response));
  }