In NET MVC project I want to change the language of my web application from English to Swahili using a dropdown menu. Once I select the language, I want the entire application to be displayed in that language.
Loading
In NET MVC project I want to change the language of my web application from English to Swahili using a dropdown menu. Once I select the language, I want the entire application to be displayed in that language.
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Jayraj ChhayaRank #304 · 8.2K reputationPosted May 8, 2024, 3:22 AM EDT
To achieve language localization in your .NET MVC project, you can utilize resource files for storing language-specific content. Here's a simplified guide to help you implement this feature:
Create Resource Files: Generate resource files for each language you want to support (e.g., English and Swahili).
Set Culture in Global.asax: In the
Application_Startmethod ofGlobal.asax, set the culture based on the selected language.Change Language: Implement a dropdown menu in your view to allow users to select the language.
Switch Language: Based on the selected language, update the culture in the controller action.
protected void Application_Start() { AreaRegistration.RegisterAllAreas(); // Set default culture CultureInfo.DefaultThreadCurrentCulture = new CultureInfo("en-US"); CultureInfo.DefaultThreadCurrentUICulture = new CultureInfo("en-US"); // Other configurations RouteConfig.RegisterRoutes(RouteTable.Routes); }Ishika TiwariRank #298 · 8.4K reputationPosted May 8, 2024, 6:41 AM EDT
Thank you, sir, for your help and time. I have a question for this
Create Resource Files: Generate resource files for each language you want to support (e.g., English and Swahili).
Do I have to create all these key-value pairs manually?
Jaydeep PatilRank #205 · 11.7K reputationPosted May 8, 2024, 12:12 PM EDT
Follow the below steps:
Step 1: Set Up Resource Files
1. Create Resource Files: Start by creating resource files (.resx) for each language you want to support. For example:
- `AppResources.resx` (default, for English)
- `AppResources.sw.resx` (for Swahili)
2. Add Key-Value Pairs: In each resource file, define key-value pairs for the strings you want to localize. For instance:
- `Greeting` with value `Hello`
- `WelcomeMessage` with value `Welcome to our website`
Step 2: Implement Language Switching
1. Create a Language Controller: Create a controller to handle language switching. For example, `LanguageController`.
2. Define Action Method: In `LanguageController`, define an action method to set the selected language in a session variable or a cookie. This method will be triggered when you change the language from the dropdown.
public class LanguageController : Controller
{
public ActionResult ChangeLanguage(string lang)
{
// Set the selected language in session or cookie
Session["CurrentCulture"] = lang;
return Redirect(Request.UrlReferrer.ToString());
}
}
Step 3: Update Controller and Views
1. Controller Base Class: Update your base controller class (e.g., `ControllerBase`) to override `ExecuteCore` method to set the thread's culture based on the session/cookie value.
public class ControllerBase : Controller
{
protected override void ExecuteCore()
{
string cultureName = Session["CurrentCulture"] as string;
cultureName = cultureName ?? "en"; // Default to English if no language is selected
Thread.CurrentThread.CurrentCulture = new CultureInfo(cultureName);
Thread.CurrentThread.CurrentUICulture = new CultureInfo(cultureName);
base.ExecuteCore();
}
}
2. Update Views: Use resource files in your views to display localized strings. Access localized strings using `@Resources.AppResources.StringKey`.
Step 4: Add Dropdown in Layout
1. Add Dropdown: In your layout (e.g., `_Layout.cshtml`), add a dropdown to select the language.
Step 5: Apply Localization
1. Use Localized Strings: Replace hard-coded strings in your views with localized strings from resource files.
@Resources.AppResources.Greeting
@Resources.AppResources.WelcomeMessage
Thanks