Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to serve different Angular locales during development using ng serve?

I'm developing an app in two different languages (fa/en) using Angular Internationalization (i18n).

  • The target is to deploy the two different builds into sub-folders on the server (example.com/en/...)
  • These builds are different not only in translation but also styles and layout directions are different.

I can serve any of the localization (languages) like this

  "architect": {
    "build": {
      ...
      ,
      "configurations": {
      ...
        },
        "fa": {
          "localize": ["fa"],
          "baseHref": "/fa/"
        },
        "en": {
          "localize": ["en"],
          "baseHref": "/en/"
        }
      }
    },
    "serve": {
      "builder": "@angular-devkit/build-angular:dev-server",
      "options": {
        "browserTarget": "app:build"
      },
      "configurations": {
        "production": {
          "browserTarget": "app:build:production"
        },
        "en": {
          "browserTarget": "app:build:en"
        },
        "fa": {
          "browserTarget": "app:build:fa"
        }
      }
    },
    "extract-i18n": {
      "builder": "@angular-devkit/build-angular:extract-i18n",
      "options": {
        "browserTarget": "app:build"
      }
    },
    ...
  }

And then ng serve --configuration=en works and I have it on http://localhost:4200/en/... But I need to serve both languages simultaneously during development to work on the styles and the correct layout and check the translations. If I try to do this in the build configuration "localize": ["fa","en"] I get the following error.

An unhandled exception occurred: The development server only supports localizing a single locale per build

The best I got so far is to run ng serve .. multiple times on different ports to have two instances of the development server in different locales but that is kinda ugly. I am hoping for a better solution.

like image 666
azerafati Avatar asked Mar 14 '20 14:03

azerafati


People also ask

Can I use NG serve in production?

You should not use ng serve for production because it use webpack-dev-server that is build for development only.

What does ng serve do in angular?

Syntax. ng serve command builds and serve the application. It rebuilds the application if changes occur. Here project is the name of the application as defined in angular.

What is locale in angular?

Angular uses the Unicode locale identifier (Unicode locale ID) to find the correct locale data for internationalization of text strings. Unicode locale ID. A locale ID conforms to the Unicode Common Locale Data Repository (CLDR) core specification.

How does internal ng serve work?

From the docs: The CLI supports running a live browser reload experience to users by running ng serve. This will compile the application upon file saves and reload the browser with the newly compiled application. This is done by hosting the application in memory and serving it via webpack-dev-server.


Video Answer


1 Answers

In Angular 9 the development server (ng serve) can only be used with a single locale.

However, you can still serve each locale on different ports by running two separate commands:

ng serve --configuration=fa --port 4200

ng serve --configuration=en --port 4201

Hopefully, they will introduce multiple locale options for development builds in Angular 10 🤞

like image 168
Csaba Avatar answered Oct 09 '22 21:10

Csaba