S
Shapper
Hello,
I am creating a translator as follows:
public interface ITranslator {
String Translate(String text, String fromCulture, String toCulture);
} // ITranslator
Basically, this translates a string from one culture to another.
This is working in a different way from something I asked sometime ago in this newsgroup. I hope I am able to get some advice on this version.
I have a class which holds groups of translations:
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
Using this class as base I can place translations in different parts of my application:
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
I created a class to "save" the providers and the translator configuration:
public class TranslatorConfiguration {
public IList<String> Cultures { get; private set; }
public IList<TranslatorProviderBase> Providers { get; private set; }
public TranslatorConfiguration() {
Cultures = new List<String>();
Providers = new List<TranslatorProviderBase>();
}
public void AddProvider(TranslatorProviderBase provider) {
Providers.Add(provider);
}
public void AddProvider<T>() where T : TranslatorProviderBase, new() {
Providers.Add(new T());
}
public void SetValidCultures(params String[] cultures) {
Cultures = cultures.ToList();
}
} // TranslatorConfiguration
I have a class through which I configure the translator:
public class TranslatorManager {
public static void Initialize(Action<TranslatorConfiguration> configuration) {
configuration.Invoke(new TranslatorConfiguration());
} // Initialize
} // TranslatorManager
NOTE: This class will have other methods like AssertTranslatorConfigurationIsValid, etc.
The translator setup and usage would be something like this:
TranslatorManager.Initialize(x => {
x.AddProvider<TranslatorProvider>();
x.SetValidCultures("en", "pt", "fr");
});
ITranslator translator = new Translator();
String adeus = translator.Translate("Bye", "en", "pt");
PROBLEMS:
1 - My main problem, at the moment, is how to wire the Translator, implementation of ITranslator, to the TranslatorConfiguration so I can access providers and values in Translator.
2 - I think I should also allow to register the container in configuration and have some kind of factory to wire things ... But I am not sure if yes and how.
My Translator, implementation of ITranslator, is the following:
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;
} // Translate
} // Translate
See how I am using "TranslatorConfiguration.Providers" in my foreach loop.
Of course this does not work unless I make TranslatorConfiguration as static as well as its methods and properties.
But then I get the following errors in TranslatorManager:
'TranslatorConfiguration': static types cannot be used as type arguments
Cannot create an instance of the static class
I have seen a few libraries 'working' this way but I am not sure how to wire the configuration values (TranslatorConfiguration) to the service (Translator).
Thank You,
Miguel
I am creating a translator as follows:
public interface ITranslator {
String Translate(String text, String fromCulture, String toCulture);
} // ITranslator
Basically, this translates a string from one culture to another.
This is working in a different way from something I asked sometime ago in this newsgroup. I hope I am able to get some advice on this version.
I have a class which holds groups of translations:
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
Using this class as base I can place translations in different parts of my application:
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
I created a class to "save" the providers and the translator configuration:
public class TranslatorConfiguration {
public IList<String> Cultures { get; private set; }
public IList<TranslatorProviderBase> Providers { get; private set; }
public TranslatorConfiguration() {
Cultures = new List<String>();
Providers = new List<TranslatorProviderBase>();
}
public void AddProvider(TranslatorProviderBase provider) {
Providers.Add(provider);
}
public void AddProvider<T>() where T : TranslatorProviderBase, new() {
Providers.Add(new T());
}
public void SetValidCultures(params String[] cultures) {
Cultures = cultures.ToList();
}
} // TranslatorConfiguration
I have a class through which I configure the translator:
public class TranslatorManager {
public static void Initialize(Action<TranslatorConfiguration> configuration) {
configuration.Invoke(new TranslatorConfiguration());
} // Initialize
} // TranslatorManager
NOTE: This class will have other methods like AssertTranslatorConfigurationIsValid, etc.
The translator setup and usage would be something like this:
TranslatorManager.Initialize(x => {
x.AddProvider<TranslatorProvider>();
x.SetValidCultures("en", "pt", "fr");
});
ITranslator translator = new Translator();
String adeus = translator.Translate("Bye", "en", "pt");
PROBLEMS:
1 - My main problem, at the moment, is how to wire the Translator, implementation of ITranslator, to the TranslatorConfiguration so I can access providers and values in Translator.
2 - I think I should also allow to register the container in configuration and have some kind of factory to wire things ... But I am not sure if yes and how.
My Translator, implementation of ITranslator, is the following:
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;
} // Translate
} // Translate
See how I am using "TranslatorConfiguration.Providers" in my foreach loop.
Of course this does not work unless I make TranslatorConfiguration as static as well as its methods and properties.
But then I get the following errors in TranslatorManager:
'TranslatorConfiguration': static types cannot be used as type arguments
Cannot create an instance of the static class
I have seen a few libraries 'working' this way but I am not sure how to wire the configuration values (TranslatorConfiguration) to the service (Translator).
Thank You,
Miguel