An open API service indexing awesome lists of open source software.

https://github.com/kardelruveyda/dotnet-localization-example

A simple ASP.NET Core MVC example demonstrating how to implement multilingual support using IStringLocalizer and IViewLocalizer with .resx resource files.
https://github.com/kardelruveyda/dotnet-localization-example

dotnet dotnetcore localization mvc

Last synced: 11 months ago
JSON representation

A simple ASP.NET Core MVC example demonstrating how to implement multilingual support using IStringLocalizer and IViewLocalizer with .resx resource files.

Awesome Lists containing this project

README

          

# 🌍 MultiLangDemo - ASP.NET Core Localization Example

MultiLangDemo is a sample ASP.NET Core MVC project that demonstrates how to implement **multi-language support** using `IStringLocalizer` and `IViewLocalizer`.

Users can view the site in their preferred language based on browser settings or a query string.

---

## πŸ“ Project Structure

```
MultiLangDemo/
β”œβ”€β”€ Controllers/
β”‚ └── HomeController.cs
β”œβ”€β”€ Views/
β”‚ └── Home/
β”‚ └── Index.cshtml
β”œβ”€β”€ Resources/
β”‚ └── Views/
β”‚ └── Home/
β”‚ β”œβ”€β”€ Index.en-US.resx
β”‚ β”œβ”€β”€ Index.tr-TR.resx
β”‚ └── Index.de-DE.resx
β”œβ”€β”€ Program.cs
β”œβ”€β”€ MultiLangDemo.csproj
└── README.md
```

---

## πŸš€ Getting Started

### βœ… Requirements

- [.NET 9 SDK](https://dotnet.microsoft.com/download)
- Visual Studio Code or Visual Studio 2022+
- Git (optional)

### πŸ”§ Setup Instructions

```bash
git clone https://github.com/yourusername/MultiLangDemo.git
cd MultiLangDemo
dotnet restore
dotnet run
```

The application will launch at:
πŸ‘‰ https://localhost:5001

---

## 🌐 Testing Localization

You can switch the UI language by appending `?ui-culture=xx-XX` to the URL:

| Language | URL Example |
|----------|--------------------------------------------------|
| πŸ‡ΊπŸ‡Έ English | `https://localhost:5001/?ui-culture=en-US` |
| πŸ‡ΉπŸ‡· Turkish | `https://localhost:5001/?ui-culture=tr-TR` |
| πŸ‡©πŸ‡ͺ German | `https://localhost:5001/?ui-culture=de-DE` |

---

## 🧩 Key Concepts

- `IViewLocalizer`: Used in Razor Views to localize UI content.
- `IStringLocalizer`: Used in controllers or services to retrieve localized strings.
- `.resx` Files: Resource files that store key-value pairs for each supported culture.

---

## πŸ’‘ Sample Output

When visiting `https://localhost:5001/?ui-culture=tr-TR`:

```
Hoş geldiniz!
Aktif dil: tr-TR
```

When visiting `https://localhost:5001/?ui-culture=en-US`:

```
Welcome to our site!
Active culture: en-US
```

---

## βž• Adding a New Language

To add support for French:

1. Create a new resource file:
`Resources/Views/Home/Index.fr-FR.resx`

2. Add translation keys and values:

| Name | Value |
|-----------------|----------------------------|
| WelcomeMessage | Bienvenue sur notre site |

3. Add `fr-FR` to the supported cultures list in `Program.cs`.

---

## πŸ”’ Security Note

If you encounter a warning like:

```
Package 'System.Text.RegularExpressions' 4.3.0 has a known high severity vulnerability
```

You can upgrade the package using:

```bash
dotnet add package System.Text.RegularExpressions --version 4.3.1
```

---

## πŸ“„ License

This project is licensed under the MIT License.