角度翻译如何工作?

时间:2020-05-13 12:09:56

标签: angular internationalization

我正在尝试开发一个小应用程序,我想用几种语言(目前是英语和法语)生成它,但是我有一些麻烦:

  1. 法语消息仅包含英语翻译
  2. 启动法语应用程序时,不会显示任何文本(仅显示输入字段,按钮等)。

对于翻译,我创建了2个文件:

  • messages.xlf为默认语言(en),其中消息位于“源”中
  • message.fr.xlf(法语),其中消息包含参考消息的“源”和法语翻译的“ target”。

我正在使用Angular 9.1.3版本。 我看不到我错了。谢谢你的想法。

我的angular.json文件:

{
  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "version": 1,
  "newProjectRoot": "projects",
  "projects": {
    "hammer-client": {
      "projectType": "application",
      "schematics": {
        "@schematics/angular:component": {
          "style": "css"
        }
      },
      "root": "",
      "sourceRoot": "src",
      "prefix": "hm",
      "i18n": {
        "sourceLocale": "en-US",
        "locales": {
          "fr": "./src/assets/i18n/messages.fr.xlf"
        }
      },
      "architect": {
        "build": {
          "builder": "@angular-devkit/build-angular:browser",
          "options": {
            "outputPath": "dist/hammer-client",
            "index": "src/index.html",
            "main": "src/main.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "tsconfig.app.json",
            "aot": true,
            "assets": ["src/favicon.ico", "src/assets"],
            "styles": [
              "node_modules/bootstrap/dist/css/bootstrap.min.css",
              "node_modules/font-awesome/css/font-awesome.min.css",
              "src/styles.css"
            ],
            "scripts": [
              "node_modules/jquery/dist/jquery.min.js",
              "node_modules/bootstrap/dist/js/bootstrap.bundle.min.js"
            ]
          },
          "configurations": {
            "default": {
              "localize": ["en"]
            },
            "fr": {
              "localize": ["fr"],
              "aot": true,
              "outputPath": "dist/hammer-client",
              "i18nFile": "src/assets/i18n/messages.fr.xlf",
              "i18nLocale": "fr",
              "i18nFormat": "xlf",
              "i18nMissingTranslation": "warning",
              "baseHref": "/fr/"
            },
            "production": {
              "fileReplacements": [
                {
                  "replace": "src/environments/environment.ts",
                  "with": "src/environments/environment.prod.ts"
                }
              ],
              "optimization": true,
              "outputHashing": "all",
              "sourceMap": false,
              "extractCss": true,
              "namedChunks": false,
              "extractLicenses": true,
              "vendorChunk": false,
              "buildOptimizer": true,
              "localize": true,
              "budgets": [
                {
                  "type": "initial",
                  "maximumWarning": "2mb",
                  "maximumError": "5mb"
                },
                {
                  "type": "anyComponentStyle",
                  "maximumWarning": "6kb",
                  "maximumError": "10kb"
                }
              ]
            }
          }
        },
        "serve": {
          "builder": "@angular-devkit/build-angular:dev-server",
          "options": {
            "browserTarget": "hammer-client:build"
          },
          "configurations": {
            "production": {
              "browserTarget": "hammer-client:build:production"
            },
            "fr": {
              "browserTarget": "hammer-client:build:fr"
            }
          }
        },
        "extract-i18n": {
          "builder": "@angular-devkit/build-angular:extract-i18n",
          "options": {
            "browserTarget": "hammer-client:build"
          }
        },
        "test": {
          "builder": "@angular-devkit/build-angular:karma",
          "options": {
            "main": "src/test.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "tsconfig.spec.json",
            "karmaConfig": "karma.conf.js",
            "assets": ["src/favicon.ico", "src/assets"],
            "styles": ["src/styles.css"],
            "scripts": []
          }
        },
        "lint": {
          "builder": "@angular-devkit/build-angular:tslint",
          "options": {
            "tsConfig": [
              "tsconfig.app.json",
              "tsconfig.spec.json",
              "e2e/tsconfig.json"
            ],
            "exclude": ["**/node_modules/**"]
          }
        },
        "e2e": {
          "builder": "@angular-devkit/build-angular:protractor",
          "options": {
            "protractorConfig": "e2e/protractor.conf.js",
            "devServerTarget": "hammer-client:serve"
          },
          "configurations": {
            "production": {
              "devServerTarget": "hammer-client:serve:production"
            }
          }
        }
      }
    }
  },
  "defaultProject": "hammer-client"
}

我的package.json文件:

{
  "name": "hammer-client",
  "version": "0.0.3",
  "description": "Hammer game client.",
  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "start:en": "ng serve --configuration=production -- --port=4201",
    "start:fr": "ng serve --configuration=fr -- --port=4201",
    "build": "ng build --prod --localize --configuration=production,fr",
    "build:fr": "ng build --prod --localize --configuration=fr",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e",
    "i18n": "ng xi18n --output-path src/assets/i18n"
  },
  "private": true,
  "dependencies": {
    "@angular/animations": "~9.1.3",
    "@angular/common": "~9.1.3",
    "@angular/compiler": "~9.1.3",
    "@angular/core": "~9.1.3",
    "@angular/forms": "~9.1.3",
    "@angular/localize": "^9.1.3",
    "@angular/platform-browser": "~9.1.3",
    "@angular/platform-browser-dynamic": "~9.1.3",
    "@angular/router": "~9.1.3",
    "@ng-bootstrap/ng-bootstrap": "^6.1.0",
    "@ngrx/effects": "^9.1.0",
    "@ngrx/store": "^9.1.0",
    "angular-disable-browser-back-button": "^1.0.1",
    "bootstrap": "^4.4.1",
    "font-awesome": "^4.7.0",
    "jquery": "^3.5.0",
    "moment": "^2.25.3",
    "moment-timezone": "^0.5.28",
    "ng-recaptcha": "^5.0.0",
    "rxjs": "~6.5.4",
    "string": "^3.3.3",
    "tslib": "^1.10.0",
    "zone.js": "~0.10.2"
  },
  "devDependencies": {
    "@angular-devkit/build-angular": "~0.901.3",
    "@angular/cli": "~9.1.3",
    "@angular/compiler-cli": "~9.1.3",
    "@angular/language-service": "~9.1.3",
    "@types/jasmine": "~3.5.0",
    "@types/jasminewd2": "~2.0.3",
    "@types/node": "^12.11.1",
    "codelyzer": "^5.1.2",
    "jasmine-core": "~3.5.0",
    "jasmine-spec-reporter": "~4.2.1",
    "karma": "~5.0.0",
    "karma-chrome-launcher": "~3.1.0",
    "karma-coverage-istanbul-reporter": "~2.1.0",
    "karma-jasmine": "~3.0.1",
    "karma-jasmine-html-reporter": "^1.4.2",
    "protractor": "~5.4.3",
    "ts-node": "~8.3.0",
    "tslint": "~6.1.0",
    "typescript": "~3.8.3"
  },
  "files.watcherExclude": {
    "**/.git/objects/**": true,
    "**/node_modules/**": true
  }
}

我的AppModule.ts:

@NgModule({
  imports: [
    BrowserModule,
    FormsModule,
    HttpClientModule,
    ReactiveFormsModule,
    RecaptchaModule,
    RecaptchaFormsModule,
    HttpClientModule,
    appRoutingModule,
    StoreModule.forRoot(fromApp.appReducer),
    NgbModule,
    BackButtonDisableModule.forRoot({ preserveScrollPosition: true }),
  ],
  declarations: [
    AppComponent,
    HomeComponent,
    LoginComponent,
    RegisterComponent,
    AlertComponent,
    PageNotFoundComponent,
    ConfirmBoxComponent,
    TranslationComponent,
    RegisterValidComponent,
    RegisterNoValidComponent,
    ResetPasswordComponent,
    ChangePasswordComponent,
    ChangeEmailComponent,
    ChangePseudoComponent,
    UpdateAvatarComponent,
    ResetPwdComponent,
    ValidateEmailComponent,
    ConnectionFailureComponent,
  ],
  providers: [
    {
      provide: RECAPTCHA_SETTINGS,
      useValue: {
        siteKey: environment.CAPTCHA_KEY,
      } as RecaptchaSettings,
    },
    {
      provide: RECAPTCHA_LANGUAGE,
      useValue: 'en',
    },
    {
      provide: HTTP_INTERCEPTORS,
      useClass: ErrorInterceptor,
      multi: true,
    },
    ConfirmBoxService,
    CanDeactivateGuard,
  ],
  bootstrap: [AppComponent],
})
export class AppModule {}

0 个答案:

没有答案