Skip to content

Commit eeed3c8

Browse files
authored
Merge pull request #5 from bastianeicher/main
Drop ampersand hotkey markers and preserve string.Format placeholders during translation
2 parents d9d4df8 + 89356bd commit eeed3c8

5 files changed

Lines changed: 35 additions & 8 deletions

File tree

Action/ActionInputs.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,14 @@ public class ActionInputs : IActionInputs
1919
HelpText = "Auth key for your DeepL API access.")]
2020
public string AuthKey { get; set; } = "";
2121

22+
[Option(
23+
's',
24+
"source-lang",
25+
Required = false,
26+
Default = "",
27+
HelpText = "The language used in the original ResX files. Leave empty to auto-detect.")]
28+
public string SourceLang { get; set; } = "";
29+
2230
[Option(
2331
'e',
2432
"excludes-regex",
@@ -60,5 +68,5 @@ public class ActionInputs : IActionInputs
6068
public string LocalizationExcludes { get; set; } = "";
6169

6270
public override string ToString() =>
63-
$"{nameof(Directory)}: {Directory}, {nameof(AuthKey)}: {AuthKey}, {nameof(ExcludesRegex)}: {ExcludesRegex}, {nameof(DataCopiesRegex)}: {DataCopiesRegex}, {nameof(TakeOverridesKeysSuperSetAsKeyFilter)}: {TakeOverridesKeysSuperSetAsKeyFilter}, {nameof(LocalizationFilter)}: {LocalizationFilter}, {nameof(LocalizationExcludes)}: {LocalizationExcludes}";
71+
$"{nameof(Directory)}: {Directory}, {nameof(AuthKey)}: {AuthKey}, {nameof(SourceLang)}: {SourceLang}, {nameof(ExcludesRegex)}: {ExcludesRegex}, {nameof(DataCopiesRegex)}: {DataCopiesRegex}, {nameof(TakeOverridesKeysSuperSetAsKeyFilter)}: {TakeOverridesKeysSuperSetAsKeyFilter}, {nameof(LocalizationFilter)}: {LocalizationFilter}, {nameof(LocalizationExcludes)}: {LocalizationExcludes}";
6472
}

Dockerfile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
# Set the base image as the .NET 5.0 SDK (this includes the runtime)
2-
FROM mcr.microsoft.com/dotnet/sdk:5.0 as build-env
1+
# Set the base image as the .NET 6.0 SDK (this includes the runtime)
2+
FROM mcr.microsoft.com/dotnet/sdk:6.0 as build-env
33

44
# Copy everything and publish the release (publish implicitly restores and builds)
55
COPY . ./
@@ -16,4 +16,4 @@ LABEL com.github.actions.description="A Github action that searches for default,
1616
LABEL com.github.actions.icon="file-plus"
1717
LABEL com.github.actions.color="purple"
1818

19-
ENTRYPOINT [ "dotnet", "/Action/bin/Release/net5.0/MrMeeseeks.ResXTranslationCombinator.Action.dll" ]
19+
ENTRYPOINT [ "dotnet", "/Action/bin/Release/net6.0/MrMeeseeks.ResXTranslationCombinator.Action.dll" ]

Main/IActionInputs.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ public interface IActionInputs
44
{
55
string Directory { get; }
66
string AuthKey { get; }
7+
string SourceLang { get; }
78
string ExcludesRegex { get; }
89
string DataCopiesRegex { get; }
910
bool TakeOverridesKeysSuperSetAsKeyFilter { get; }

Main/Translation/DeepLTranslator.cs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
using System.Globalization;
2+
using System.Text.RegularExpressions;
23
using System.Threading.Tasks;
4+
using System.Web;
35
using DeepL;
46
using MrMeeseeks.ResXTranslationCombinator.Utility;
57

@@ -15,6 +17,7 @@ internal class DeepLTranslator : IDeepLTranslator
1517
private readonly ILogger _logger;
1618
private readonly Translator _deepLClient;
1719
private IImmutableSet<CultureInfo>? _cachedSupportedCultureInfos;
20+
private string? _sourceLanguage;
1821

1922
public DeepLTranslator(
2023
IActionInputs actionInputs,
@@ -23,6 +26,7 @@ public DeepLTranslator(
2326
{
2427
_logger = logger;
2528
_deepLClient = deepLClientFactory(actionInputs.AuthKey);
29+
_sourceLanguage = string.IsNullOrEmpty(actionInputs.SourceLang) ? null : actionInputs.SourceLang;
2630
}
2731

2832
public bool TranslationsShouldBeCached => true;
@@ -50,23 +54,31 @@ async Task<IImmutableSet<CultureInfo>> Inner()
5054
}
5155
}
5256

57+
private static readonly Regex HotkeyPrefixRegex = new("&([a-zA-Z0-9])", RegexOptions.Compiled);
58+
private static readonly Regex PlaceholderRegex = new("{([0-9])}", RegexOptions.Compiled);
59+
private static readonly Regex PlaceholderReverseRegex = new("<placeholder>([0-9])</placeholder>", RegexOptions.Compiled);
60+
5361
public async Task<string[]> Translate(
5462
string[] sourceTexts,
5563
CultureInfo targetLanguageCode)
5664
{
5765
try
5866
{
5967
var translations = await _deepLClient.TranslateTextAsync(
60-
sourceTexts,
61-
null,
68+
sourceTexts.Select(t => PlaceholderRegex.Replace(
69+
HttpUtility.HtmlEncode(HotkeyPrefixRegex.Replace(t, "$1")),
70+
"<placeholder>$1</placeholder>")),
71+
_sourceLanguage,
6272
targetLanguageCode.Name,
6373
new TextTranslateOptions
6474
{
65-
PreserveFormatting = true
75+
PreserveFormatting = true,
76+
TagHandling = "xml",
77+
IgnoreTags = { "placeholder" }
6678
}
6779
);
6880

69-
return translations.Select(t => t.Text).ToArray();
81+
return translations.Select(t => PlaceholderReverseRegex.Replace(HttpUtility.HtmlDecode(t.Text), "{$1}")).ToArray();
7082
}
7183
catch (Exception exception)
7284
{

action.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ inputs:
1111
auth:
1212
description: 'Auth key for your DeepL API access.'
1313
required: true
14+
source-lang:
15+
description: 'The language used in the original ResX files. Leave empty to auto-detect.'
16+
required: false
17+
default: ''
1418
excludes-regex:
1519
description: 'Regex for names of default ResX files in order to decide whether to exclude file from processing.'
1620
required: false
@@ -44,6 +48,8 @@ runs:
4448
- ${{ inputs.dir }}
4549
- '-a'
4650
- ${{ inputs.auth }}
51+
- '-s'
52+
- ${{ inputs.source-lang }}
4753
- '-e'
4854
- ${{ inputs.excludes-regex }}
4955
- '-c'

0 commit comments

Comments
 (0)