-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'pipe-support-for-enum-msgid-issue-37'
This closes #37
- Loading branch information
Showing
9 changed files
with
154 additions
and
117 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |