.NET Daily

Strongly Typed Local Resources

Introduction

Darius

Darius

Darius Dumitrescu is a creative Senior CMS Consultant with in depth .NET knowledge, focused on Web Development and Architecture Design.


LATEST POSTS

My View of a JIRA Workflow for Web Development 17th April, 2018

How To Solve: SQL Server detected a logical consistency-based I/O error: invalid protection option. 19th March, 2018

ASP.net

Strongly Typed Local Resources

Posted on .

Unlike Global Resources, Local Resources items from ASP.NET can be accessed only with GetLocalResouceObject method which can take as parameter a string key of the resource item.

So, the default functionality does not support strongly typed access to the resource items.

Here are the steps to obtain strongly typed local resources:

  1. Add the App_LocalResource folder to your solution;
  2. Add a new resource file to App_LocalResource folder;
  3. Add some keys to the resource file;
  4. Change the Access Modifier of the resource file to public;
    strongly typed local resources - step 4

    strongly typed local resources – step 4

     

  5. Right click on the resource file, then select Properties and from the Build Action dropdown select Embedded Resource;
    strongly typed local resources - step5

    strongly typed local resources – step5

     

After you have followed the above steps you should be able to access the local resource items like this:

Don’t worry about the resource files that contain the language codes in their names. It will automatically take the file according to the defined culture in your code.

You can download a demo application with strongly typed local resources from below.

Attachments

Darius

Darius

Darius Dumitrescu is a creative Senior CMS Consultant with in depth .NET knowledge, focused on Web Development and Architecture Design.

Comments
  • user

    AUTHOR Mizu

    Posted on 11:35 am December 5, 2011.
    Reply

    Thanks very much for posting this! It’s good to have strong typing on an app_localresources RESX.

  • user

    AUTHOR Hans

    Posted on 2:04 pm April 20, 2012.
    Reply

    Very nice 🙂 It seems like very few people know about this..
    (If you are not using ASP.NET (i.e. WinForms or WPF) put your Resources.resx in the folder ‘Properties’ instead and reference your LocalisedVariable like:
    button1.Content = Properties.Resources.myLocalisedButtonString;

  • user

    AUTHOR Scott

    Posted on 8:21 pm August 29, 2014.
    Reply

    Great solution! One issue with this – automatically generated resource keys, from using the Design mode’s Tools/Generate Local Resources, causes warnings in the resource file. Everything “works” it seems, but the message “The Resource Name is not a valid identifier” because they contain dots. LIke lblResourceTest.Text.

    The proxy class generated uses underscores instead. Is there a way to make the automatically generated resx file use underscores instead of dots, when auto-creating key names?

  • View Comments (0) ...
    Navigation