Skip to content

Commit f1e7af0

Browse files
committed
Alternative "translator" which just copies the values of the data
1 parent 045a53e commit f1e7af0

8 files changed

Lines changed: 85 additions & 28 deletions

File tree

Action/ActionInputs.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,13 @@ public class ActionInputs : IActionInputs
2626
Default = "",
2727
HelpText = "Regex for names of default ResX files in order to decide whether to exclude file from processing.")]
2828
public string ExcludesRegex { get; set; } = "";
29+
30+
[Option(
31+
'c',
32+
"data-copies-regex",
33+
Required = false,
34+
Default = "",
35+
HelpText = "Regex for names of default ResX files whose data should be copied instead of translated.")]
36+
public string DataCopiesRegex { get; set; } = "";
2937
}
3038
}

Action/StrongInjectContainer.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@
1111
namespace MrMeeseeks.ResXTranslationCombinator.Action
1212
{
1313
[Register(typeof(DeepLContext), typeof(IContext))]
14-
[Register(typeof(ResXTranslator), typeof(IResXTranslator))]
15-
[Register(typeof(DeepLTranslator), typeof(ITranslator))]
14+
[Register(typeof(ResXCombinator<>), typeof(IResXCombinator<>))]
15+
[Register(typeof(CopyTranslator), Scope.SingleInstance, typeof(ICopyTranslator))]
16+
[Register(typeof(DeepLTranslator), Scope.SingleInstance, typeof(IDeepLTranslator))]
1617
[Register(typeof(DataMappingFactory), typeof(IDataMappingFactory))]
1718
[Register(typeof(DataMapping), typeof(IDataMapping))]
1819

Main/IActionInputs.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@ public interface IActionInputs
55
string Directory { get; }
66
string AuthKey { get; }
77
string ExcludesRegex { get; }
8+
string DataCopiesRegex { get; }
89
}
910
}

Main/Translation/CopyTranslator.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using System.Collections.Generic;
2+
using System.Globalization;
3+
using System.Threading.Tasks;
4+
5+
namespace MrMeeseeks.ResXTranslationCombinator.Translation
6+
{
7+
internal interface ICopyTranslator : ITranslator
8+
{
9+
10+
}
11+
12+
internal class CopyTranslator : ICopyTranslator
13+
{
14+
private readonly IDeepLTranslator _deepLTranslator;
15+
16+
public CopyTranslator(IDeepLTranslator deepLTranslator) => _deepLTranslator = deepLTranslator;
17+
18+
public bool TranslationsShouldBeCached => false;
19+
public Task<HashSet<CultureInfo>> GetSupportedCultureInfos() => _deepLTranslator.GetSupportedCultureInfos();
20+
21+
public Task<string[]> Translate(
22+
string[] sourceTexts,
23+
CultureInfo targetLanguageCode) => Task.FromResult(sourceTexts);
24+
}
25+
}

Main/Translation/DeepLTranslator.cs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.Globalization;
34
using System.Linq;
45
using System.Threading.Tasks;
@@ -7,11 +8,16 @@
78

89
namespace MrMeeseeks.ResXTranslationCombinator.Translation
910
{
10-
internal class DeepLTranslator : ITranslator
11+
internal interface IDeepLTranslator : ITranslator
12+
{
13+
14+
}
15+
16+
internal class DeepLTranslator : IDeepLTranslator
1117
{
1218
private readonly ILogger _logger;
1319
private readonly DeepLClient _deepLClient;
14-
private CultureInfo[]? _cachedSupportedCultureInfos;
20+
private HashSet<CultureInfo>? _cachedSupportedCultureInfos;
1521

1622
public DeepLTranslator(
1723
IActionInputs actionInputs,
@@ -22,11 +28,13 @@ public DeepLTranslator(
2228
_deepLClient = deepLClientFactory(actionInputs.AuthKey);
2329
}
2430

25-
public async Task<CultureInfo[]> GetSupportedCultureInfos()
31+
public bool TranslationsShouldBeCached => true;
32+
33+
public async Task<HashSet<CultureInfo>> GetSupportedCultureInfos()
2634
{
2735
return _cachedSupportedCultureInfos ??= await Inner().ConfigureAwait(false);
2836

29-
async Task<CultureInfo[]> Inner()
37+
async Task<HashSet<CultureInfo>> Inner()
3038
{
3139
try
3240
{
@@ -35,7 +43,7 @@ async Task<CultureInfo[]> Inner()
3543
.Select(sl => CultureInfo.GetCultureInfo(sl.LanguageCode))
3644
.ToArray();
3745
_logger.FileLessNotice($"Currently DeepL supports following CultureInfos: {string.Join(", ", ret.Select(ci => ci.Name))}");
38-
return ret;
46+
return new HashSet<CultureInfo>(ret);
3947
}
4048
catch (Exception e)
4149
{

Main/Translation/IContext.cs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,27 +15,32 @@ public interface IContext
1515
internal class DeepLContext : IContext
1616
{
1717
private readonly IActionInputs _actionInputs;
18-
private readonly IResXTranslator _resXTranslator;
18+
private readonly IResXCombinator<IDeepLTranslator> _deepLCombinator;
19+
private readonly IResXCombinator<ICopyTranslator> _copyCombinator;
1920
private readonly ILogger _logger;
2021
private readonly Func<string, FileInfo> _fileInfoFactory;
2122
private readonly Func<string, DirectoryInfo> _directoryInfoFactory;
2223
private readonly Regex? _excludesRegex;
24+
private readonly Regex? _dataCopiesRegex;
2325

2426
public DeepLContext(
2527
IActionInputs actionInputs,
26-
IResXTranslator resXTranslator,
28+
IResXCombinator<IDeepLTranslator> deepLCombinator,
29+
IResXCombinator<ICopyTranslator> copyCombinator,
2730
ILogger logger,
2831
Func<string, FileInfo> fileInfoFactory,
2932
Func<string, DirectoryInfo> directoryInfoFactory,
3033
Func<string, Regex> regexFactory)
3134
{
3235
_actionInputs = actionInputs;
33-
_resXTranslator = resXTranslator;
36+
_deepLCombinator = deepLCombinator;
37+
_copyCombinator = copyCombinator;
3438
_logger = logger;
3539
_fileInfoFactory = fileInfoFactory;
3640
_directoryInfoFactory = directoryInfoFactory;
3741

38-
_excludesRegex = string.IsNullOrWhiteSpace(actionInputs.ExcludesRegex) ? null : regexFactory(actionInputs.ExcludesRegex) ;
42+
_excludesRegex = string.IsNullOrWhiteSpace(actionInputs.ExcludesRegex) ? null : regexFactory(actionInputs.ExcludesRegex);
43+
_dataCopiesRegex = string.IsNullOrWhiteSpace(actionInputs.DataCopiesRegex) ? null : regexFactory(actionInputs.DataCopiesRegex);
3944
}
4045

4146
public async Task TraverseAndTranslate()
@@ -54,7 +59,12 @@ public async Task TraverseAndTranslate()
5459
.Select(ToFileInfo)
5560
.Where(IsDefaultResxFileName)
5661
.Where(IsNotExcluded))
57-
await _resXTranslator.Translate(defaultResXFile).ConfigureAwait(false);
62+
{
63+
await (_dataCopiesRegex?.IsMatch(defaultResXFile.Name) ?? false
64+
? _copyCombinator.Translate(defaultResXFile)
65+
: _deepLCombinator.Translate(defaultResXFile))
66+
.ConfigureAwait(false);
67+
}
5868

5969
_logger.SetOutput("summary-title", "Localization");
6070
_logger.SetOutput("summary-details", "Summary");

Main/Translation/ITranslator.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1+
using System.Collections.Generic;
12
using System.Globalization;
23
using System.Threading.Tasks;
34

45
namespace MrMeeseeks.ResXTranslationCombinator.Translation
56
{
67
internal interface ITranslator
78
{
8-
Task<CultureInfo[]> GetSupportedCultureInfos();
9+
bool TranslationsShouldBeCached { get; }
10+
Task<HashSet<CultureInfo>> GetSupportedCultureInfos();
911
Task<string[]> Translate(
1012
string[] sourceTexts,
1113
CultureInfo targetCulture);
Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Collections.Immutable;
4-
using System.Globalization;
54
using System.IO;
65
using System.Linq;
76
using System.Threading.Tasks;
@@ -11,7 +10,7 @@
1110

1211
namespace MrMeeseeks.ResXTranslationCombinator.Translation
1312
{
14-
public interface IResXTranslator
13+
internal interface IResXCombinator<T> where T : ITranslator
1514
{
1615
Task Translate(FileInfo defaultResXFile);
1716
}
@@ -22,7 +21,7 @@ public enum ResXFileType
2221
ManuallyOverriden
2322
}
2423

25-
internal class ResXTranslator : IResXTranslator
24+
internal class ResXCombinator<T> : IResXCombinator<T> where T : ITranslator
2625
{
2726
private readonly ITranslator _translator;
2827
private readonly Func<FileInfo, IResXWriterFactory> _resXWriterFactoryFactory;
@@ -31,8 +30,8 @@ internal class ResXTranslator : IResXTranslator
3130
private readonly IDataMappingFactory _dataMappingFactory;
3231
private readonly ILogger _logger;
3332

34-
public ResXTranslator(
35-
ITranslator translator,
33+
public ResXCombinator(
34+
T translator,
3635
Func<FileInfo, IResXWriterFactory> resXWriterFactoryFactory,
3736
Func<(string Name, string Value, string Comment), IResXNode> resXNodeFactory,
3837
Func<string, FileInfo> fileInfoFactory,
@@ -59,7 +58,7 @@ public async Task Translate(FileInfo defaultResXFile)
5958

6059
var orderedDefaultKeys = dataMapping.Default.Keys.ToImmutableSortedSet();
6160

62-
var supportedCultureInfos = new HashSet<CultureInfo>(await _translator.GetSupportedCultureInfos().ConfigureAwait(false));
61+
var supportedCultureInfos = await _translator.GetSupportedCultureInfos().ConfigureAwait(false);
6362

6463
// Update automatics
6564
foreach (var supportedCultureInfo in supportedCultureInfos)
@@ -87,17 +86,20 @@ public async Task Translate(FileInfo defaultResXFile)
8786
var file = _fileInfoFactory(Path.Combine(defaultResXFile.DirectoryName ?? "",
8887
$"{defaultResXFile.Name[..defaultResXFile.Name.IndexOf('.')]}.{supportedCultureInfo.Name}.a{defaultResXFile.Extension}"));
8988
_logger.Notice(file, "New translations added");
90-
91-
var resXResourceWriter = placeholder.Create(file);
92-
foreach (var keyValuePair in acc.OrderBy(kvp => kvp.Key))
89+
90+
if (_translator.TranslationsShouldBeCached)
9391
{
94-
var resXDataNode = _resXNodeFactory((
95-
keyValuePair.Key,
96-
keyValuePair.Value,
97-
"Automatically Translated"));
98-
resXResourceWriter.AddResource(resXDataNode);
92+
var resXResourceWriter = placeholder.Create(file);
93+
foreach (var keyValuePair in acc.OrderBy(kvp => kvp.Key))
94+
{
95+
var resXDataNode = _resXNodeFactory((
96+
keyValuePair.Key,
97+
keyValuePair.Value,
98+
"Automatically Translated"));
99+
resXResourceWriter.AddResource(resXDataNode);
100+
}
101+
resXResourceWriter.Generate();
99102
}
100-
resXResourceWriter.Generate();
101103
}
102104
}
103105

0 commit comments

Comments
 (0)