2023-05-03 11:06:20 +02:00
== Why is this an issue?
2021-01-27 13:42:22 +01:00
It is important to inform the ``++ResourceManager++`` of the language used to display the resources of the neutral culture for an assembly. This improves lookup performance for the first resource loaded.
2020-06-30 12:49:37 +02:00
2021-02-02 15:02:10 +01:00
2021-01-27 13:42:22 +01:00
This rule raises an issue when an assembly contains a ``++ResX++``-based resource but does not have the ``++System.Resources.NeutralResourcesLanguageAttribute++`` applied to it.
2020-06-30 12:49:37 +02:00
2023-05-03 11:06:20 +02:00
=== Noncompliant code example
2020-06-30 12:49:37 +02:00
2022-02-04 17:28:24 +01:00
[source,text]
2020-06-30 12:49:37 +02:00
----
using System;
public class MyClass // Noncompliant
{
public static void Main()
{
string[] cultures = { "de-DE", "en-us", "fr-FR" };
Random rnd = new Random();
int index = rnd.Next(0, cultures.Length);
Thread.CurrentThread.CurrentUICulture = CultureInfo.CreateSpecificCulture(cultures[index]);
ResourceManager rm = new ResourceManager("MyResources" ,
typeof(MyClass).Assembly);
string greeting = rm.GetString("Greeting");
Console.Write("Enter your name: ");
string name = Console.ReadLine();
Console.WriteLine("{0} {1}!", greeting, name);
}
}
----
2023-05-03 11:06:20 +02:00
=== Compliant solution
2020-06-30 12:49:37 +02:00
2022-02-04 17:28:24 +01:00
[source,text]
2020-06-30 12:49:37 +02:00
----
using System;
[assembly:NeutralResourcesLanguageAttribute("en")]
public class MyClass
{
public static void Main()
{
string[] cultures = { "de-DE", "en-us", "fr-FR" };
Random rnd = new Random();
int index = rnd.Next(0, cultures.Length);
Thread.CurrentThread.CurrentUICulture = CultureInfo.CreateSpecificCulture(cultures[index]);
ResourceManager rm = new ResourceManager("MyResources" ,
typeof(MyClass).Assembly);
string greeting = rm.GetString("Greeting");
Console.Write("Enter your name: ");
string name = Console.ReadLine();
Console.WriteLine("{0} {1}!", greeting, name);
}
}
----