Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 9 Internationalization - Requested locale Error

I am creating a new app with the new way of internationalization in Angular 9. When I'm running command ng serve --configuration=en I'm getting following error -

An unhandled exception occurred: Requested locale 'en' is not defined for the project.

I have verified the xlf file location. The locale files are present. The application runs fine without configuration parameters. My project's Angular.json -

{
  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "version": 1,
  "newProjectRoot": "projects",
  "projects": {
    "khelFever2": {
      "projectType": "application",
      "schematics": {
        "@schematics/angular:component": {
          "style": "scss"
        },
        "i18n": {
          "locales": {
            "en": "src/locale/messages.en.xlf",
            "hi": "src/locale/messages.hi.xlf"
          }
        }
      },
      "root": "",
      "sourceRoot": "src",
      "prefix": "app",
      "architect": {
        "build": {
          "builder": "@angular-devkit/build-angular:browser",
          "options": {
            "outputPath": "dist/khelFever2",
            "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.scss",
              "node_modules/bootstrap/dist/css/bootstrap.min.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"
                }
              ]
            },
            "en": {
              "localize": ["en"]
            },
            "hi": {
              "localize": ["hi"]
            }
          }
        },
        "serve": {
          "builder": "@angular-devkit/build-angular:dev-server",
          "options": {
            "browserTarget": "khelFever2:build"
          },
          "configurations": {
            "production": {
              "browserTarget": "khelFever2:build:production"
            },
            "en": {
              "browserTarget": "khelFever2:build:en"
            },
            "hi": {
              "browserTarget": "khelFever2:build:hi"
            }
          }
        },
        "extract-i18n": {
          "builder": "@angular-devkit/build-angular:extract-i18n",
          "options": {
            "browserTarget": "khelFever2: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.scss",
              "node_modules/bootstrap/dist/css/bootstrap.min.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": "khelFever2:serve"
          },
          "configurations": {
            "production": {
              "devServerTarget": "khelFever2:serve:production"
            }
          }
        }
      }
    }
  },
  "defaultProject": "khelFever2",
  "cli": {
    "analytics": false
  }
}
like image 685
Avi Avatar asked Jan 25 '23 01:01

Avi


2 Answers

This error is because you've included i18ndata inside a schematics node. You need to move it up one level

Try with

"projects": {
 "khelFever2": {
  "projectType": "application",
    "i18n": {
      "locales": {
        "en": "src/locale/messages.en.xlf",
        "hi": "src/locale/messages.hi.xlf"
      }
    }
  "schematics": {
    "@schematics/angular:component": {
      "style": "scss"
    }
  },
  "root": "",
  "sourceRoot": "src",
  "prefix": "app",
like image 174
David Avatar answered Jan 31 '23 07:01

David


When you run ng serve --configuration=en, the target that actually runs is this one:

    "extract-i18n": {
      "builder": "@angular-devkit/build-angular:extract-i18n",
      "options": {
        "browserTarget": "rubywebclient:build"
      }
    },

There you indeed have no configuration named en. You can add it there and save the options you want to use:

    "extract-i18n": {
      "builder": "@angular-devkit/build-angular:extract-i18n",
      "options": {
        "browserTarget": "rubywebclient:build"
      },
      "configurations" : {
        "en": {
           //your configuration 
          "outputPath": "locale/",
          "outFile": "messages.en.untranslated.xlf",
          "i18nFormat": "xlf",
          "i18nLocale": "en"
        }
      }
    },

Then running ng serve --configuration=en will use those options.

like image 24
Arun Kumar Avatar answered Jan 31 '23 08:01

Arun Kumar