【问题标题】:ResX Autogenerated File for Culture Specific Access to ResourcesResX 自动生成的文件,用于特定于文化的资源访问
【发布时间】:2020-03-09 16:54:16
【问题描述】:

有没有办法自动生成一个文件,可以对资源执行线程安全、文化特定的访问? Visual Studio 为我的 resx 文件生成的当前 Resource.Designer.cs 类如下所示:

    private static global::System.Resources.ResourceManager resourceMan;

    private static global::System.Globalization.CultureInfo resourceCulture;

    [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
    internal Resource()
    {
    }

    /// <summary>
    ///   Returns the cached ResourceManager instance used by this class.
    /// </summary>
    [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
    public static global::System.Resources.ResourceManager ResourceManager
    {
        get
        {
            if (object.ReferenceEquals(resourceMan, null))
            {
                global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("ServiceModels.Resource", typeof(Resource).Assembly);
                resourceMan = temp;
            }
            return resourceMan;
        }
    }

    /// <summary>
    ///   Overrides the current thread's CurrentUICulture property for all
    ///   resource lookups using this strongly typed resource class.
    /// </summary>
    [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
    public static global::System.Globalization.CultureInfo Culture
    {
        get
        {
            return resourceCulture;
        }
        set
        {
            resourceCulture = value;
        }
    }

    /// <summary>
    ///   Looks up a localized string similar to Hiii.
    /// </summary>
    public static string Hello
    {
        get
        {
            return ResourceManager.GetString("Hello", resourceCulture);
        }
    }

这允许安全访问“Hello”资源,但在使用不同文化的不同资源文件时,它似乎不是线程安全的。例如,使用此代码,您可以通过

设置资源
Culture = CultureInfo.SomeCulture;

这将为使用 ResourceManager 的所有线程设置文化。有没有办法自动生成一个允许线程安全、文化特定访问资源的文件,还是我必须自己实现一个?

【问题讨论】:

    标签: c# resx


    【解决方案1】:

    Thread.CurrentThread.CurrentCulture 是线程特定的Culture 设置,大多数库都使用它来格式化日期/时间或数字,以及在您的情况下获取翻译的字符串。

    https://docs.microsoft.com/en-us/dotnet/api/system.globalization.cultureinfo.currentculture?view=netframework-4.8

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多