简体   繁体   English

Angular 9 通用为什么只有一半的app用ngx-translate翻译?

[英]Angular 9 universal why only half app is translated with ngx-translate?

I have angular 9 pwa universal web app which after npm run build:ssr works with node dist/app/server/server.js .我有 angular 9 pwa 通用 web 应用程序,在npm run build:ssr与 node dist/app/server/server.js一起使用。 I translated all text, but after build I see only half translated and another half like: common.next , common.back .我翻译了所有文本,但在构建之后我只看到一半被翻译,另一半像: common.nextcommon.back

Here is what i have:这是我所拥有的:

app.module:应用程序模块:

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

imports: [
...
    TransferHttpCacheModule,
    HttpClientModule,
    TranslateModule.forRoot({
      loader: {
        provide: TranslateLoader,
        useFactory: (createTranslateLoader),
        deps: [HttpClient]
      }
    }),
...
]

app.server.module:应用程序服务器模块:

import { NgModule } from '@angular/core';
import { ServerModule, ServerTransferStateModule } from '@angular/platform-server';
import { BrowserModule } from '@angular/platform-browser';

import { AppModule } from './app.module';
import { AppComponent } from './app.component';

@NgModule({
  imports: [
    AppModule,
    ServerModule,
    ServerTransferStateModule,
    BrowserModule.withServerTransition({ appId: 'autorent' }),
  ],
  bootstrap: [AppComponent],
})
export class AppServerModule { }

app.browser.module: app.browser.module:

import { NgModule } from '@angular/core';
import { BrowserModule, BrowserTransferStateModule } from '@angular/platform-browser';

import { AppModule } from './app.module';
import { AppComponent } from './app.component';
import { ServiceWorkerModule } from '@angular/service-worker';
import { environment } from '../environments/environment';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';

@NgModule({
  imports: [
    AppModule,
    BrowserModule.withServerTransition({ appId: 'autorent' }),
    BrowserTransferStateModule,
    ServiceWorkerModule.register('ngsw-worker.js', { enabled: environment.production }),
    BrowserAnimationsModule
  ],
  bootstrap: [AppComponent],
})
export class AppBrowserModule { }

If you need another info, which I doubt, just write in comments and I will provide.如果您需要我怀疑的其他信息,请在评论中写下,我会提供。

How to make that ngx-translate would work fully + won't be loaded on page load, It should lode on server I think, because when I open app for the first time, i see how text changes (half of it)如何使 ngx-translate 完全工作 + 不会在页面加载时加载,我认为它应该在服务器上加载,因为当我第一次打开应用程序时,我看到文本如何变化(其中一半)

As you use separate translation files and load them after the code ( TranslateHttpLoader(http, './assets/i18n/', '.json'); ) this json file is cached by the browser.当您使用单独的翻译文件并在代码之后加载它们( TranslateHttpLoader(http, './assets/i18n/', '.json'); )这个 json 文件被浏览器缓存。 As you're not using content hashes there, it is likely that a newer version of you code is getting older translation files from the browser cache.由于您没有在那里使用内容哈希,因此您的代码的较新版本可能会从浏览器缓存中获取较旧的翻译文件。 (You can verify this with the chrome dev tools, there is a disable cache checkbox) (您可以使用 chrome 开发工具验证这一点,有一个禁用缓存复选框)

You have several options there:你有几个选择:

  1. Delete your browser cache and tell your users to do the same after every redeploy删除您的浏览器缓存并告诉您的用户在每次重新部署后都这样做
  2. Configure the web server to serve the translation file with a very short cache lifetime配置 web 服务器以提供具有非常短缓存寿命的翻译文件
  3. Use some kind of content-hashing where the requested resource is different after a redeploy so that the browser cache does not hit if the file has changed.在重新部署后请求的资源不同的地方使用某种内容散列,以便在文件更改时浏览器缓存不会命中。 (for example, modify your build process so that the filename ends like .json?67d383 where the last part is replaced at build time with the git commit hash of the code that was build) (例如,修改您的构建过程,使文件名以.json?67d383 ,其中最后一部分在构建时替换为 git 提交 hash 代码)
  4. compile the translation into the javascript bundle将翻译编译成 javascript 包

I have used options 1, 3 and 4 and recommend the last (for applications with little different locales).我使用了选项 1、3 和 4,并推荐了最后一个(适用于语言环境差异不大的应用程序)。 The javascript bundle cannot display anything meaningful without the translations, so it might as well include them.如果没有翻译,javascript 捆绑包无法显示任何有意义的内容,因此不妨将其包含在内。

You can directly include the json files for all the languages into the typescript file and use them as a constant.您可以将所有语言的 json 文件直接包含到 typescript 文件中,并将它们用作常量。 Switch as needed, as any loading of translations will be immediate (no http call) and the translations will always be accurate.根据需要切换,因为任何翻译加载都将立即进行(没有 http 调用)并且翻译将始终准确。 Content hashing should be configured for the javascript bundle already (by default).应该已经为 javascript 包配置了内容散列(默认情况下)。

If you have many languages, I'd rather move towards approach 3 for bundle size.如果您有多种语言,我宁愿采用方法 3 来获取包大小。

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

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