0

I have an ASP.NET MVC application where I pass certain translations to my views so I can show my pages in different languages (the translations are used in the corresponding javascript file, not in the razor view). Now I have a List<string> for every view which contains the descriptions of the translations I need, which is a real pain to maintain of course. If I change a single javascript file, I need to update the corresponding collection etc.

Now I had a crazy idea, in all my js files I use dictionary.find('<description>') to get access to the translations. Would it be a bad idea to populate my lists when the model is first accessed by using a regex on the javascript files? It would look something like this:

protected static List<string> Descriptions;

private static Model()
{
    string basePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Scripts");
    string fileName = Path.Combine(basePath, $"{modelName}.js");
    string javascript = File.ReadAllText(fileName);
    Regex regex = new Regex(@"dictionary\.find\('(.+?)'\)");
    var matches = regex.Matches(javascript)
                       .Cast<Match>()
                       .Select(m => m.Groups[1].Value)
                       .ToList();
}

This code basically reads the javascript file that is used for the view and finds all the words that are used between dictionary.find('...'). I've already tested this, and it seems to work but my question is:

How bad of an idea is this? Or is it good? My models/scripts are named very consistently etc so that wouldn't be a problem.

5
  • 1
    If the description has no newlines in it, and no escaped sequences, then it is quite all right. Commented Apr 29, 2016 at 13:21
  • 1
    You can always write a test that verifies all your files match your expected format as well. Then whenever someone else comes in to make changes, there is a safety net. Commented Apr 29, 2016 at 13:22
  • @SpykeBytes Great idea, this would indeed be of great use when my colleagues work on the project. Commented Apr 29, 2016 at 13:24
  • @WiktorStribiżew So you don't think there's any disadvantages? It's not that hard obviously to just add descriptions when I change the javascript files, but I'm lazy and it's just so ugly having 100's of lines of descriptions inside a class Commented Apr 29, 2016 at 13:25
  • @AlexanderDerck: Regex is meant to reduce tons of lines of code. Commented Apr 29, 2016 at 13:33

1 Answer 1

1

I would think that you would want to store data in file types meant for that.

I think there is no problem having a conventions based approach to this, but you might be better suited putting your <description> data in a JSON file. The JSON file could live right next to your js files.

If you do that, you can just load up your translations by using a json serializer and you won't have to muck around with regex.

EDIT: I think it would still be a good idea to have a test verifying that all of your expected files do exist and match your expected format.

Sign up to request clarification or add additional context in comments.

2 Comments

But then I need to manually say which translations I need again per javascript file. For example now I add a new method for an ajax request in the js file and I need dictionary.find('noConnectionError'), then I need to update the file too to include "noConnectionError" (which is easy to forget) :/
Not if you use naming conventions and have an automated test. But this is the classic argument of convention vs configuration.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.