2023-05-03 11:06:20 +02:00
|
|
|
== Why is this an issue?
|
|
|
|
|
2021-04-28 16:49:39 +02:00
|
|
|
String literals embedded in the source code will not be localized properly.
|
|
|
|
|
|
|
|
|
|
|
|
This rule raises an issue when a literal string is passed as a parameter or property and one or more of the following cases is true:
|
|
|
|
|
|
|
|
* The ``++LocalizableAttribute++`` attribute of the parameter or property is set to true.
|
|
|
|
* The parameter or property name contains "Text", "Message", or "Caption".
|
|
|
|
* The name of the string parameter that is passed to a ``++Console.Write++`` or ``++Console.WriteLine++`` method is either "value" or "format".
|
|
|
|
|
2021-04-28 18:08:03 +02:00
|
|
|
|
2023-05-03 11:06:20 +02:00
|
|
|
=== Noncompliant code example
|
2021-04-28 16:49:39 +02:00
|
|
|
|
2022-02-04 17:28:24 +01:00
|
|
|
[source,csharp]
|
2021-04-28 16:49:39 +02:00
|
|
|
----
|
|
|
|
using System;
|
|
|
|
using System.Globalization;
|
|
|
|
using System.Reflection;
|
|
|
|
using System.Windows.Forms;
|
|
|
|
|
|
|
|
[assembly: NeutralResourcesLanguageAttribute("en-US")]
|
|
|
|
namespace MyLibrary
|
|
|
|
{
|
|
|
|
public class Foo
|
|
|
|
{
|
|
|
|
public void SetHour(int hour)
|
|
|
|
{
|
|
|
|
if (hour < 0 || hour > 23)
|
|
|
|
{
|
|
|
|
MessageBox.Show("The valid range is 0 - 23."); // Noncompliant
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
----
|
|
|
|
|
2021-04-28 18:08:03 +02:00
|
|
|
|
2023-05-03 11:06:20 +02:00
|
|
|
=== Compliant solution
|
2021-04-28 16:49:39 +02:00
|
|
|
|
2022-02-04 17:28:24 +01:00
|
|
|
[source,csharp]
|
2021-04-28 16:49:39 +02:00
|
|
|
----
|
|
|
|
using System;
|
|
|
|
using System.Globalization;
|
|
|
|
using System.Reflection;
|
|
|
|
using System.Resources;
|
|
|
|
using System.Windows.Forms;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[assembly: NeutralResourcesLanguageAttribute("en-US")]
|
|
|
|
namespace MyLibrary
|
|
|
|
{
|
|
|
|
public class Foo
|
|
|
|
{
|
|
|
|
ResourceManager rm;
|
|
|
|
public Foo()
|
|
|
|
{
|
|
|
|
rm = new ResourceManager("en-US", Assembly.GetExecutingAssembly());
|
|
|
|
}
|
|
|
|
|
|
|
|
public void SetHour(int hour)
|
|
|
|
{
|
|
|
|
if (hour < 0 || hour > 23)
|
|
|
|
{
|
|
|
|
MessageBox.Show(
|
|
|
|
rm.GetString("OutOfRangeMessage", CultureInfo.CurrentUICulture));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
----
|
2021-04-28 18:08:03 +02:00
|
|
|
|
2021-09-20 15:38:42 +02:00
|
|
|
ifdef::env-github,rspecator-view[]
|
|
|
|
|
|
|
|
'''
|
|
|
|
== Implementation Specification
|
|
|
|
(visible only on this page)
|
|
|
|
|
2023-05-25 14:18:12 +02:00
|
|
|
=== Message
|
|
|
|
|
|
|
|
Replace this string literal with a string retrieved through an instance of the "ResourceManager" class.
|
|
|
|
|
|
|
|
|
|
|
|
=== Highlighting
|
|
|
|
|
|
|
|
String litteral
|
2021-09-20 15:38:42 +02:00
|
|
|
|
|
|
|
|
|
|
|
endif::env-github,rspecator-view[]
|