FxCop Rules to catch non-externalized strings?

We are about to start externalizing all the strings in our product, to prepare to translate it into French. Are there any FxCop rules that can find string literals and remind us to externalize them

Thanks,
Josh Yeager



Answer this question

FxCop Rules to catch non-externalized strings?

  • antonio97b

    Josh, I had a closer look at CA1303 and it turns out it will only flag properties with the name "Text", "Caption", or "Message".

    FxCop seems to do nothing special if you mark a property with LocalizableAttribute = true, unfortunately. For example:


    private string text;
    [Localizable(true)]
    public String Something
    {
    set
    {
    text = value;
    }
    get
    {
    return "";
    }
    }
    //...
    obj.Something = "text";// no warning

    But, this causes a warning:


    string text;
    public String Message
    {
    set
    {
    text = value;
    }
    get
    {
    return "";
    }
    }
    // ...
    obj.Message = "text"; // warning

    David, Michael; should properties flagged with LocalizableAttribute = true raise a CA1303 in this case



  • MARL

    Peter,

    I thought that we did respect the LocalizableAttribute, however, after further investigation I found that we only respect it when it is applied to the actual parameter.

    So in example in the above, FxCop would respect it if you changed it to the following:



    public String Something
    {
     [param: Localizable(true)]
     set
     {
      text = value;
     }
     get
     {
      return "";
     }
    }

     

    This is obviously not ideal. As it turns out, this is one of the rules that will get rewritten after our new data flow analysis work has been finished, so I will add your example to our test cases.



  • sandPR

    There are a couple of ways FxCop currently determines if a parameter is localizable.

    - The name of the parameter or property. As you found, parameters named text, caption or message are automatically considered localizable as well as properties named Text, Caption and Message.

    - It respects the LocalizableAttribute, so if a parameter is marked Localizable(true) then it considered localization, and vice versa.

    In your property example above, the parameter name is value and the property name is Something so it is not considered localizable. The name of the backing store (in this case the field 'text') is not considered.



  • JayKay

    See DoNotPassLiteralsAsLocalizedParameters (CA1303), it specifically warns you about string literals. The help for this rule also explains how to write compliant code (http://www.gotdotnet.com/team/fxcop/Docs/Rules/Globalization/DoNotPassLiteralsAsLocalizedParameters.html)

  • Ray Price

    David, of course, the parameter name is "text". I was staring at it too long :-)

    public void SomethingElse(string somethingMore)
    works as expected.

  • Alex Jimenez

    Thanks, I've seen that one. But it won't catch strings that are assigned into properties, will it We have a lot of labels and things that get their text from properties.
  • j_jst

    Ok, that all makes sense. Thanks!
  • jwdenny

    Thanks David. At least there's a way to get FxCop to do what was queried. I was viewing it as a property thing; not a parameter thing.

    Method parameters seem to automatically be assumed to have Localizable(true) despite the documentation for LocalizableAttribute saying the default is false. e.g.:


    string text;
    string Something
    {
    set
    {
    text = value;
    }
    }
    public void SomethingElse(string text)
    {
    //...
    }
    // ...
    Something = "ha"; // no error/warning
    SomethingElse("ha ha"); // CA1303 error

    If method paramters truly default to Localizable(false); was where a conscious decision to assume method parameters are localizable unless Localizable(false) is explicitly attributed to the parameter If so, why not for property setter "methods"



  • FxCop Rules to catch non-externalized strings?