Starting with Version 7.2, Multi-Language for Visual Studio contains a code analysis and several associated code fixes.
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 rule applied by the Multi-Language code analysis is that every string in your source code should be either:
It is important to apply this rule during coding for two reasons.
It's the programmer's job
Your source code probably contains some strings which require localization and some which don't. If you simply translate all strings to another language, you will most likely break the functionality of the program.
You cannot expect a localization professional to know which strings require localization and which ones do not. This is a job for the original programmer.
You only want to do it once
Localization is not a one-off activity. After localizing version 1 of your program, you are probably going to want to localize version 2 of the program, which will probably contain some new strings.
If - in version 1 - you have 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 will probably end up looking at every one of them again, to decide whether or not it requires localization.
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.
One way to hide a string it to place it in double brackets. This is a special syntax supported by Multi-Language.
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.
The most common way to hide a string using Multi-Language is to place comment MLHIDE at the end of the line.
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".
In this case Multi-Language will generate a unique resource name and replace the string in your code with a reference to this resource string.
When you apply the code fix, Multi-Language will automatically add the new resource string to the file Resources.resx. It that file is not present, then it will generate a new resource file and add it to your project.
Multi-Language will also run the custom tool which generates the code file Resources.Designer.cs (or .vb), so that the resource name can be resolved by the compiler.
However, if there is already a resource with the exact same string, Multi-Language will use this resource, instead of creating a new resource.
This option is available if you have chosen to localize texts using the function ml_string().