S
Shapper
Hello,
I am creating a translation service to use on application.
My idea is to setup the translations as follows:
public class MsgTranslatorProvider : TranslatorProviderBase {
public MsgTranslatorProvider() {
For("Hello", "en").Add(new Dictionary<String, String> { { "Bonjour", "fr" }, { "Olá", "pt" } });
}
}
Note that "en" version is the main version ...
Then on my application I would simply request a translation:
_translator.Get("Hello", "pt");
I have this working but I would like suggestions to improve it:
public interface ITranslatorService {
String Get(String text, String culture);
} // ITranslatorService
public class TranslatorService : ITranslatorService {
public String Get(String text, String culture) {
return TranslatorStore.Get(text, culture);
} // Get
} // TranslatorService
public class TranslatorStore {
private static IList<TranslatorProviderBase> _providers = new List<TranslatorProviderBase>();
public static void AddProvider<T>() where T : TranslatorProviderBase, new() {
_providers.Add(new T());
} // AddProvider
public static void AddProvider(TranslatorProviderBase provider) {
foreach (TranslatorProviderBase _provider in _providers)
if (_provider.Translations.Keys.Intersect(provider.Translations.Keys).Count() > 0)
throw new ArgumentException("Duplicate texts where found in the providers");
_providers.Add(provider);
} // AddProvider
public static String Get(String text, String culture) {
foreach (TranslatorProviderBase provider in _providers) {
IDictionary<String, String> translation;
if (provider.Translations.TryGetValue(text, out translation))
return translation[culture];
}
return String.Empty;
} // Get
} // TranslatorStore
public class TranslatorProviderBase {
public TranslatorDictionary Translations { get; private set; }
public TranslatorProviderBase() {
Translations = new TranslatorDictionary();
} // TranslatorProviderBase
public TranslatorBuilder For(String text, String culture) {
if (Translations.ContainsKey(text))
throw new ArgumentException(String.Format("A translation of text '{0}' already exists"));
else
Translations.Add(text, new Dictionary<String, String>(StringComparer.OrdinalIgnoreCase) { { culture, text } });
return new TranslatorBuilder(text, Translations);
} // For
} // TranslatorProviderBase
public class TranslatorBuilder {
private readonly String _key;
private readonly TranslatorDictionary _translations;
public TranslatorBuilder(String key, TranslatorDictionary translations){
_key = key;
_translations = translations;
} // TranslatorBuilder
public TranslatorBuilder Add(String text, String culture) {
_translations[_key].Add(culture, text);
return this;
} // Add
public TranslatorBuilder Add(Dictionary<String, String> translations) {
foreach (KeyValuePair<String, String> translation in translations)
_translations[_key].Add(translation.Value, translation.Key);
return this;
} // Add
} // TranslatorBuilder
public class TranslatorDictionary : Dictionary<String, IDictionary<String, String>> { } // TranslatorDictionary
Questions:
1 - In TranslationBuilder I am using StringComparer.OrdinalIgnoreCasewhen adding a new dictionary.
But how to use StringComparer.OrdinalIgnoreCase in all dictionaries, e.g, in TranslatorDictionary.
2 - TranslatorStore is used to hold all translation providers. Should I name it this way?
3 - TranslatorStore has a few static fields an methods.
The translations should be defined only once and not every time a translation is requested.
I am not sure if this is the correct way to do it.
4 - Should TranslatorStore has a Get method to get a translator? Or only toget a provider?
5 - Should I have a Factory for TranslatorService?
I will be injecting ITranslatorService in my application. Then I will use it as follows:
_translator.Get("Hello", "pt");
I am felling something better could be done to store TranslationProviders and wire them to the TranslationService.
Anyway, any help or suggestion to improve this is welcome.
I am creating a translation service to use on application.
My idea is to setup the translations as follows:
public class MsgTranslatorProvider : TranslatorProviderBase {
public MsgTranslatorProvider() {
For("Hello", "en").Add(new Dictionary<String, String> { { "Bonjour", "fr" }, { "Olá", "pt" } });
}
}
Note that "en" version is the main version ...
Then on my application I would simply request a translation:
_translator.Get("Hello", "pt");
I have this working but I would like suggestions to improve it:
public interface ITranslatorService {
String Get(String text, String culture);
} // ITranslatorService
public class TranslatorService : ITranslatorService {
public String Get(String text, String culture) {
return TranslatorStore.Get(text, culture);
} // Get
} // TranslatorService
public class TranslatorStore {
private static IList<TranslatorProviderBase> _providers = new List<TranslatorProviderBase>();
public static void AddProvider<T>() where T : TranslatorProviderBase, new() {
_providers.Add(new T());
} // AddProvider
public static void AddProvider(TranslatorProviderBase provider) {
foreach (TranslatorProviderBase _provider in _providers)
if (_provider.Translations.Keys.Intersect(provider.Translations.Keys).Count() > 0)
throw new ArgumentException("Duplicate texts where found in the providers");
_providers.Add(provider);
} // AddProvider
public static String Get(String text, String culture) {
foreach (TranslatorProviderBase provider in _providers) {
IDictionary<String, String> translation;
if (provider.Translations.TryGetValue(text, out translation))
return translation[culture];
}
return String.Empty;
} // Get
} // TranslatorStore
public class TranslatorProviderBase {
public TranslatorDictionary Translations { get; private set; }
public TranslatorProviderBase() {
Translations = new TranslatorDictionary();
} // TranslatorProviderBase
public TranslatorBuilder For(String text, String culture) {
if (Translations.ContainsKey(text))
throw new ArgumentException(String.Format("A translation of text '{0}' already exists"));
else
Translations.Add(text, new Dictionary<String, String>(StringComparer.OrdinalIgnoreCase) { { culture, text } });
return new TranslatorBuilder(text, Translations);
} // For
} // TranslatorProviderBase
public class TranslatorBuilder {
private readonly String _key;
private readonly TranslatorDictionary _translations;
public TranslatorBuilder(String key, TranslatorDictionary translations){
_key = key;
_translations = translations;
} // TranslatorBuilder
public TranslatorBuilder Add(String text, String culture) {
_translations[_key].Add(culture, text);
return this;
} // Add
public TranslatorBuilder Add(Dictionary<String, String> translations) {
foreach (KeyValuePair<String, String> translation in translations)
_translations[_key].Add(translation.Value, translation.Key);
return this;
} // Add
} // TranslatorBuilder
public class TranslatorDictionary : Dictionary<String, IDictionary<String, String>> { } // TranslatorDictionary
Questions:
1 - In TranslationBuilder I am using StringComparer.OrdinalIgnoreCasewhen adding a new dictionary.
But how to use StringComparer.OrdinalIgnoreCase in all dictionaries, e.g, in TranslatorDictionary.
2 - TranslatorStore is used to hold all translation providers. Should I name it this way?
3 - TranslatorStore has a few static fields an methods.
The translations should be defined only once and not every time a translation is requested.
I am not sure if this is the correct way to do it.
4 - Should TranslatorStore has a Get method to get a translator? Or only toget a provider?
5 - Should I have a Factory for TranslatorService?
I will be injecting ITranslatorService in my application. Then I will use it as follows:
_translator.Get("Hello", "pt");
I am felling something better could be done to store TranslationProviders and wire them to the TranslationService.
Anyway, any help or suggestion to improve this is welcome.