0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Costura.Fody を使って、サテライトアセンブリ(日本語・英語・中国語)を自動的に EXE に埋め込み

Posted at

まだ未確認。。。

プロジェクト概要
• プロジェクト名:MyApp
• 開発環境:.NET 8(.NET 6〜でもOK)
• 使用ライブラリ:
• Fody
• Costura.Fody
• 多言語リソース:
• Properties/Resources.resx(デフォルト:日本語)
• Properties/Resources.en.resx
• Properties/Resources.zh.resx

Costura.Fody が自動的に:
• 生成された en\MyApp.resources.dll と zh\MyApp.resources.dll を検出
• EXE 内に埋め込みリソースとして組み込み
• 実行時に AssemblyResolve をフックして自動ロード
を行います。
(つまり、Program.cs 側で Assembly.Load のコードは不要 です)

MyApp.csproj
<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net8.0</TargetFramework>
    <RootNamespace>MyApp</RootNamespace>
    <Nullable>enable</Nullable>
    <ImplicitUsings>enable</ImplicitUsings>
  </PropertyGroup>

  <!-- Costura を使ってサテライトDLLを埋め込む設定 -->
  <ItemGroup>
    <PackageReference Include="Fody" Version="6.8.0" PrivateAssets="All" />
    <PackageReference Include="Costura.Fody" Version="5.8.0" />
  </ItemGroup>

  <!-- Costura がサテライトアセンブリも対象に含めるように設定 -->
  <ItemGroup>
    <FodyWeavers>
      <Costura>
        <IncludeAssemblies>
          MyApp
        </IncludeAssemblies>
        <IncludeSatelliteAssemblies>true</IncludeSatelliteAssemblies>
      </Costura>
    </FodyWeavers>
  </ItemGroup>

</Project>

補足:
• IncludeSatelliteAssemblies=true により、en\MyApp.resources.dll、zh\MyApp.resources.dll なども自動埋め込み対象になります。
• 出力は 単一 EXE になり、en や zh フォルダは不要になります。

Program.cs
using System;
using System.Globalization;
using System.Resources;
using System.Reflection;
using System.Threading;

namespace MyApp
{
    internal class Program
    {
        static void Main()
        {
            // Windows の表示言語を自動反映(CurrentUICultureに適用)
            CultureInfo current = CultureInfo.CurrentUICulture;
            Thread.CurrentThread.CurrentUICulture = current;

            Console.WriteLine($"Detected UI Language: {current.Name} ({current.DisplayName})");

            // ResourceManager経由で多言語文字列を取得
            var rm = new ResourceManager("MyApp.Properties.Resources", Assembly.GetExecutingAssembly());
            Console.WriteLine($"Hello message: {rm.GetString("Hello")}");

            Console.WriteLine("\n--- 明示的なテスト ---");
            Console.WriteLine("日本語: " + rm.GetString("Hello", new CultureInfo("ja")));
            Console.WriteLine("英語: " + rm.GetString("Hello", new CultureInfo("en")));
            Console.WriteLine("中国語: " + rm.GetString("Hello", new CultureInfo("zh")));

            Console.WriteLine("\nPress any key to exit...");
            Console.ReadKey();
        }
    }
}

リソースファイル構成例

MyApp/
 ├─ Program.cs
 └─ Properties/
     ├─ Resources.resx         ← デフォルト (日本語)
     ├─ Resources.en.resx      ← 英語
     ├─ Resources.zh.resx      ← 中国語
     └─ Resources.Designer.cs  ← 自動生成
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?