Chris
Chris

Reputation: 28064

How to detect if a language is supported in ASP.NET MVC localization

I'm using a custom route in my ASP.NET MVC app (/lang/{culture}/{controller}/{action}/{id}) and in my base controller, I'm detecting the presence of a value for the culture parameter and setting the current thread culture. However, if someone manages to navigate to a language that doesn't have any resources, rather than silently falling back to the default culture, I want to present a message indicating the lack of language support.

How can I detect if the current culture has no resources specified rather than silently falling back to the default?

Upvotes: 1

Views: 522

Answers (2)

Clafou
Clafou

Reputation: 15400

The issue is that in practice it's complicated to programmatically determine whether you have localizations for a given culture. You may have satellite assemblies for a given culture for one assembly, but not for another. And worse, one satellite assembly may contain translations for some resources, but not all. This may be intentional: for en-GB you may prefer to only take an "override rather than duplicate" approach and define only a few resources here and there, only where you differ from your base English.

So the simplest and clearest approach may be to maintain a hard-coded master list of supported cultures in your application code, and then just base your language support alerting logic on this list. When you add a new localization, you'll need update that list in code. But most of us do localization by checking-in new resource files into the solution anyway--there already isn't a complete separation between code and localized resources.

Upvotes: 1

Andy T
Andy T

Reputation: 9881

There doesn't seem to be a direct API to give you the list of available cultures from the resource files.

One possible solution is to enumerate the resource files and parse out the language code.

Take a look at this SO question: Get available languages from resource

Upvotes: 0

Related Questions