浏览代码

Fixes #3611. Localization not working on self-contained single-file.

BDisp 1 年之前
父节点
当前提交
39b39ddc70

+ 18 - 1
SelfContained/Program.cs

@@ -1,6 +1,7 @@
 // This is a simple example application for a self-contained single file.
 
 using System.Diagnostics.CodeAnalysis;
+using System.Globalization;
 using Terminal.Gui;
 
 namespace SelfContained;
@@ -10,7 +11,23 @@ public static class Program
     [RequiresUnreferencedCode ("Calls Terminal.Gui.Application.Run<T>(Func<Exception, Boolean>, ConsoleDriver)")]
     private static void Main (string [] args)
     {
-        Application.Run<ExampleWindow> ().Dispose ();
+        Application.Init ();
+
+        if (Equals (Thread.CurrentThread.CurrentUICulture, CultureInfo.InvariantCulture))
+        {
+            System.Diagnostics.Debug.Assert (Application.SupportedCultures.Count == 0);
+        }
+        else
+        {
+            System.Diagnostics.Debug.Assert (Application.SupportedCultures.Count == 4);
+            System.Diagnostics.Debug.Assert (Equals (CultureInfo.CurrentCulture, Thread.CurrentThread.CurrentUICulture));
+        }
+
+        ExampleWindow app = new ();
+        Application.Run (app);
+
+        // Dispose the app object before shutdown
+        app.Dispose ();
 
         // Before the application exits, reset Terminal.Gui for clean shutdown
         Application.Shutdown ();

+ 1 - 1
SelfContained/SelfContained.csproj

@@ -8,7 +8,7 @@
     <PublishTrimmed>true</PublishTrimmed>
     <TrimMode>Link</TrimMode>
     <PublishSingleFile>true</PublishSingleFile>
-    <InvariantGlobalization>true</InvariantGlobalization>
+    <InvariantGlobalization>false</InvariantGlobalization>
     <DebugType>embedded</DebugType>
   </PropertyGroup>
 

+ 32 - 10
Terminal.Gui/Application/Application.cs

@@ -48,7 +48,7 @@ public static partial class Application
 
     internal static List<CultureInfo> GetSupportedCultures ()
     {
-        CultureInfo [] culture = CultureInfo.GetCultures (CultureTypes.AllCultures);
+        CultureInfo [] cultures = CultureInfo.GetCultures (CultureTypes.AllCultures);
 
         // Get the assembly
         var assembly = Assembly.GetExecutingAssembly ();
@@ -57,15 +57,37 @@ public static partial class Application
         string assemblyLocation = AppDomain.CurrentDomain.BaseDirectory;
 
         // Find the resource file name of the assembly
-        var resourceFilename = $"{Path.GetFileNameWithoutExtension (AppContext.BaseDirectory)}.resources.dll";
-
-        // Return all culture for which satellite folder found with culture code.
-        return culture.Where (
-                              cultureInfo =>
-                                  Directory.Exists (Path.Combine (assemblyLocation, cultureInfo.Name))
-                                  && File.Exists (Path.Combine (assemblyLocation, cultureInfo.Name, resourceFilename))
-                             )
-                      .ToList ();
+        var resourceFilename = $"{assembly.GetName ().Name}.resources.dll";
+
+        if (cultures.Length > 1 && Directory.Exists (Path.Combine (assemblyLocation, "pt-PT")))
+        {
+            // Return all culture for which satellite folder found with culture code.
+            return cultures.Where (
+                                  cultureInfo =>
+                                      Directory.Exists (Path.Combine (assemblyLocation, cultureInfo.Name))
+                                      && File.Exists (Path.Combine (assemblyLocation, cultureInfo.Name, resourceFilename))
+                                 )
+                          .ToList ();
+        }
+
+        // It's called from a self-contained single.file.
+        try
+        {
+            // <InvariantGlobalization>false</InvariantGlobalization>
+            return
+            [
+                new ("fr-FR"),
+                new ("ja-JP"),
+                new ("pt-PT"),
+                new ("zh-Hans")
+            ];
+        }
+        catch (CultureNotFoundException)
+        {
+            // <InvariantGlobalization>true</InvariantGlobalization>
+            // Only the invariant culture is supported in globalization-invariant mode.
+            return [];
+        }
     }
 
     // When `End ()` is called, it is possible `RunState.Toplevel` is a different object than `Top`.

+ 1 - 0
UnitTests/Application/ApplicationTests.cs

@@ -193,6 +193,7 @@ public class ApplicationTests
             // Internal properties
             Assert.False (Application._initialized);
             Assert.Equal (Application.GetSupportedCultures (), Application.SupportedCultures);
+            Assert.Equal (4, Application.SupportedCultures.Count);
             Assert.False (Application._forceFakeConsole);
             Assert.Equal (-1, Application._mainThreadId);
             Assert.Empty (Application._topLevels);