GICT knowledge base
How to Make Your Angular Website Work in Many Languages
Published
Making websites that work in different languages is important today. Your website visitors come from all over the world. Some speak English, others speak Spanish, French, or Hindi.
When you build a website that shows content in the visitor’s language, more people will use it. This is called making your website international. Angular has tools built in to help you do this easily.
Think about popular websites like YouTube or Netflix. They show different languages to different users. Your website can do the same thing without much work.
Benefits of Supporting Multiple Languages
There are many good reasons to add language support to your website.
First, you reach more customers. When people see your website in their native language, they feel more comfortable buying from you or using your service.
Second, your business looks more professional. Companies that support multiple languages seem bigger and more trustworthy to customers.
Third, you get better search engine results in different countries. Google shows websites in local languages higher in search results for those countries.
Fourth, you can compete with bigger companies. Even small businesses can look global when they support multiple languages.
What You Need to Get Started
Before we begin building, make sure your computer has the right tools.
You need Node.js installed on your computer. You also need Angular CLI which helps create and manage Angular projects. If you do not have these, download them from their official websites.
You should understand basic HTML and how Angular components work. You do not need to be an expert but knowing the basics helps.
Create a new project or use an existing one. We will use this project to add language support.
If you want to create a new project, open your terminal and type these commands.
ng new language-demo
cd language-demo
This creates a new Angular project called language-demo and moves you into that folder.
Adding Language Support to Your Project
Angular comes with language tools but they are not turned on by default. You need to add them to your project first.
Open your terminal in your project folder and run this command.
ng add @angular/localize
This command downloads and installs everything needed for multiple languages. It also changes some files in your project automatically.
The command adds new imports to your main module file. It also updates your package.json file with new dependencies. These are libraries Angular needs to handle different languages.
After running this command, your project is ready to support multiple languages.
Marking Text That Needs Translation
Now comes the important part. You need to tell Angular which text should be translated into other languages.
You do this by adding a special attribute called i18n to your HTML elements. This attribute tells Angular that the text inside should be translated.
Here is how you mark different types of text.
For headings and paragraphs:
<h1 i18n="header-welcome">Welcome to Our Store</h1>
<p i18n="description-main">We offer the best products at great prices</p>
For buttons and links:
<button i18n="button-login">Sign In</button>
<a href="/contact" i18n="link-contact">Contact Us</a>
For form labels and placeholders:
<label i18n="label-email">Email Address</label>
<input placeholder="Enter your email" i18n-placeholder="placeholder-email">
Each i18n attribute has a unique name. These names help organize your translations. Use clear names that describe what the text does.
Make sure each name is different. If you use the same name twice, Angular will show an error when you build your project.
Working with Dynamic Content
Sometimes your text includes information that changes, like usernames or product counts. Angular can handle this type of content too.
For text with variables:
<p i18n="greeting-user">Hello {userName}, welcome back!</p>
<p i18n="cart-items">You have {itemCount} products in your shopping cart</p>
The curly braces show where the changing information goes. Angular replaces these with real values when your website runs.
For counting items with different rules:
<span i18n="product-count">{count, plural, =0 {no products} =1 {one product} other {{count} products}}</span>
This handles different counting rules. Some languages have special rules for zero, one, or many items.
You can also format dates and money:
<p i18n="order-date">Order placed on {orderDate, date, short}</p>
<p i18n="total-price">Total amount: {totalPrice, currency}</p>
Extracting Text for Translators
After marking all your text, you need to create files that translators can work with.
Run this command in your terminal:
ng extract-i18n
This command looks through all your HTML files and finds every piece of text marked with i18n. It creates a file called messages.xlf that contains all this text.
The file is in XML format which looks complicated but translators only need to work with specific parts of it.
This file becomes the starting point for all your translations. Every time you add new text or change existing text, run this command again to update the file.
Creating Translation Files
Now you need to create separate files for each language you want to support.
Copy the messages.xlf file and rename the copies for each language. Use standard language codes like:
- messages.es.xlf for Spanish
- messages.fr.xlf for French
- messages.de.xlf for German
- messages.hi.xlf for Hindi
- messages.ar.xlf for Arabic
Put these files in a new folder called locale inside your src folder. This keeps your translation files organized.
Open each file in a text editor. Find the target tags and add translations between them.
The English version looks like this:
<target>Welcome to Our Store</target>
Change it to Spanish:
<target>Bienvenido a Nuestra Tienda</target>
Change it to Hindi:
<target>हमारे स्टोर में आपका स्वागत है</target>
Do this for every piece of text in each language file.
Setting Up Build Configuration
Angular needs to know about your new language files. Open the angular.json file in your project root.
Find the build section and add configurations for each language:
"build": {
"configurations": {
"production": {
// existing production config
},
"es": {
"aot": true,
"outputPath": "dist/es/",
"i18nFile": "src/locale/messages.es.xlf",
"i18nFormat": "xlf",
"i18nLocale": "es",
"i18nMissingTranslation": "error"
},
"fr": {
"aot": true,
"outputPath": "dist/fr/",
"i18nFile": "src/locale/messages.fr.xlf",
"i18nFormat": "xlf",
"i18nLocale": "fr",
"i18nMissingTranslation": "error"
}
}
}
Also add serve configurations for testing:
"serve": {
"configurations": {
"es": {
"browserTarget": "language-demo:build:es"
},
"fr": {
"browserTarget": "language-demo:build:fr"
}
}
}
This tells Angular where to find translation files and where to put the finished websites.
Building Your Multi Language Website
Now you can create different versions of your website for each language.
To build the Spanish version:
ng build --configuration=es
To build the French version:
ng build --configuration=fr
To build the default English version:
ng build
Each command creates a complete website in the specified language. The Spanish version goes in the dist/es folder. The French version goes in dist/fr.
These folders contain everything needed to run your website in that language.
Testing Your Translations Locally
You can test your translated websites on your computer before putting them online.
To test the Spanish version:
ng serve --configuration=es
To test the French version:
ng serve --configuration=fr
Open your web browser and go to localhost:4200. You will see your website in the selected language.
Stop the server with Ctrl+C and start it with a different language configuration to test other languages.
Real Example: Building a Shopping Website
Let me show you how this works with a real example. We will build a simple shopping website that works in English, Spanish, and Hindi.
First, create the main page HTML:
<div class="container">
<header>
<h1 i18n="site-title">Best Electronics Store</h1>
<nav>
<a href="/" i18n="nav-home">Home</a>
<a href="/products" i18n="nav-products">Products</a>
<a href="/contact" i18n="nav-contact">Contact</a>
</nav>
</header>
<main>
<section class="hero">
<h2 i18n="hero-title">Find Amazing Deals on Electronics</h2>
<p i18n="hero-description">We have the latest phones, laptops, and gadgets at unbeatable prices</p>
<button i18n="hero-button">Shop Now</button>
</section>
<section class="products">
<h3 i18n="products-title">Featured Products</h3>
<div class="product-card">
<img src="laptop.jpg" alt="Laptop">
<h4 i18n="product-laptop">Gaming Laptop</h4>
<p i18n="product-laptop-desc">High performance laptop for gaming and work</p>
<span class="price" i18n="product-laptop-price">Price: 75000 rupees</span>
<button i18n="button-add-cart">Add to Cart</button>
</div>
<div class="product-card">
<img src="phone.jpg" alt="Phone">
<h4 i18n="product-phone">Smartphone</h4>
<p i18n="product-phone-desc">Latest smartphone with amazing camera</p>
<span class="price" i18n="product-phone-price">Price: 25000 rupees</span>
<button i18n="button-add-cart">Add to Cart</button>
</div>
</section>
<section class="cart">
<h3 i18n="cart-title">Shopping Cart</h3>
<p i18n="cart-count">Items in cart: {cartItems}</p>
<p i18n="cart-total">Total amount: {cartTotal} rupees</p>
<button i18n="button-checkout">Proceed to Checkout</button>
</section>
</main>
<footer>
<p i18n="footer-copy">Copyright 2024 Best Electronics Store</p>
<p i18n="footer-contact">Email: [email protected] | Phone: 1234567890</p>
</footer>
</div>
The component TypeScript file:
export class ShopComponent {
cartItems = 3;
cartTotal = 125000;
}
After running the extract command, you get a messages.xlf file with all the text.
For the Spanish translation file (messages.es.xlf):
<trans-unit id="site-title">
<source>Best Electronics Store</source>
<target>Mejor Tienda de Electrónicos</target>
</trans-unit>
<trans-unit id="nav-home">
<source>Home</source>
<target>Inicio</target>
</trans-unit>
<trans-unit id="hero-title">
<source>Find Amazing Deals on Electronics</source>
<target>Encuentra Ofertas Increíbles en Electrónicos</target>
</trans-unit>
<trans-unit id="button-add-cart">
<source>Add to Cart</source>
<target>Agregar al Carrito</target>
</trans-unit>
For the Hindi translation file (messages.hi.xlf):
<trans-unit id="site-title">
<source>Best Electronics Store</source>
<target>सर्वश्रेष्ठ इलेक्ट्रॉनिक्स स्टोर</target>
</trans-unit>
<trans-unit id="nav-home">
<source>Home</source>
<target>होम</target>
</trans-unit>
<trans-unit id="hero-title">
<source>Find Amazing Deals on Electronics</source>
<target>इलेक्ट्रॉनिक्स पर शानदार डील्स पाएं</target>
</trans-unit>
<trans-unit id="button-add-cart">
<source>Add to Cart</source>
<target>कार्ट में डालें</target>
</trans-unit>
What Users See in Different Languages
When English users visit your website, they see:
- Best Electronics Store
- Home Products Contact
- Find Amazing Deals on Electronics
- Add to Cart
- Shopping Cart
When Spanish users visit, they see:
- Mejor Tienda de Electrónicos
- Inicio Productos Contacto
- Encuentra Ofertas Increíbles en Electrónicos
- Agregar al Carrito
- Carrito de Compras
When Hindi users visit, they see:
- सर्वश्रेष्ठ इलेक्ट्रॉनिक्स स्टोर
- होम प्रोडक्ट्स संपर्क
- इलेक्ट्रॉनिक्स पर शानदार डील्स पाएं
- कार्ट में डालें
- शॉपिंग कार्ट
Handling Numbers and Dates
Angular automatically formats numbers, dates, and currency based on the selected language and region.
For dates:
<p>Today is {currentDate | date}</p>
For currency:
<p>Price: {productPrice | currency:'INR':'symbol':'1.0-0'}</p>
For numbers:
<p>Discount: {discountPercent | percent}</p>
Different languages show these in their local format automatically.
Common Issues and Solutions
Sometimes things do not work as expected. Here are common problems and how to fix them.
If text does not change after adding translations, check that your target tags are filled in correctly. Empty target tags will show the original English text.
If your build fails with translation errors, make sure all your i18n IDs are unique. You cannot use the same ID in multiple places.
If some text shows in English instead of the translated language, verify that your translation files are in the correct location and have the right names.
If your website does not load after building, check your angular.json configuration. Make sure the file paths are correct.
If special characters do not display correctly, ensure your HTML files use UTF-8 encoding.
Best Practices for Translation
Follow these guidelines to create better translations and avoid problems.
Use descriptive names for your i18n IDs. Instead of id1 or text1, use names like welcome-message or login-button. This makes it easier to understand what each piece of text does.
Keep your original text clear and simple. Avoid slang, idioms, or cultural references that might not translate well.
Consider text length differences. German text is often longer than English. Arabic and Hebrew read right to left. Plan your layout to handle these differences.
Test your website in all supported languages before going live. Check that text fits properly and looks good in each language.
Get native speakers to review your translations. Automatic translation tools are helpful for starting but human review improves quality significantly.
Update all translation files when you change text. If you modify English text, remember to update the corresponding translations.
Organizing Translation Work
For larger projects, you need good organization to manage multiple languages efficiently.
Create a spreadsheet listing all your i18n IDs and their meanings. This helps translators understand context.
Consider using translation management services like Crowdin or Lokalise. These tools help coordinate translation work and track progress.
Set up a review process where native speakers check translations before they go live on your website.
Plan for ongoing maintenance. As you add new features, you will need to translate new text.
Document your translation workflow so team members know how to add new languages or update existing ones.
Deploying Multi Language Websites
When you put your website online, you have several options for serving different languages.
The simplest approach is to deploy each language version to a separate folder on your web server. Put the Spanish version in /es/ and the French version in /fr/.
You can also use automatic language detection based on the user’s browser settings. Most web servers can redirect users to the appropriate language version.
Content delivery networks like Cloudflare can handle this automatically based on user location.
Some hosting platforms like Netlify and Vercel have built-in support for multi-language websites with automatic redirects.
Growing Your Multi Language Support
Start with one additional language that many of your users speak. Test the complete process with this language before adding more.
Research which languages your users prefer by checking your website analytics or surveying customers.
Consider regional differences within languages. Spanish speakers in Mexico use different terms than Spanish speakers in Spain.
Plan for right-to-left languages like Arabic and Hebrew. These require additional CSS changes for proper layout.
Think about local cultural preferences beyond just language. Colors, images, and design elements might need adjustment for different cultures.
Monitor which language versions get the most use and focus your translation efforts accordingly.
Measuring Success
Track how well your multi-language support works by monitoring key metrics.
Check website traffic by language to see which versions are most popular.
Monitor conversion rates to see if users in different languages are more likely to make purchases or sign up.
Look at user engagement metrics like time spent on site and pages viewed per session.
Survey users to get feedback about translation quality and user experience.
Use this data to improve your translations and decide which languages to prioritize.
Conclusion
Adding multi-language support to your Angular website opens doors to global audiences. The process requires planning and work but Angular’s built-in tools make it manageable.
Start by marking your text for translation, extract it into files, get it translated, and configure your build process. Test thoroughly before deploying.
Remember that good internationalization goes beyond just translating words. Consider cultural differences, text length variations, and local preferences.
The investment in multi-language support pays off through increased user engagement, better search engine visibility, and access to new markets.
Begin with one additional language and expand gradually as you gain experience and see results. Your international users will appreciate the effort you put into speaking their language.
Related guides
Get Started with Ionic Angular – Build Your First App in Minutes
Build stunning mobile apps with Ionic Angular! Step-by-step tutorial with examples, commands, and best practices for beginners. Start coding today!
Angular.jsHow to Build Web Apps with Angular Material – Step by Step Guide
Learn Angular Material from scratch with this complete step-by-step guide. Build beautiful web apps using Google's Material Design components.
Angular.jsNew Angular 20 Features and Easy Upgrade Guide
Learn about Angular 20 new features like stable signals, zoneless apps, and better templates.
