CA1303, DoNotPassLiteralsAsLocalizedParameters, but I'm actually not
Asked Answered
R

2

11

My code gets the CA1303 warning from Microsoft.Globalization, "Do not pass literals as localized parameters", but my code is not actually passing a literal:

private void MyForm_Load(object sender, EventArgs e)
{
    UpdateTitle();
}

private void UpdateTitle()
{
    Version version = Assembly.GetExecutingAssembly().GetName().Version;
    CultureInfo culture = CultureInfo.CurrentUICulture;
    this.Text = String.Format(culture, "{0} v{1}.{2} Alpha r{3}", this.Text, version.Major, version.Minor, version.Build);
}

This code sets the title of the form to something like this, every time it is loaded:

MyFormNameAsSetInTheDesigner v0.1 Alpha r123

(version.build actually contains the SVN revision, which is auto-updated at each commit, I do not use revision because my versioning scheme uses only 3 numbers, major.minor.revision)

But this triggers the aforementioned warning, because it thinks I'm setting the titlebar text from a string literal. In fact, I've set Localizable = True in the designer, so that the string is fetched from a resource table.

I do not want to set the form's title statically because (especially in the alpha and beta stages) I want it to have dynamic version numbers.

The question is what do I do so I don't get a warning (for example, which code will do what I'm doing but be considered correct per FxCop, or how can I supress it for that line).

Redwing answered 21/3, 2012 at 12:45 Comment(1)
@Jon The question is what do I do so I don't get a warning (for example, which code will do what I'm doing but be considered correct per FxCop, or how can I supress it for that line).Redwing
A
14

Based on the documentation for CA1303, the reason for the warning being raised is that you pass a literal string as the second parameter of the String.Format method, and in addition the second formal parameter of that particular overload is annotated with LocalizableAttribute.

Therefore what the warning wants you to do is to put the string "{0} v{1}.{2} Alpha r{3}" as a localized resource in your resource assembly, and refer to it as such. Which is probably a good idea, as technically the structure of the format string and the fixed parts of its contents are localizable resources.

If you simply want to make FxCop shut up, you can annotate UpdateTitle accordingly:

[SuppressMessage("Microsoft.Globalization",
                 "CA1303:DoNotPassLiteralsAsLocalizedParameters" )]
private void UpdateTitle() { /* ... */ }
Alrich answered 21/3, 2012 at 12:53 Comment(6)
I see, this makes sense. But how do I do this? (there's a "Resources" tab in the project's properties, but how do I specify the language of the properties I'm setting? For example, I'd like this to be the default, and if other language requires a different format string, to set one just for that language).Redwing
@CamiloMartin: msdn.microsoft.com/en-us/library/y99d1cd3%28v=vs.100%29.aspxAlrich
I just wanted to note that the instructions Microsoft gives are terribly awkward and overcomplicated. You just need to: 1) edit resources in the project properties' "resources" tab, 2) fetch the resource from Properties.Resources.YourResourceName in the code, and 3) to localize you just create (from the resource file template) a Resources.en-US.resx (example language code) sitting alongside Resources.resx, then edit a property with the same YourResourceName used before. Done. Microsoft actually tells you to create satellite assemblies and use a resource manager - you don't need to!Redwing
@CamiloMartin: Could you help? 1) leads to the ressource dialogue with 3 columns (Name, Value, Comment) It is unclear to me what i should edit there. 2) should be clear: every field inside MyResourceFile can be accessed from code by Properties.Resources.MyResourceFileName.MyAttribute. 3) i believe is partly clear: i add an additional file for example Properties.Resources.MyResourceFileName.en-US.resx and inside this xml file i have to add the same fields / attributes: <data name="MyAttribute" ...><value>myTranslation</value></data> Can you confirm 1-3 and clarify step 1? ThanksFranciskus
@threeFourOneSixOneThree 1) You put the desired variable name (variable name rules apply), the contents of the variable (a string), and optionally a comment. For example: name: "ApplicationName", value: "Fartilizer Pro 2014 360 One", comment: "Until I settle on a name". 2) Didn't remember having to do that, maybe for the "default" resources you don't have to? It's been a while. 3) You don't need to edit the resx files textually, the editor on step one does that for you.Redwing
this may sound silly, but it is not better to define a string variable prior to that line, and the assign it?Matron
S
0

A general way to ignore this warning is to add the following to your to your .csproj file(s):

<NoWarn>CA1303</NoWarn>
Stormystorting answered 16/10, 2020 at 15:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.