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.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Jaydeep PatilPosted May 8, 2024, 4:12 PM
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
Ishika TiwariPosted May 8, 2024, 10:41 AM
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?
Jayraj ChhayaPosted May 8, 2024, 7:22 AM
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.