Skip to content

Commit

Permalink
Merge branch 'pipe-support-for-enum-msgid-issue-37'
Browse files Browse the repository at this point in the history
This closes #37
  • Loading branch information
robert-j-engdahl committed Mar 18, 2019
2 parents 03c3408 + 9754fc4 commit 0373011
Show file tree
Hide file tree
Showing 9 changed files with 154 additions and 117 deletions.
3 changes: 3 additions & 0 deletions NGettext.Wpf.Example/ExampleEnum.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ public enum ExampleEnum
[EnumMsgId("Some third value")]
SomeThirdValue,

[EnumMsgId("EnumMsgId example|Some fourth value")]
SomeFourthValue,

SomeValueWithoutEnumMsgId,
}
}
Binary file modified NGettext.Wpf.Example/Locale/da-DK/LC_MESSAGES/Example.mo
Binary file not shown.
18 changes: 14 additions & 4 deletions NGettext.Wpf.Example/Locale/da-DK/LC_MESSAGES/Example.po
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ msgid ""
msgstr ""
"Project-Id-Version: \n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2019-02-24 23:17+0100\n"
"PO-Revision-Date: 2019-02-24 23:17+0100\n"
"Last-Translator: Robert Engdahl <rje@accuratech.dk>\n"
"POT-Creation-Date: 2019-03-18 08:39+0100\n"
"PO-Revision-Date: 2019-03-18 08:40+0100\n"
"Last-Translator: Robert Jørgensgaard Engdahl <rje@accuratech.dk>\n"
"Language-Team: \n"
"Language: da_DK\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"X-Generator: Poedit 2.1.1\n"
"X-Generator: Poedit 2.2.1\n"
"X-Poedit-Basepath: ../../..\n"
"Plural-Forms: nplurals=2; plural=(n != 1);\n"
"X-Poedit-KeywordsList: Localizer.Noop\n"
Expand Down Expand Up @@ -100,6 +100,11 @@ msgid_plural "Plural {0:n3}"
msgstr[0] "Ental {0:n3}"
msgstr[1] "Flertal {0:n3}"

#: C:\Git\ngettext-wpf\NGettext.Wpf.Example\ExampleEnum.cs:16
msgctxt "EnumMsgId example"
msgid "Some fourth value"
msgstr "En fjerde værdi"

#: C:\Git\ngettext-wpf\NGettext.Wpf.Example\ExampleEnum.cs:10
msgid "Some other value"
msgstr "En anden værdi"
Expand Down Expand Up @@ -171,3 +176,8 @@ msgstr "Eksempel på variation i store og små bogstaver:"
#~ "Plural-Forms: nplurals=2; plural=(n != 1);\n"
#~ "X-Poedit-KeywordsList: Localizer.Noop\n"
#~ "X-Poedit-SearchPath-0: .\n"

#, fuzzy
#~ msgctxt "EnumMsgId example"
#~ msgid "Some third value"
#~ msgstr "En tredje værdi"
Binary file modified NGettext.Wpf.Example/Locale/de-DE/LC_MESSAGES/Example.mo
Binary file not shown.
18 changes: 14 additions & 4 deletions NGettext.Wpf.Example/Locale/de-DE/LC_MESSAGES/Example.po
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ msgid ""
msgstr ""
"Project-Id-Version: \n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2019-02-24 23:17+0100\n"
"PO-Revision-Date: 2019-02-24 23:18+0100\n"
"Last-Translator: Robert Engdahl <rje@accuratech.dk>\n"
"POT-Creation-Date: 2019-03-18 08:39+0100\n"
"PO-Revision-Date: 2019-03-18 08:40+0100\n"
"Last-Translator: Robert Jørgensgaard Engdahl <rje@accuratech.dk>\n"
"Language-Team: \n"
"Language: de_DE\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"X-Generator: Poedit 2.1.1\n"
"X-Generator: Poedit 2.2.1\n"
"X-Poedit-Basepath: ../../..\n"
"Plural-Forms: nplurals=2; plural=(n != 1);\n"
"X-Poedit-KeywordsList: Localizer.Noop\n"
Expand Down Expand Up @@ -100,6 +100,11 @@ msgid_plural "Plural {0:n3}"
msgstr[0] "Singular {0:n3}"
msgstr[1] "Plural {0:n3}"

#: C:\Git\ngettext-wpf\NGettext.Wpf.Example\ExampleEnum.cs:16
msgctxt "EnumMsgId example"
msgid "Some fourth value"
msgstr "Ein vierter Wert"

#: C:\Git\ngettext-wpf\NGettext.Wpf.Example\ExampleEnum.cs:10
msgid "Some other value"
msgstr "Ein anderer Wert"
Expand Down Expand Up @@ -171,3 +176,8 @@ msgstr "Beispiel von Variation zwischen Groß- und Kleinbuchstaben:"
#~ "Plural-Forms: nplurals=2; plural=(n != 1);\n"
#~ "X-Poedit-KeywordsList: Localizer.Noop\n"
#~ "X-Poedit-SearchPath-0: .\n"

#, fuzzy
#~ msgctxt "EnumMsgId example"
#~ msgid "Some third value"
#~ msgstr "Ein dritter Wert"
2 changes: 1 addition & 1 deletion NGettext.Wpf.Example/UpdateTranslations.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ XGetText-Xaml -o obj/xamlmessages.pot -k Gettext,GettextFormatConverter @(Get-Ch
Get-ChildItem -Recurse -File -Filter *.cs | Where { $_.FullName -NotLike '*\obj\*' } | ForEach-Object { $_.FullName } | Out-File -Encoding ascii "obj\csharpfiles"

# Extract msgids from cs files in project into pot file
xgettext.exe --force-po --from-code UTF-8 --language=c# -o obj/csmessages.pot -k_ -kNoop:1g -kEnumMsgId --keyword=Catalog.GetString --keyword=PluralGettext:2,3 --files-from=obj\csharpfiles
xgettext.exe --force-po --from-code UTF-8 --language=c# -o obj/csmessages.pot -k_ -kNoop:1g -kEnumMsgId:1g --keyword=Catalog.GetString --keyword=PluralGettext:2,3 --files-from=obj\csharpfiles

# Merge two pot files into one
msgcat.exe --use-first -o obj/result.pot obj/csmessages.pot obj/xamlmessages.pot
Expand Down
14 changes: 14 additions & 0 deletions NGettext.Wpf.Tests/EnumTranslation/EnumLocalizerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ public enum TestEnum
[EnumMsgId("another enum value")]
AnotherEnumValue,

[EnumMsgId("some context|a third enum value")]
ThirdEnumValue,

EnumValueWithoutMsgId,
}

Expand All @@ -37,6 +40,17 @@ public void Translates_MsgId_Of_Enum_Value(TestEnum enumValue, string msgId)
Assert.Equal("expected translation", actual);
}

[Theory]
[InlineData(TestEnum.ThirdEnumValue, "some context", "a third enum value")]
public void Translates_MsgId_Of_Enum_Value_With_Context(TestEnum enumValue, string context, string msgId)
{
_localizer.Catalog.GetParticularString(Arg.Is(context), Arg.Is(msgId)).Returns("expected translation");

var actual = _target.LocalizeEnum(enumValue);

Assert.Equal("expected translation", actual);
}

[Fact]
public void Translates_Enum_Value_Without_MsgId_To_Value_Name()
{
Expand Down
2 changes: 1 addition & 1 deletion NGettext.Wpf/EnumTranslation/EnumLocalizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public string LocalizeEnum(Enum value)
return enumMemberName;
}

return _localizer.Catalog.GetString(msgIdAttribute.MsgId);
return _localizer.Gettext(msgIdAttribute.MsgId);
}
}
}
214 changes: 107 additions & 107 deletions NGettext.Wpf/Localizer.cs
Original file line number Diff line number Diff line change
@@ -1,108 +1,108 @@
using System;
using System.Globalization;
using System.IO;

namespace NGettext.Wpf
{
public interface ILocalizer
{
ICatalog Catalog { get; }
ICultureTracker CultureTracker { get; }
}

public class Localizer : IDisposable, ILocalizer
{
private readonly string _domainName;

public Localizer(ICultureTracker cultureTracker, string domainName)
{
_domainName = domainName;
CultureTracker = cultureTracker;
if (cultureTracker == null) throw new ArgumentNullException(nameof(cultureTracker));
cultureTracker.CultureChanging += ResetCatalog;
ResetCatalog(cultureTracker.CurrentCulture);
}

private void ResetCatalog(object sender, CultureEventArgs e)
{
ResetCatalog(e.CultureInfo);
}

private void ResetCatalog(CultureInfo cultureInfo)
{
var localeDir = "Locale";
Console.WriteLine(
$"NGettext.Wpf: Attempting to load \"{Path.GetFullPath(Path.Combine(localeDir, cultureInfo.Name, "LC_MESSAGES", _domainName + ".mo"))}\"");
Catalog = new Catalog(_domainName, localeDir, cultureInfo);
}

public ICatalog Catalog { get; private set; }
public ICultureTracker CultureTracker { get; }

public void Dispose()
{
CultureTracker.CultureChanging -= ResetCatalog;
}
}

public static class LocalizerExtensions
{
private struct MsgIdWithContext
{
internal string Context { get; set; }
internal string MsgId { get; set; }
}

private static MsgIdWithContext ConvertToMsgIdWithContext(string msgId)
{
var result = new MsgIdWithContext { MsgId = msgId };

if (msgId.Contains("|"))
{
var pipePosition = msgId.IndexOf('|');
result.Context = msgId.Substring(0, pipePosition);
result.MsgId = msgId.Substring(pipePosition + 1);
}

return result;
}

internal static string Gettext(this ILocalizer @this, string msgId, params object[] values)
{
if (msgId is null) return null;

var msgIdWithContext = ConvertToMsgIdWithContext(msgId);

if (@this is null)
{
CompositionRoot.WriteMissingInitializationErrorMessage();
return string.Format(msgIdWithContext.MsgId, values);
}

if (msgIdWithContext.Context != null)
{
return @this.Catalog.GetParticularString(msgIdWithContext.Context, msgIdWithContext.MsgId, values);
}
return @this.Catalog.GetString(msgIdWithContext.MsgId, values);
}

internal static string Gettext(this ILocalizer @this, string msgId)
{
if (msgId is null) return null;

var msgIdWithContext = ConvertToMsgIdWithContext(msgId);

if (@this is null)
{
CompositionRoot.WriteMissingInitializationErrorMessage();
return msgIdWithContext.MsgId;
}

if (msgIdWithContext.Context != null)
{
return @this.Catalog.GetParticularString(msgIdWithContext.Context, msgIdWithContext.MsgId);
}
return @this.Catalog.GetString(msgIdWithContext.MsgId);
}
}
using System;
using System.Globalization;
using System.IO;

namespace NGettext.Wpf
{
public interface ILocalizer
{
ICatalog Catalog { get; }
ICultureTracker CultureTracker { get; }
}

public class Localizer : IDisposable, ILocalizer
{
private readonly string _domainName;

public Localizer(ICultureTracker cultureTracker, string domainName)
{
_domainName = domainName;
CultureTracker = cultureTracker;
if (cultureTracker == null) throw new ArgumentNullException(nameof(cultureTracker));
cultureTracker.CultureChanging += ResetCatalog;
ResetCatalog(cultureTracker.CurrentCulture);
}

private void ResetCatalog(object sender, CultureEventArgs e)
{
ResetCatalog(e.CultureInfo);
}

private void ResetCatalog(CultureInfo cultureInfo)
{
var localeDir = "Locale";
Console.WriteLine(
$"NGettext.Wpf: Attempting to load \"{Path.GetFullPath(Path.Combine(localeDir, cultureInfo.Name, "LC_MESSAGES", _domainName + ".mo"))}\"");
Catalog = new Catalog(_domainName, localeDir, cultureInfo);
}

public ICatalog Catalog { get; private set; }
public ICultureTracker CultureTracker { get; }

public void Dispose()
{
CultureTracker.CultureChanging -= ResetCatalog;
}
}

public static class LocalizerExtensions
{
private struct MsgIdWithContext
{
internal string Context { get; set; }
internal string MsgId { get; set; }
}

private static MsgIdWithContext ConvertToMsgIdWithContext(string msgId)
{
var result = new MsgIdWithContext { MsgId = msgId };

if (msgId.Contains("|"))
{
var pipePosition = msgId.IndexOf('|');
result.Context = msgId.Substring(0, pipePosition);
result.MsgId = msgId.Substring(pipePosition + 1);
}

return result;
}

internal static string Gettext(this ILocalizer @this, string msgId, params object[] values)
{
if (msgId is null) return null;

var msgIdWithContext = ConvertToMsgIdWithContext(msgId);

if (@this is null)
{
CompositionRoot.WriteMissingInitializationErrorMessage();
return string.Format(msgIdWithContext.MsgId, values);
}

if (msgIdWithContext.Context != null)
{
return @this.Catalog.GetParticularString(msgIdWithContext.Context, msgIdWithContext.MsgId, values);
}
return @this.Catalog.GetString(msgIdWithContext.MsgId, values);
}

internal static string Gettext(this ILocalizer @this, string msgId)
{
if (msgId is null) return null;

var msgIdWithContext = ConvertToMsgIdWithContext(msgId);

if (@this is null)
{
CompositionRoot.WriteMissingInitializationErrorMessage();
return msgIdWithContext.MsgId;
}

if (msgIdWithContext.Context != null)
{
return @this.Catalog.GetParticularString(msgIdWithContext.Context, msgIdWithContext.MsgId);
}
return @this.Catalog.GetString(msgIdWithContext.MsgId);
}
}
}

0 comments on commit 0373011

Please sign in to comment.