Is it possible to generate a list of all marked lines in C # at compile time / runtime?

So, I have a small translation system configured for my application, where we create a list of all the lines that are marked as translatable, dump that in CSV as a translation template, and then the translator fills in the next column with translations.

The problem I'm trying to solve is how to extract a bunch of tagged lines from the code base to automate the creation of a translation template.

An example C # line of code looks something like this:

textBoxName.Text = string.Format(Translate.tr("Create {0}"), NextAutoName()); 

And C ++ will look like this:

 info_out << tr( L"Grip weights range from {0} to {1}" )(low_weight)(high_weight) << endl; 

On the C ++ side, when constructing a list of strings for generating a template, the C ++ parser is used (see my previous question ), which is performed as part of external assemblies on all C ++ code in the project. Basically, any string placed in a tr () call is automatically retrieved.

Is there a better solution with C # than writing another parser? I would like a list of lines that are created at compile time, or one that I can get at runtime. A List<string> would be great.

I would like to keep the same translation file format, because it simplifies the coordination of the two sides. As expected, we use many lines.

Currently, it’s much more convenient to update the translation template in c ++ - I just need to make sure that the lines I want to translate are wrapped in tr (), and the parser processes the rest. In C #, I manually check all the lines and update the dummy function on the C ++ side with new lines. I am approaching the break and just writing another parser. I was hoping C #, with its high-level features, could do a better job here.

+4
source share
2 answers

I have a project that actually does the same thing. Only the syntax translation itself is written in C # (this is the name c3po). In addition to analyzing the project, c3po is also responsible for generating the files to send to the translation provider, as well as for creating files that the .net project uses to store the translated strings. We found that it has several advantages over the “traditional” .Net resource files:

1), since c3po maintains its own internal database of localized strings, we can track our own translation memory and make sure that new lines are sent to translators every month. It also removes lines that are no longer needed. This saved us literary thousands of dollars in translation costs.

2) Developers are free to write any desired line wherever they want, and they do not need to worry about resource files.

3) c3po serves several different projects at once, which simplifies our interactions with our translation provider.

4) We can automate c3po through our CI server, so every time a developer checks (or one night or something else), we can perform all tasks, including sending files to translators, collecting new phrases, etc. .

+1
source

I did something similar (I just pulled out a list of string constants), and I used the same parser for C # and C ++. Although it was a more lexical analyzer than a parser, which works because the two languages ​​have a very similar lexical structure.

Once you get the list of lines, you can write it to the C # source file and compile it into your program. Your code will generate a file something like this:

 namespace MyProject { class MyStrings { public string[] Strings = { "pony", "cob", "stallion" }; } } 

You can then include this file in your project to give your code access to a list of its lines.

If you run the tool as a pre-build event, this will happen as part of the build.

However, as Hans says, you'd better look at the native localization support.

+1
source

Source: https://habr.com/ru/post/1343085/


All Articles