This guide should work with Angular 8+.
In this post:
1. Create an angular web project.
2. Set up i18n and translate.
3. Deploy the website on Windows using IIS.
In this guide, I am using 2 languages, the setup for more than 2 languages should be similar.
You can find the code of this guide in my github.
1. Create an angular web project.
To create an angular app, in command prompt type:
ng new multiLanguageApp
Open "app.component.html" delete the default code and paste this code:
<div>
<a href="/">de</a>
<a href="/en">en</a>
</div>
<div>Danke!</div>
Run "ng serve" to serve your app. Go to localhost:4200 you should see:
I am creating an app with German as the default language and English as the second language.
"Danke" is a word in German means "Thank you". When we click "de" or "en" in the UI to switch language, we expect this word to change respectively.
2. Set up i18n and translate.
In the tag contains texts you want to translate add attribute "i18n". If there is no tag, you can use <ng-container>your text here</ng-container> to wrap your texts. You can read more about i18n attribute in Angular document but I think that's is all we need to know.
In my case I need to change <div>Danke!</div> to <div i18n>Danke!</div> and that's it!.
When you know how to do it with a word you can do it with any texts.
When you know how to do it with a word you can do it with any texts.
Now for the translation part, we need a few packages.
Add i18n toolings and tell angular that we are using "de" as the default language and we use "de" and "en". This command will also add xliffmerge to help you manage your translation files.
ng add @ngx-i18nsupport/tooling --i18nLocale=de --languages de,en
Open package.json you will see a new script is added
"extract-i18n": "ng xi18n my-app --i18n-format xlf
--output-path i18n --i18n-locale de && ng run my-app:xliffmerge"
This script is used to extract texts for translating. Notice the part --output-path i18n it let you choose the output folder for your files. I like mine to stay inside the src folder so I change it to --output-path src/i18n
ng add @angular/localize
That's it, now run npm run extract-i18n to extract text for translating.
Notice that the command creates 3 files in src/i18n.
messages.xlf is the source file you don't need to care about it.
messages.de.xlf contains texts to translate for German, but since our default language is German so we just leave it as it is.
messages.en.xlf contains texts to translate for English and this is the file we need to translate.
Open messages.en.xlf and look for <target> tag with state="new" (not yet translated), translate texts inside the tag, when you are finished change state to "final" (translated).
This is my file after translated:
<?xml version="1.0" encoding="UTF-8"?>
<xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2">
<file source-language="de" datatype="plaintext" original="ng2.template"
target-language="en">
<body>
<trans-unit id="6911867083572686626" datatype="html">
<source>Danke!</source><target state="final">Thank you!</target>
<context-group purpose="location">
<context context-type="sourcefile">src/app/app.component.html</context>
<context context-type="linenumber">1</context>
</context-group>
</trans-unit>
</body>
</file>
</xliff>
Next time if you add something new to your app, just run npm run extract-i18n again, it will automatically merge new files and old files so you just need to translate new texts. (texts inside <target> tag with state="new")
Now serve your app in English to see if it works ng s --configuration=en
Go to localhost:4200/en/ if things went well you should see:
3. Deploy the website on Windows using IIS.
If you find this part is hard to understand please watch my video at the top of this post to get better visualization.Before building our app, we need to make some changes.
Open angular.json to modify the output path and baseHref for your builds.
Open app.module.ts to add APP_BASE_HREF provider, this code tells Angular when you are browsing http://your-domain/en your <base href="/en"> otherwise your <base href="/">
import { APP_BASE_HREF } from '@angular/common';
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
AppRoutingModule
],
providers: [
{
provide: APP_BASE_HREF,
useValue: '/' + (window.location.pathname.split('/')[1] === 'en' ? 'en' : '')
},
],
bootstrap: [AppComponent]
})
export class AppModule { }
For production build use
"baseHref": "/",
"outputPath": "dist",
For en build use:
"baseHref": "/en/",
"outputPath": "dist",
Open package.json to add build script, you can build it separately but I like to run one command for all builds.
"build-all": "ng build --configuration=production && ng build --configuration=en"
Now run npm run build-all to build your app.
Copy everything in your project-folder/dist to a folder in your server or your computer if you want to test it locally.
To deploy Angular app with IIS you need to install urlrewrite in your machine and you need a file call "web.config" in your app folder.
We only have 2 languages so my web.config look like this:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<directoryBrowse enabled="true" />
<rewrite>
<rules>
<rule name="AngularRoutesEn" stopProcessing="true">
<match url="^en/" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="./en/index.html" appendQueryString="false" />
</rule>
<rule name="AngularRoutesDe" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="./index.html" appendQueryString="false" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
Now open IIS (Internet Information Services) and create a new site point to your app folder.
Open your site and try to switch language.
That's it for this post. Thank you for reading, if you find my post helpful please subscribe to my youtube channel for more guides and give me the motivation to keep going.
Comments
Post a Comment