Starting with Version 7.2, Multi-Language for Visual Studio contains a code analysis and several associated code fixes.
Every code analysis checks a rule. Lets start by looking at what the rule is.
Every string in your source code should be either
In particular, why is it important to mark strings which do not require localization?
This is important for two reasons:
Your source code probably contains some strings which require localization, but it will probably contain some strings which should not be localized. In fact, it may contain strings which must not be localized, because doing so would break the functionality of the program.
Figuring out which strings require localization, and which do not, is best performed by the original programmer.
After version 1 of your program has been localized and released, you are probably going to make some changes and at some point release a version 2.
Version 2 probably contains some new strings which require localization.
If you had previously (in version 1) marked the strings which do not require localization, then it will be fairly easy to find any new strings added in version 2.
If you did not mark the strings which do not require localization, then you might end up looking at every one of them again, to decide whether or not it requires localization.
A code analysis is a rule that is applied in the background. If the rule is violated, then the code which violates the rule is underlined with a wavy green line.
There is some general information about code analyzers on this page.
A code fix is a change that can be made to your code, so that the rule is satisfied. There may be one or more code fixes for any rule.
The code fixes appear in the menu below the light bulb icon. This appears in the left hand margin, or next to the code, if you hold the mouse pointer over the code with the wavy green line.
The code analysis is active on any project which is already using Multi-Language.
Multi-Language adds a project database file (usually ProjectName_ml.xml) to the project, so the code analysis should be active if this file is present in your project.
In addition, the code analysis can be enabled and disabled in the project settings dialog, as shown below. By default, the code analysis is enabled.
It is not necessary to scan the project using the the Multi-Language tool window.
The code analysis checks the strings and interpolated strings in the source code files (C# and VB).
For each string, it checks whether it is "hidden" ...
Strings which do not contain any letters are ignored.
For example ".", "100" or " ".
In the current version (7.02.0004) attribute parameters are ignored, because a necessary code fix has not yet been implemented.
This will be changed in a future version.
Parameters to certain special functions are ignored, in particular:
This list may be expanded in future.
You can define a hidden block using the comments MLHIDEON and MLHIDEOFF, for example
//MLHIDEON private string templatefile = "template.cs" ; private string logfile = "logfile.txt" ; private string helpfile = "helpfile.chm" ; //MLHIDEOFF
Any string in a hidden block is ignored.
This is a special syntax used by Multi-Language to hide a single string, for example:
public string junk()
{
string hello = (("Hello World")) ;
return hello ;
}
Strings in double brackets are ignored.
The usual way to hide a string using Multi-Language is to add the comment MLHIDE to the end of the line, for example:
public string junk()
{
string hello = "Hello World" ; //MLHIDE
return hello ;
}
Any string on a line with this comment is ignored.
All strings in certain blocked functions are ignored, in particular:
This list may be expanded in future.
Actually, it only really makes sense to ignore the function InitializeComponent in WinForms projects (and not WPF) so this might also be changed.
ASP.NET Core supports a method of localization using a "localizer" object with the interface IStringLocalizer. This uses the original text string as resource-key to for the localized text.
The parameter to the localizer object is ignored..
Any string which is not ignored for one of the reasons listed above violates the rule.
There are generally three possible code fixes:
In each case, a preview window shows the exact code change, before it is made.
This has already been described in the table above.
If there is only one string on the line, I generally prefer to use the MLHIDE comment instead of this method, because the syntax is a bit odd.
If there are multiple strings on a single line, this method has the advantage of of applying only to one string, and not to the whole line.
This has already been described in the table above.
If there are multiple strings on the line, this method has the disadvantage of hiding them all (but it has the advantage of simplicity).
This option is available if you have chosen to localize texts using so called "Named Resources".