0

I have a series of Resource Files for Localization purposes.In Drupal, there are something called tokens, which are some patterned text that it is replaced by the server when it is called.

Example:

Resource file in English:

I have this localization text in my resource file in English, and I would like to display to $[user] how this works.

Resource file in Spanish:

Yo tengo este texto localizado en el archivo de recursos en español y quisiera enseñarle al $[user] cómo esto funciona.

Then, after some parsing I would replace the token with the correct details.

My current approach has been the following:

public class LocalizationParser
{
    private Dictionary<string, dynamic> Tokens { get; set; }

    public LocalizationParser()
    {
        Tokens = new Dictionary<string, dynamic>();
        Tokens.Add("$[user]", HttpContext.Current.User.Identity.Name);
    }


    public string ParseResource(string text)
    {
        foreach(var token in Tokens)
        {
            text.Replace(token.Key, token.Value);
        }
        
        return text;
    }
}

I know I have a huge problem that I am getting all these values populated, and there WILL be an unnecessary performance impact. I know that if I could call a method which could retrieve the correct value only if the dictionary key has been found, there wouldn't be a huge overhead into it.

What is the correct way to do this in c#? (Currently using ASP.NET MVC 5 with EF 6.1)

Dharman
  • 30,962
  • 25
  • 85
  • 135
Jose A
  • 10,053
  • 11
  • 75
  • 108
  • 1
    in ParseResource loop, you should have `text = text.Replace(token.Key, token.Value);` for this to work – Wapac Feb 13 '16 at 14:19
  • Hahah. Thanks you're right! What about the approach? – Jose A Feb 13 '16 at 14:20
  • 1
    I am also unsure, why dynamic is being used there. Could you explain why not ? – Wapac Feb 13 '16 at 14:21
  • 1
    As for the approach I am not sure I understand what the problem is - do you worry about that you have thousands of these tokens and thousands of these messages but each message is to contain only one or two tokens and thus iterating through all the tokens is resource wasting? – Wapac Feb 13 '16 at 14:22
  • @Wapac: The reason which is dynamic is because it is going.... OMG You're right. My bad :/ BTW, I'm future thinking. I don't know if this is going to get big, so I wanted to be safe just in case. – Jose A Feb 13 '16 at 14:26

1 Answers1

2

"Premature optimization is the root of all evil" as Knuth says. Why are you so sure there will be a performance impact?

Is this an assumption or a fact that you can back up with numbers that you identified a bottleneck with a profiler like dotTrace? Also it would be useful to know how many tokens do you expect to have.

In any case, a Replace is fine. You can use a Hashtable and use the Exists method combined with a regular expression to get first the tokens and then do the lookup.

Just remember that strings are immutable and thus every replacement means a nee copy and that can be slow if done any times.

For modifying many times a string use instead a StringBuilder.

The process is thoroughly documented: https://msdn.microsoft.com/en-us/library/2839d5h5(v=vs.110).aspx

xmorera
  • 1,933
  • 3
  • 20
  • 35
  • Great! Will be replacing it asap. – Jose A Feb 13 '16 at 14:29
  • I went ahead and looked up Hashtables. It seems that Dictionary is the upgraded Hashtable."Don't use this. It is an older .NET Framework type. It is slower than the generic Dictionary type. But if an old program uses Hashtable, it is helpful to know how to use this type." http://www.dotnetperls.com/dictionary – Jose A Feb 13 '16 at 14:35
  • 1
    Yes but read this: http://stackoverflow.com/questions/301371/why-is-dictionary-preferred-over-hashtable – xmorera Feb 13 '16 at 21:07