Skip to content

Archway-SharedLib/Nut.ResxBridge

Repository files navigation

Nut.ResxBridge

CI NuGet NuGet

Resxファイルから型付けされたクラスを生成します。

Resx source:

リソースファイル

Generated source:

/// <auto-generated>
/// This code was generated by Nut.ResxBridge.
/// </auto-generated>

using System;
using System.Resources;
using System.Runtime.CompilerServices;
using System.CodeDom.Compiler;

namespace ConsoleApp1.Resources
{
    [CompilerGenerated]
    [GeneratedCode("Nut.ResxBridge", "0.3.0")]
    internal static partial class Strings
    {
        private static ResourceManager resourceManager = new ResourceManager(typeof(ConsoleApp1.Resources.Strings));

        private static string GetResourceString(string resourceKey, string defaultString = null)
        {
            string resourceString = null;
            try
            {
                resourceString = resourceManager.GetString(resourceKey);
            }
            catch (MissingManifestResourceException) { }

            if (defaultString != null && resourceKey.Equals(resourceString))
            {
                return defaultString;
            }

            return resourceString;
        }

        private static string Format(string resourceFormat, params object[] args)
        {
            if (args != null)
            {
                return string.Format(resourceFormat, args);
            }

            return resourceFormat;
        }

        /// <summary>
        /// Looks up a localized string similar to Key 3.
        /// </summary>
        public static string Key_3 => GetResourceString(@"Key 3", @"Key 3");

        /// <summary>
        /// Looks up a localized string similar to Key 4.
        /// </summary>
        public static string Key_4 => GetResourceString(@"Key""4", @"Key 4");

        /// <summary>
        /// Looks up a localized string similar to Key 2 &lt;test&gt;Foo&lt;/test&gt;.
        /// </summary>
        public static string Key_2 => GetResourceString(@"Key.2", @"Key 2 <test>Foo</test>");

        /// <summary>
        /// Looks up a localized string similar to Key 1.
        /// </summary>
        public static string Key1 => GetResourceString(@"Key1", @"Key 1");

        /// <summary>
        /// Looks up a localized string similar to {0}{1}&quot;{2} Method.
        /// </summary>
        public static string Method1(object _0, object _1, object _2)
             => Format(GetResourceString(@"Method1", @"{0}{1}""{2} Method"), 
                 _0, _1, _2);

        /// <summary>
        /// Looks up a localized string similar to Value&quot;1.
        /// </summary>
        public static string Val1 => GetResourceString(@"Val1", @"Value""1");

    }
}

Publicクラスを生成する

既定ではinternalなクラスを生成します。publicなクラスを生成する場合は、次のようにプロジェクトファイルに設定を行ってください。 リソースの設定をいったん消したうえで、ResxBridge_Modifierpublicに指定したリソースの設定を追加しています。

<ItemGroup>
    <EmbeddedResource Remove="Resources\PublicStrings.resx" />
    <EmbeddedResource Include="Resources\PublicStrings.resx" ResxBridge_Modifier="public" />
</ItemGroup>

制限事項

  • クラス名にできないファイル名では生成されません。
    • e.g. Strings.ja.resx
  • ファイル名と同じキーの項目は生成されません。
  • 値がないキーの項目は生成されません。(将来修正予定)
  • プロパティ/メソッド名にできない文字が含まれるキーは、NGな文字が自動的に_に置き換えられます。
    • または先頭に_が付加されます。