Ian Boyd
Ian Boyd

Reputation: 256731

How to localize App_GlobalResources in ASP.NET?

i have an ASP.NET website with some "global" resources:

\App_GlobalResources
    ContosoFrobberResource.resx

This is in addition to the "local" resources associated with each "aspx" file:

\App_LocalResources
    Materials.aspx.resx
    Notes.aspx.resx
    OrderHistory.aspx.resx

Now i want to localize the site into another locale (e.g. en). i create a resx file for each "local" resource:

\App_LocalResources
    Materials.aspx.en.resx
    Materials.aspx.en-us.resx
    Materials.aspx.en-uk.resx
    Materials.aspx.en-sg.resx
    Notes.aspx.en.resx
    Notes.aspx.en-us.resx
    Notes.aspx.en-uk.resx
    Notes.aspx.en-sg.resx
    OrderHistory.aspx.en.resx
    OrderHistory.aspx.en-us.resx
    OrderHistory.aspx.en-uk.resx
    OrderHistory.aspx.en-sg.resx

And that all works fine; the site displays tailored versions of English for

as well as a fallback for generic

The problem comes when i try to localize the resources in App_GlobalResources:

\App_GlobalResources
    ContosoFrobberResource.resx
    ContosoFrobberResource.en-us.resx

The web-site crashes with the error:

CS0101: The namespace 'Resources' already contains a definition for 'ContosoFrobberResource'

How do i localize App_GlobalResources in an ASP.NET website?

Bonus Chatter

ContosoFrobberResource.resx

 <?xml version="1.0" encoding="utf-8"?>
 <root>
    <data name="ProjectDueDate" xml:space="preserve">
       <value>Proposal Due Date</value>
    </data>
 </root>

 

ContosoFrobberResource.qps.resx

 <?xml version="1.0" encoding="utf-8"?>
 <root>
    <data name="ProjectDueDate" xml:space="preserve">
       <value>Prȍposẳl Duɇ Dãtē</value>
    </data>
 </root>

Upvotes: 4

Views: 1941

Answers (1)

Ian Boyd
Ian Boyd

Reputation: 256731

Found the answer.

ASP.NET doesn't fallback properly to the parent language. If it cannot recognize the file as a "fallback" locale then it fails to parse it, and shows the misleading error.

e.g.

ContosoGrobber.resx
ContosoGrobber.en-us.resx

works fine. But:

ContosoGrobber.resx
ContosoGrobber.en.resx

fails when the "en" portion of the locale string isn't recognized. Changing it to use the full locale name, and avoid the buggy fallback code is the fix. e.g.:

ContosoGrobber.qps.resx

should become:

ContosoGrobber.qps-ploc.resx

In other words: if a browser is requesting locale qps-ploc, ideally you could use

ContosoGrobber.qps.resx

and the local qps-ploc will fallback to qps. But because of the bug in ASP.NET it doesn't fallback correctly. This means you cannot let it fallback; you have to actually handle all possible locales:

ContosoGrobber.qps-ploc.resx
ContosoGrobber.qps-plocm.resx
ContosoGrobber.qps-ploca.resx

Upvotes: 4

Related Questions