Reputation: 13
I'm working on a website project and I need to internationalize the project. Now, I use resources files.
I have already internationalized almos everything, but one thing I came up wich I would like to know how to do is this:
Suppose I have a serie of classes that are already on my db, each class has a string property named Description. Is there a way to display the Description based on the locale browser? I mean, I use the browser in english and display the descriptions in english, but I change it to french and display those descriptions in french? (I thought of assign each Description a "numeric" value, and then in the resources file have 1 : Human Resources, 2 : IT, 3 : HealthCare, and so on. And another file for french with their respective values, but I think it's a kind of messed up)
Thank you very much
Upvotes: 0
Views: 711
Reputation: 1862
Well, given that the description texts are constants, couldn't you write the translation key for the respective text into your DB and then get the matching (localized) value from your translation file? If you can't change the DB, maybe simply use the existing values from the DB as keys in a translation file and map to the respective translation. You might have to get rid of spaces etc., but that shouldn't be a big deal.
In your DB:
Human Resources
Health Care
...
Translation file example:
English:
Human_Resources | Human Resources
Health_Care | Health Care
...
French:
Human_Resources | Service du personnel
...
Retrieving the translated value (pseudo):
var translationKey = descriptionValueFromDB.Replace(' ', '_');
var translatedDescription = ResourceManager.GetString(translationKey);
What do you think?
Cheers, Alex
UPDATE: Displaying the translated string within a view is straightforward using standard techniques. I am assuming that you are using a model object to provide the data to your view. Then you could for instance write the ready-made translation into your model and use it within the view:
public class FooModel
{
public string Description {get; set;}
public string TranslatedDescription
{
get { return Description.ToTranslationKey().Translate(); }
}
}
public static class TranslationExtensions
{
public static string ToTranslationKey(this string term)
{
if (string.IsNullOrEmpty(term))
return term;
return term.Replace(' ', '_');
}
public static string Translate(this string term)
{
if (string.IsNullOrEmpty(term))
return term;
return ResourceManager.GetString(term);
}
}
<div id="fooDescription">@Model.TranslatedDescription</div>
Of course, you could also move the translation part into the view, if you like that better:
<div id="fooDescription">@Model.Description.ToTranslationKey().Translate()</div>
Mkay?
Upvotes: 1