Hello Arne,
Yes, I know you did helped me on a similar question.
At that moment I had a different approach ... Maybe wrong.
To be honest I didn't completely understand your suggestions.
Let me explain what would be the ideal in my head:
KEY = HELLO > "Hello", "en" ; "Olá", "pt" ; "Bounjour", "fr"
KEY = BYE > "Bye", "en" ; "Adeus", "pt" ; "Adieu", "fr"
Then I would have a Translator class with the following methods:
- TranslateKey(String key, String fromCulture, String toCulture);
TranslateKey("BYE", "en", "pt"); // > Adeus
- TranslateValue(String value, String fromCulture, String toCulture);
TranslateValue("Hello", "pt", "fr"); // > Adieu
NOTE: If possible also have two methods that accepts a IEnumerable<String> Keys or a IEnumerable<String> Values ... These methods would be the same asthe one before but to N values. The fromCulture and toCulture would be thesame for all.
I am looking for the following:
1 - Translator must implement ITranslator
This is because I will inject ITranslator in parts of my application;
2 - I need a configuration class such as:
public static class TranslatorConfiguration {
public static IList<TranslatorProviderBase> Providers { get; set; }
public static String DefaultFromCulture { get; set; }
public static String DefaultToCulture { get; set; }
// ...
}
A) Providers are classes that contains the translations.
I could separate some translations in 2 or 3 files.
Translator would go into these providers for search.
It would be just a table partition ...
B) DefaultFromCulture would be for example "en".
Then I would have a methods such as:
TranslateValue(String value, String toCulture);
C) DefaultToCulture would be set for example as follows:
I would set this culture maybe as follows:
SetDefaultToCulture(() => Thread.CurrentThread.CurrentCulture);
Now I could would have methods such as:
TranslateValue(String value, String toCulture)
And I can add a few more like "AllowedCultures", etc.
In App_Start I would set the configuration with all these values.
Then I would inject ITranslator and use those methods.
I hope I gave an idea on how I am trying this to "work".
I read your answers but when you suggest something like:
public class Translation
{
private Dictionary<string, Dictionary<int, string>> forward;
private Dictionary<string, Dictionary<string, int>> backward;
...
}
I am completely lost on how to integrate this in what I am trying to do.
I can post the code I have at the moment:
(NOTE: I am using List<Dictionary<String, String>> because at the moment I didn't add the Keys yet. I was trying to make it work only with Values. Butmy intention is to use Dictionary<string, Dictionary<string, int>> to Keysto.)
public interface ITranslator {
String Translate(String text, String fromCulture, String toCulture);
}
public class Translator : ITranslator {
public String Translate(String text, String fromCulture, String toCulture) {
foreach (TranslatorProviderBase provider in TranslatorConfiguration.Providers) {
Dictionary<String, String> row = provider.Table.First(x => x.Any(y => y.Key == fromCulture && y.Value == text));
if (row == null)
return null;
String translation;
if (row.TryGetValue(toCulture, out translation))
return translation;
}
return String.Empty;
}
}
public static class TranslatorConfiguration {
public static String DefaultFromCulture { get; private set; }
public static String DefaultToCulture { get; private set; }
public static IList<TranslatorProviderBase> Providers { get; private set; }
static TranslatorConfiguration() {
Cultures = new List<String>();
Providers = new List<TranslatorProviderBase>();
}
public static void AddProvider<T>() where T : TranslatorProviderBase, new() {
Providers.Add(new T());
}
public static void SetDefaultFromCulture(String culture) {
DefaultFromCulture = culture;
}
// ...
} // TranslatorConfiguration
public class TranslatorProviderBase {
public List<Dictionary<String, String>> Table { get; private set; }
public TranslatorProviderBase() {
Table = new List<Dictionary<String, String>>();
}
public void Add(Dictionary<String, String> translations) {
Table.Add(translations);
}
} // TranslatorProviderBase
A TranslatorProvider would be something as follows:
public class TranslatorProvider : TranslatorProviderBase {
public TranslatorProvider() {
Add(new Dictionary<String, String> { { "en", "Hello" }, { "pt", "Olá" }, { "fr", "Bonjour" } });
Add(new Dictionary<String, String> { { "en", "Bye" }, { "pt", "Adeus"}, { "fr", "Adieu" } });
}
} // TranslationProvider
Well this is where I am at the moment ...
Thank You,
Miguel