0

I am creating an Angular App that support different language by using i18n. The app working well with English language. However, I tried to check Spanish language using the following CLI command:

ng serve --configuration=es

The unhandled error appears. The modified versoin of The Angular.json shown below:

{
  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "version": 1,
  "newProjectRoot": "projects",
  "projects": {
    "i18nDemo": {
      "projectType": "application",
      "schematics": {},
      "root": "",
      "sourceRoot": "src",
      "prefix": "app",
      "architect": {
        "build": {
          "builder": "@angular-devkit/build-angular:browser",
          "options": {
            "outputPath": "dist/i18nDemo",
            "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": [
              "src/styles.css"
            ],
            "scripts": []
          },
          "configurations": {
            "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,
              "budgets": [
                {
                  "type": "initial",
                  "maximumWarning": "2mb",
                  "maximumError": "5mb"
                },
                {
                  "type": "anyComponentStyle",
                  "maximumWarning": "6kb",
                  "maximumError": "10kb"
                }
              ],
              "es": { 
                "aot": true, 
                "i18nFile": "src/translate/messages.es.xlf", 
                "i18nFormat": "xlf",
                "i18nLocale": "es", 
                "i18nMissingTranslation": "error" }
            }
          }
        
        },
        "serve": {
          "builder": "@angular-devkit/build-angular:dev-server",
          "options": {
            "browserTarget": "i18nDemo:build"
          },
          "configurations": {
            "production": {
              "browserTarget": "i18nDemo:build:production"
            },
            "es": {
              "browserTarget": "i18nDemo:build:es"
             }
          }
        },
        "extract-i18n": {
          "builder": "@angular-devkit/build-angular:extract-i18n",
          "options": {
            "browserTarget": "i18nDemo: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": "i18nDemo:serve"
          },
          "configurations": {
            "production": {
              "devServerTarget": "i18nDemo:serve:production"
            }
          }
        }
      }
    }
  },
  "defaultProject": "i18nDemo",
  "cli": {
    "analytics": false
  }
}

Could you please help to find the configuration issue in the file.

s.alhaj
  • 141
  • 2
  • 11
  • Possible duplicate of this ? https://stackoverflow.com/questions/58876170/angular-configuration-is-not-set-in-the-workspace – Mambo Sep 18 '20 at 10:06

1 Answers1

0

You need to use the 18n and then locales to specify the translation locales. See also Define locales in the build configuration

  "projects": {
    "i18nDemo": {
      "i18n": {
        "sourceLocale": {
          "code": "en"
        },
        "locales": {
          "es": {
            "translation": "src/translate/messages.es.xlf"
          }
        }
      }

That way all your translations will be compiled when you use the cli command

ng build --localize

As far as the configuration the issue is that you have the configuration "es" nested inside the "production" configuration instead of under it. That is a simple matter of moving "es" to outside of "production". I do not see any reason to have a configuration section named "es" in this case though given what you are trying to set. See also Apply specific build options for just one locale.

Igor
  • 60,821
  • 10
  • 100
  • 175