Web applications and APIs must accommodate users from various nations, cultures, and languages in today's international digital ecosystem. Multi-language (or localization) support is now necessary, regardless of whether you're developing a content management system, an e-commerce website, or a customer service portal.
This post will teach us how to use ASP.NET Core to handle multi-language content in REST APIs. It will include clear examples, an architecture methodology, and best practices for maintaining scalable, clean localization logic.
Multi-Language Content Handling: What Is It?
Your API can offer the same data in many languages depending on system configuration or user choices if it has multi-language content handling.
For instance
For example
The key goal is to separate content from language, making the system easy to scale and maintain.
Technical Workflow (Flowchart)
Approaches for Multi-Language Content
There are two common strategies to handle multi-language content in APIs:
1. Database Localization
You store content in multiple languages in the database.
Example Table
| ProductId | Language | Name | Description |
|---|---|---|---|
| 101 | en | Wireless Mouse | Ergonomic mouse design |
| 101 | fr | Souris sans fil | Souris ergonomique |
Pros
Centralized management
Easy to update via admin panel
Good for dynamic content
Cons
Slightly complex queries
Needs proper caching
2. File-Based Localization (Resource Files)
For static text (like labels, validation messages, etc.), use .resx or JSON resource files.
Example
SharedResources.en.json
SharedResources.fr.json
Step 1: Add Localization Dependencies
Add this package to your project:
Step 2: Configure Localization in Program.cs
Step 3: Create a Resource File
Create a folder named Resources and add the following files:
Controllers.HomeController.en.resxControllers.HomeController.fr.resx
HomeController.en.resx
| Name | Value |
|---|---|
| WelcomeMessage | Welcome to the API! |
HomeController.fr.resx
| Name | Value |
|---|---|
| WelcomeMessage | Bienvenue sur l'API! |
Step 4: Use Localizer in Controller
Now call the API with header:
Response:
Step 5: Multi-Language Content from Database
If your content is dynamic (e.g., product names), structure your database like this:
Sample Query
Always include a fallback mechanism if a translation doesn’t exist.
Example in C#:
Angular sends a
langparameter orAccept-Languageheader.API reads it and sends localized content.
Angular translates UI labels using ngx-translate while data (from API) comes already localized.
Angular Service Example
Cache Translations
UseIMemoryCacheor Redis for frequent translations.Batch Translations
Load all localized fields in one query.Avoid Overfetching
Only return localized fields required by the frontend.Async Resource Loading
When using.resx, preload resource dictionaries asynchronously.
| Area | Description |
|---|---|
| Content Separation | Text is not hardcoded in API responses |
| Default Language | System falls back to English or configured language |
| UTF-8 Support | Database and API both handle UTF-8 encoding |
| Dynamic Localization | API can handle runtime changes in preferred language |
| Header-Based Language | Supports Accept-Language header |
Conclusion
Implementing multi-language content handling in your ASP.NET Core REST APIs not only improves user experience but also opens your product to a global audience. With resource-based localization for static text and database-driven translations for dynamic content, you can build a flexible, scalable architecture that supports any number of languages. Combine this with caching, proper culture handling, and smart fallbacks and your API will be truly international-ready.


0 comments:
Post a Comment