Extending Dictionary

S

Shapper

Hello,

I need to create a table with for localization. Something like:

"Hello", "en"

"Bounjour", "fr"

"Olá", "pt"

For this I could simply use a Dictionary<String, String> so something like:

public class TranslationDictionary : Dictionary<String, String> { }

The problem is one of this items should be used as a key.

I will have a method that does the following:

Get(String key, String language);

If I pass the key "Hello" and the language "en" then I return "Hello".

If I pass the key "Hello" and the language "pt" then I return "Olá".

So when I define the dictionary I need to define which item is the key.

And I would also like to create a Fluent Interface. Something like:

translations.For("Hello", "en").Add("Bounjour", "fr").Add("Olá", "pt");

The item in For would be the one used as key ... And the others are translations.

How can I create a Class to hold this data and use such an interface?

Thank You,

Miguel
 
A

Arne Vajhøj

I need to create a table with for localization. Something like:

"Hello", "en"

"Bounjour", "fr"

"Olá", "pt"

For this I could simply use a Dictionary<String, String> so something like:

public class TranslationDictionary : Dictionary<String, String> { }

The problem is one of this items should be used as a key.

I will have a method that does the following:

Get(String key, String language);

If I pass the key "Hello" and the language "en" then I return "Hello".

If I pass the key "Hello" and the language "pt" then I return "Olá".

So when I define the dictionary I need to define which item is the key.
Dictionary<String,Dictionary<String,String>>

translations[lang][key]

And I would also like to create a Fluent Interface. Something like:

translations.For("Hello", "en").Add("Bounjour", "fr").Add("Olá", "pt");

The item in For would be the one used as key ... And the others are translations.

That should just be a two extension methods.

Arne
 
A

Arne Vajhøj

I need to create a table with for localization. Something like:

"Hello", "en"

"Bounjour", "fr"

"Olá", "pt"

For this I could simply use a Dictionary<String, String> so something
like:

public class TranslationDictionary : Dictionary<String, String> { }

The problem is one of this items should be used as a key.

I will have a method that does the following:

Get(String key, String language);

If I pass the key "Hello" and the language "en" then I return "Hello".

If I pass the key "Hello" and the language "pt" then I return "Olá".

So when I define the dictionary I need to define which item is the key.
Dictionary<String,Dictionary<String,String>>

translations[lang][key]

And I would also like to create a Fluent Interface. Something like:

translations.For("Hello", "en").Add("Bounjour", "fr").Add("Olá", "pt");

The item in For would be the one used as key ... And the others are
translations.

That should just be a two extension methods.

Hmm.

It is actually a bit more tricky than that.

But try something like this:

using System;
using System.Collections.Generic;

namespace E
{
public class Translation
{
private Dictionary<string, Dictionary<int, string>> forward;
private Dictionary<string, Dictionary<string, int>> backward;
private int n;
public Translation()
{
forward = new Dictionary<string, Dictionary<int, string>>();
backward = new Dictionary<string, Dictionary<string, int>>();
n = 0;
}
private void CheckLang(string lang)
{
if(!forward.ContainsKey(lang)) forward.Add(lang, new
Dictionary<int, string>());
if(!backward.ContainsKey(lang)) backward.Add(lang, new
Dictionary<string, int>());
}
public int Add(string lang, string val)
{
CheckLang(lang);
n++;
forward[lang][n] = val;
backward[lang][val] = n;
return n;
}
internal void Add(string lang, int n, string val)
{
CheckLang(lang);
forward[lang][n] = val;
backward[lang][val] = n;
}
internal int BackwardLookup(string lang, string val)
{
return backward[lang][val];
}
internal string this[string lang, int n]
{
get { return forward[lang][n]; }
}
}
public static class Extensions
{
public static Tuple<Translation, int> For(this Translation
trans, string lang, string val)
{
return new Tuple<Translation, int>(trans,
trans.BackwardLookup(lang, val));
}
public static Tuple<Translation, int> Add(this
Tuple<Translation, int> ctx, string lang, string val)
{
ctx.Item1.Add(lang, ctx.Item2, val);
return ctx;
}
public static string Get(this Tuple<Translation, int> ctx,
string lang)
{
return ctx.Item1[lang, ctx.Item2];
}
}
public class Program
{
public static void Main(string[] args)
{
Translation trans = new Translation();
trans.Add("en", "Hello");
trans.For("en", "Hello").Add("fr", "Bonjour").Add("pt", "Olá");
Console.WriteLine(trans.For("en", "Hello").Get("fr"));
Console.WriteLine(trans.For("en", "Hello").Get("pt"));
Console.ReadKey();
}
}
}

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top