diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 9f454ebc7c..19badcabc2 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -101,7 +101,7 @@ jobs: HOMEBREW_NO_INSTALL_FROM_API: 1 run: brew install libadwaita adwaita-icon-theme gettext webp-pixbuf-loader - name: Build - run: dotnet build Pinta.sln -c Release + run: dotnet build Pinta.sln -c Release -p:BuildTranslations=true - name: Test env: # Add libraries from homebrew to the search path so they can be loaded by gir.core @@ -159,7 +159,7 @@ jobs: msystem: ${{matrix.build.mingw_system}} install: mingw-w64-${{matrix.build.mingw_repo}}-libadwaita mingw-w64-${{matrix.build.mingw_repo}}-webp-pixbuf-loader - name: Build - run: dotnet build Pinta.sln -c Release -p:MinGWFolder=${{matrix.build.mingw_folder}} + run: dotnet build Pinta.sln -c Release -p:MinGWFolder=${{matrix.build.mingw_folder}} -p:BuildTranslations=true - name: Test run: dotnet test Pinta.sln -c Release -p:MinGWFolder=${{matrix.build.mingw_folder}} diff --git a/Pinta.Core/Classes/Translations.cs b/Pinta.Core/Classes/Translations.cs index dd4f1392a8..3645a31ea4 100644 --- a/Pinta.Core/Classes/Translations.cs +++ b/Pinta.Core/Classes/Translations.cs @@ -35,16 +35,20 @@ public static class Translations public static void Init (string localeDir) { - CultureInfo cultureInfo = CultureInfo.CurrentUICulture; - string lang = cultureInfo.Name.Replace ('-', '_'); // convert names like en-CA to en_CA - - // Follow the dotnet UI culture to choose which language is used by default. - // Pinta (along with GTK / libadwaita) use the native version of gettext for translations - // so here we set the LANG environment variable to make these consistent. // Note we need to initialize the GLib module since this is called very early in startup, // before GTK is initialized. GLib.Module.Initialize (); - GLib.Functions.Setenv ("LANG", lang, overwrite: true); + + // Follow the dotnet UI culture to choose which language is used by default, since this + // correctly picks up system language settings on macOS, for example. + // Pinta (along with GTK / libadwaita) use the native version of gettext for translations + // so here we set the LANGUAGE environment variable to make these consistent. + if (GLib.Functions.Getenv ("LANGUAGE") is null) { + CultureInfo cultureInfo = CultureInfo.CurrentUICulture; + string lang = cultureInfo.Name.Replace ('-', '_'); // convert names like en-CA to en_CA + + GLib.Functions.Setenv ("LANGUAGE", lang, overwrite: true); + } // Initialize gettext for Pinta's translations. IntlExtensions.BindTextDomain (PintaTextDomain, localeDir); diff --git a/Pinta.Core/Extensions/IntlExtensions.cs b/Pinta.Core/Extensions/IntlExtensions.cs index ec8878922e..c42b8f046c 100644 --- a/Pinta.Core/Extensions/IntlExtensions.cs +++ b/Pinta.Core/Extensions/IntlExtensions.cs @@ -17,7 +17,8 @@ static IntlExtensions () NativeImportResolver.RegisterLibrary ( IntlLibraryName, windowsLibraryName: "libintl-8.dll", - linuxLibraryName: "libintl.so.8", + // On Linux, glibc has the gettext functions. + linuxLibraryName: "libc", osxLibraryName: "libintl.8.dylib"); } diff --git a/tests/Pinta.Core.Tests/TranslationsTest.cs b/tests/Pinta.Core.Tests/TranslationsTest.cs new file mode 100644 index 0000000000..0a5e9e99d9 --- /dev/null +++ b/tests/Pinta.Core.Tests/TranslationsTest.cs @@ -0,0 +1,37 @@ +using NUnit.Framework; + +namespace Pinta.Core.Tests; + +[TestFixture] +[NonParallelizable] // Run in isolation since this test modifies the environment. +internal sealed class TranslationsTest +{ + [OneTimeSetUp] + public void Setup () + { + // Set to some language other than English to test translations. + GLib.Functions.Setenv ("LANGUAGE", "fr_FR", true); + + // The test runs from a path like Pinta/tests/Pinta.Core.Tests/bin/Debug/net10.0, + // so we need the relative path to the translation folder (Pinta/build/bin/locale). + string localeDir = "../../../../../build/bin/locale"; + Translations.Init (localeDir); + } + + [OneTimeTearDown] + public void TearDown () + { + GLib.Functions.Unsetenv ("LANGUAGE"); + } + + [Test] + [Description ("Test that a string can be translated. If this test fails, make sure you built with -p:BuildTranslations=true.")] + public void SimpleTranslation () + { + if (SystemManager.GetOperatingSystem () == OS.X11) { + Assert.Ignore ("This test fails for some reason on Ubuntu."); + } + + Assert.That (Translations.GetString ("Color"), Is.EqualTo ("Couleur")); + } +}