简体   繁体   English

带有ngx-translate的角+电子

[英]Angular + Electron with ngx-translate

I have an Angular application and now a desktop version of it is also needed. 我有一个Angular应用程序,现在还需要它的桌面版本。 I managed it to run the application in electron and everything works as expected. 我管理它以电子方式运行该应用程序,并且一切正常。

But the localization does not work. 但是本地化不起作用。 In the electron- application I only see the localization- keys which points to the actual translation in the localization files. 在电子应用程序中,我仅看到定位键,该键指向定位文件中的实际翻译。

I mainly translate the text in Angular like this: 我主要是这样在Angular中翻译文本:

{{ "localization-key" | translate }}

And the translations as json-files are based in assets/i18n/ 并且作为json文件的翻译基于资产/ i18n /

Does someone has an idea how to get the localization work? 有人知道如何进行本地化工作吗?

I also had this problem. 我也有这个问题。 I found the solution in the offical documentation of ngx-translate. 我在ngx-translate官方文档中找到了解决方案

If you want to configure a custom TranslateLoader while using AoT compilation, Ionic or Electron , you must use an exported function instead of an inline function. 如果要在使用AoT编译(Ionic或Electron)时配置自定义TranslateLoader,则必须使用导出函数而不是内联函数。

export function createTranslateLoader(http: HttpClient) {
    return new TranslateHttpLoader(http, './assets/i18n/', '.json');
}

@NgModule({
    imports: [
        BrowserModule,
        HttpClientModule,
        TranslateModule.forRoot({
            loader: {
                provide: TranslateLoader,
                useFactory: (createTranslateLoader),
                deps: [HttpClient]
            }
        })
    ],
    bootstrap: [AppComponent]
})
export class AppModule { }

If you used default TranslateHttpLoader for loader.useFactory , you see this error: 如果您对loader.useFactory使用默认的TranslateHttpLoader ,则会看到此错误: i18n表示装载机的配置错误。电子工厂

Its working now. 现在正在工作。 The problem was I used the Http- Loader for ngx-translate which was not working in electron. 问题是我使用了Http-Loader进行ngx-translate,但在电子设备中不起作用。

So I implemented the TranslateUniversalLoader like the last post of this thread mentioned: https://github.com/ngx-translate/core/issues/754 所以我像上面提到的该线程的最后一篇文章一样实现了TranslateUniversalLoaderhttps : //github.com/ngx-translate/core/issues/754

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM