Skip to content
Merged
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
20 changes: 20 additions & 0 deletions MN.L10n.Tests/PhraseMetadataReplacerTests.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
4 changes: 3 additions & 1 deletion MN.L10n/L10n.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using MN.L10n.PhraseMetadata;

namespace MN.L10n
{
Expand Down Expand Up @@ -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)
Expand Down
5 changes: 3 additions & 2 deletions MN.L10n/MN.L10n.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@ Translation package</Description>
<PackageProjectUrl>https://github.com/MultinetInteractive/MN.L10n</PackageProjectUrl>
<RepositoryType>git</RepositoryType>
<Copyright>© 20XX MultiNet Interactive AB</Copyright>
<Version>4.0.1</Version>
<Version>4.1.0</Version>
<LangVersion>latest</LangVersion>
<AutoIncrementPackageRevision>True</AutoIncrementPackageRevision>
<PackageReleaseNotes>Now includes analyzer</PackageReleaseNotes>
<ApplicationIcon />
<OutputType>Library</OutputType>
<StartupObject></StartupObject>
<AssemblyVersion>4.0.1</AssemblyVersion>
<AssemblyVersion>4.1.0</AssemblyVersion>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
Expand Down
32 changes: 32 additions & 0 deletions MN.L10n/PhraseMetadata/PhraseMetadataReplacer.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
Loading