Explicit Localization

I use explicit localization on my pages, for example:

<asp:Literal ID="appTitle" runat="server" Text="<%$ Resources:TranslationResource, AppTitle %>"></asp:Literal></div> 

If the AppTitle resource does not exist, is there a way to avoid the "parser error" and show an empty string or resource name?

Thanks in advance.

+4
source share
1 answer

You can use GetGlobalResourceObject , which does the same thing, but returns null, and does not throw an exception if the key does not exist.

 <div><%= GetGlobalResourceObject("TranslationResource", "AppTitle") %></div> 

You can easily add a fallback value using the zero-coalescing operator:

 <div><%= GetGlobalResourceObject("TranslationResource", "AppTitle") ?? "No translation resource" %></div> 

If you need to modify the behavior of the localization provider in a centralized way (without changing the syntax used on the pages), you can override resourceProviderFactoryType . Add this type to the globalization section of web.config ...

 <globalization resourceProviderFactoryType="MyLocalizationProvider"/> 

And create MyLocalizationProvider by subclassing ResourceProviderFactory .

+4
source

Source: https://habr.com/ru/post/1435126/


All Articles