Hello,
I would like to store this data into an array:
[Error1],[Lang1,Text]
[Error1],[Lang2,Text]
[Error2],[Lang1,Text]
So I have an error id and I would like to have an array or other object to quickly find the error and select the correct text depending on the language.
And if needed I just need to add a new language and text for an error I do not need to change the code where it is used to find the correct text.
Any idea's ?
Kind regards,
Loading
Danatas GerviPosted Nov 9, 2009, 10:50 AM
You can write your own class
public class TranslationKeeper
{
public TranslationKeeper()
{
Translations = new Dictionary<string,string>();
}
public Dictionary<string, string> Translations;
}
And use it in dictionary
Dictionary<string, TranslationKeeper> AllErrorsMessages = new Dictionary<string, TranslationKeeper>();
Possible code to fill in:
TranslationKeeper oneTranslation = new TranslationKeeper();
oneTranslation.Translations.Add("RUS", "Oshibka");
oneTranslation.Translations.Add("ENG", "Error");
AllErrorsMessages.Add("Error1", oneTranslation);
CharlyPosted Nov 9, 2009, 4:02 PM
Jorge L FernandezPosted Nov 9, 2009, 11:10 AM
Jorge L FernandezPosted Nov 9, 2009, 11:05 AM
You can use this approach for perfomance. A class which stores every Error, the ErrorStore and an Error class which actually represents each error with its specific messages by language. Implementation in both classes will run fast.
public class Error
{
public string ErrorID {get; set;}
Dictionary
public Error(string ErrorID)
{
this.ErrorID = ErrorID;
this.errors = new Dictionary
}
public void InsertError(string language, string message)
{
// INSERT ERROR ONLY IF IS NOT INSERTED YET
if(!this.errors.ContainsKey(language))
this.errors.Add(language,message);
}
public string GetError(string language)
{
if(this.errors.ContainsKey(language))
return this.errors[language];
return String.Empty;
}
}
public class ErrorStore
{
Dictionary
public ErrorStore()
{
this.errors = new Dictionary
}
public Error GetError(string errorID)
{
if(this.errors.ContainsKey(errorID))
return this.errors[errorID];
return null;
}
public void InsertError(string errorId, string language, string msg)
{
Error error = null;
if (this.errors.ContainsKey(errorId))
{
// ERROR ALREADY EXISTS
error = this.errors[errorId];
error.InsertError(language,msg);
}
else
{
// CREATE NEW AND INSERT IN PRIVATE DICTIONARY
error = new Error(errorId);
error.InsertError(language, msg);
this.errors.Add(errorId, error);
}
}
Danatas GerviPosted Nov 9, 2009, 10:58 AM
string TranslatedMessage = GetTranslationOf("Error1", "RUS", AllErrorsMessages);
use function:
private static string GetTranslationOf(string MessageKey, string LangCode,Dictionary<string, TranslationKeeper> AllErrorsMessages)
{
return AllErrorsMessages[MessageKey].Translations[LangCode];
}