Loading .resx file by custom string that doesn't map to a culture

1k Views Asked by At

I'm making a website for use by multiple clients and I need to offer the flexibility to change labels on various places. For example, if one client wants to call the login "Email" and another wants to call it "Employee ID." Though I've never used them, I thought that resx resource files used for localization would be a good idea, but I'm having problems setting it because what I'm doing isn't a real localization that maps to any culture.

My testing has these resource files:

Global.resx
    Hello   "Hello!"
Global.Casual.resx
    Hello   "Sup!"
Global.Formal.resx
    Hello   "Greetings!"

Then in my cshtml view, I can say @Resources.Global.Hello and that works as expected. My question is, how can I set it to display Casual by default? If I rename Casual and Formal to es and ru, I can do something like this:

Thread.CurrentThread.CurrentUICulture = CultureInfo.CreateSpecificCulture("es");

And that works just fine. But if I try that with "Casual" I get that it "is an invalid culture identifier." I can explicitly show it by saying @Resources.Global_Casual.Hello in the view, but that's obviously not what I'm after.

Am I going the complete wrong direction here, trying to use something for a purpose contrary to its design? I just want to load resource files by arbitrary string values. Do I need to roll my own database-driven approach?

This looks very similar to this SO question, which never got an answer: How to specify a custom culture?

1

There are 1 best solutions below

2
On BEST ANSWER

Personally I feel that this kind of customisation belongs in the database - not least because you can then allow the clients to customise it themselves if required. If you are concerned about performance then you can cache the terms into a dictionary and even create a class that will expose the strings either from the dictionary (if the client has customised them) or from a resource file if not. This allows you to present reasonable defaults which can be changed on demand. Alternatively you could present a number of alternatives in the database, and a flag to allow selection per term of the word to be displayed in the UI.

I had a similar problem of allowing customisation within an application in the SO question Localization vs Customization which you may find useful.