diff --git a/MN.L10n.Tests/PhraseMetadataReplacerTests.cs b/MN.L10n.Tests/PhraseMetadataReplacerTests.cs new file mode 100644 index 0000000..7464ee9 --- /dev/null +++ b/MN.L10n.Tests/PhraseMetadataReplacerTests.cs @@ -0,0 +1,20 @@ +using MN.L10n.PhraseMetadata; +using Xunit; + +namespace MN.L10n.Tests; + +public class PhraseMetadataReplacerTests +{ + [Theory] + [InlineData(null, "")] + [InlineData("Deltagare", "Deltagare")] + [InlineData("Deltagare !ctx=1", "Deltagare")] + [InlineData("Deltagare !Ctx=1", "Deltagare")] + [InlineData("Deltagare!ctx=1", "Deltagare!ctx=1")] + [InlineData("Det finns en massa deltagare !ctx=1 all this text is meta", "Det finns en massa deltagare")] + public void ReplacesMetadataCorrectly(string phrase, string expected) + { + var result = PhraseMetadataReplacer.ReplaceMetadata(phrase); + Assert.Equal(expected, result); + } +} diff --git a/MN.L10n/L10n.cs b/MN.L10n/L10n.cs index edd3fbf..ef585d5 100644 --- a/MN.L10n/L10n.cs +++ b/MN.L10n/L10n.cs @@ -4,6 +4,7 @@ using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; +using MN.L10n.PhraseMetadata; namespace MN.L10n { @@ -177,7 +178,8 @@ internal L10nTranslatedString __getPhrase(string phrase, object args = null) } } - return FormatNamed(cleanedPhrase, args); + var withoutMetadata = PhraseMetadataReplacer.ReplaceMetadata(cleanedPhrase); + return FormatNamed(withoutMetadata, args); } public static bool IsPluralized(object args = null) diff --git a/MN.L10n/MN.L10n.csproj b/MN.L10n/MN.L10n.csproj index ca9b0f2..253870d 100644 --- a/MN.L10n/MN.L10n.csproj +++ b/MN.L10n/MN.L10n.csproj @@ -12,13 +12,14 @@ Translation package https://github.com/MultinetInteractive/MN.L10n git © 20XX MultiNet Interactive AB - 4.0.1 + 4.1.0 + latest True Now includes analyzer Library - 4.0.1 + 4.1.0 diff --git a/MN.L10n/PhraseMetadata/PhraseMetadataReplacer.cs b/MN.L10n/PhraseMetadata/PhraseMetadataReplacer.cs new file mode 100644 index 0000000..cedad78 --- /dev/null +++ b/MN.L10n/PhraseMetadata/PhraseMetadataReplacer.cs @@ -0,0 +1,32 @@ +#nullable enable +using System; + +namespace MN.L10n.PhraseMetadata +{ + public class PhraseMetadataReplacer + { + private const string MetaDataStartToken = " !ctx="; + + public static string ReplaceMetadata(string? phrase) + { + if (phrase == null) + { + return string.Empty; + } + + if (phrase.Length < MetaDataStartToken.Length) + { + return phrase; + } + + var metaDataStartIndex = phrase.IndexOf(MetaDataStartToken, StringComparison.OrdinalIgnoreCase); + + if (metaDataStartIndex == -1) + { + return phrase; + } + + return phrase.Substring(0, metaDataStartIndex); + } + } +}