Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
<PackageVersion Include="Mono.Addins" Version="1.4.2-alpha.4" />
<PackageVersion Include="Mono.Addins.Setup" Version="1.4.2-alpha.4" />
<PackageVersion Include="Mono.Addins.CecilReflector" Version="1.4.2-alpha.4" />
<PackageVersion Include="NGettext" Version="0.6.7" />
<PackageVersion Include="ParagonClipper" Version="6.4.2" />
<PackageVersion Include="System.CommandLine" Version="2.0.10" />
<PackageVersion Include="Tmds.DBus.Protocol" Version="0.94.2" />
Expand Down
25 changes: 15 additions & 10 deletions Pinta.Core/Classes/Translations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,37 +24,42 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

using System;
using System.Globalization;
using NGettext;

namespace Pinta.Core;

public static class Translations
{
private static ICatalog? catalog;
private const string PintaTextDomain = "pinta";

public static void Init (string domain, string locale_dir)
public static void Init (string localeDir)
{
CultureInfo cultureInfo = CultureInfo.CurrentUICulture;
catalog = new Catalog (domain, locale_dir, cultureInfo);
string lang = cultureInfo.Name.Replace ('-', '_'); // convert names like en-CA to en_CA

// The dotnet UI culture controls which translations are loaded for Pinta above.
// The GTK / libadwaita libraries use the native version of gettext for their translations
// (e.g. the About dialog), so here we set the LANG environment variable to be consistent.
// 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 ();
string lang = cultureInfo.Name.Replace ('-', '_'); // convert names like en-CA to en_CA
GLib.Functions.Setenv ("LANG", lang, overwrite: true);

// Initialize gettext for Pinta's translations.
IntlExtensions.BindTextDomain (PintaTextDomain, localeDir);
IntlExtensions.BindTextDomainCodeset (PintaTextDomain, "UTF-8");
IntlExtensions.TextDomain (PintaTextDomain);
}

public static string GetString (string text)
{
return catalog?.GetString (text) ?? text;
// Just use glib'c gettext wrapper for convenience instead of adding our own binding.
return GLib.Functions.Dgettext (PintaTextDomain, text);
}

public static string GetString (string text, params object[] args)
{
return catalog?.GetString (text, args) ?? text;
return string.Format (GetString (text), args);
}
}
22 changes: 22 additions & 0 deletions Pinta.Core/Extensions/IntlExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,26 @@ public static void BindTextDomain (string domain, string dir)
GLib.Internal.NonNullablePlatformStringUnownedHandle.Create (domain),
GLib.Internal.NonNullablePlatformStringUnownedHandle.Create (dir));
}

[LibraryImport (IntlLibraryName, EntryPoint = "bind_textdomain_codeset")]
private static partial IntPtr InternalBindTextDomainCodeset (
GLib.Internal.NonNullablePlatformStringHandle domain,
GLib.Internal.NonNullablePlatformStringHandle codeset);

public static void BindTextDomainCodeset (string domain, string codeset)
{
InternalBindTextDomainCodeset (
GLib.Internal.NonNullablePlatformStringUnownedHandle.Create (domain),
GLib.Internal.NonNullablePlatformStringUnownedHandle.Create (codeset));
}

[LibraryImport (IntlLibraryName, EntryPoint = "textdomain")]
private static partial IntPtr InternalTextDomain (
GLib.Internal.NonNullablePlatformStringHandle domain);

public static void TextDomain (string domain)
{
InternalTextDomain (
GLib.Internal.NonNullablePlatformStringUnownedHandle.Create (domain));
}
}
1 change: 0 additions & 1 deletion Pinta.Core/Pinta.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
<Import Project="../properties/ReferenceGirCorePangoCairo10.props" />

<ItemGroup>
<PackageReference Include="NGettext" />
<PackageReference Include="ParagonClipper" />
<PackageReference Include="Mono.Addins" />
</ItemGroup>
Expand Down
6 changes: 3 additions & 3 deletions Pinta/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ public static int Main (string[] args)
MacInterop.Environment.Init ();
}

string locale_dir = Path.Combine (SystemManager.GetDataRootDirectory (), "locale");
string localeDir = Path.Combine (SystemManager.GetDataRootDirectory (), "locale");

try {
Translations.Init ("pinta", locale_dir);
Translations.Init (localeDir);
} catch (Exception ex) {
Console.WriteLine (ex);
}
Expand Down Expand Up @@ -78,7 +78,7 @@ public static int Main (string[] args)
parseResult.GetValue (threads_option),
parseResult.GetValue (files_arg) ?? [],
parseResult.GetValue (debug_option),
locale_dir);
localeDir);
});

return root_command.Parse (args).Invoke ();
Expand Down
Loading