Search code examples
asp.netlocalizationculture

Override culture when using GetGlobalResourceObject or GetLocalResourceObject in ASP.NET


I've implemented localization in this ASP.NET project using resource files and that's working as expected. But there is one situation where I have to display the whole page in one language and then a section of it in some other language based on the selection from a dropdown list.

From what I can see, the GetGlobalResourceObject() and GetLocalResourceObject() methods work from the page culture, but I need to adjust the language programmatically. Is there a way to override those methods' behaviour so that I can specify the language?


Solution

  • I believe you can access a specific resx file by using this method:

       value = HttpContext.GetGlobalResourceObject(classKey, resourceKey, culture)
    

    You can initialize your culture like this:

    Culture culture = CultureInfo.CreateSpecificCulture("pt-PT");
    

    Just pick up your value from the dropdown and switch the culture in the CreateSpecificCulture method.

    I havent tested it, let me know if that doesnt work.

    EDIT: its the same for GetLocalResourceObject