Skip to content

Commit 4b7137d

Browse files
Add support for factoring strings into multiple resources files
1 parent b2c6f49 commit 4b7137d

1 file changed

Lines changed: 37 additions & 12 deletions

File tree

WinUI3Localizer/LocalizerBuilder.cs

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,23 @@ private record StringResourceItems(string Language, IEnumerable<StringResourceIt
2121

2222
private readonly Localizer.Options options = new();
2323

24-
public static string StringResourcesFileXPath { get; set; } = "//root/data";
24+
private string defaultStringResourcesFileName = "Resources.resw";
25+
26+
private string stringResourcesFileXPath = "//root/data";
2527

2628
public static bool IsLocalizerAlreadyBuilt => Localizer.Get() is Localizer;
2729

28-
private ILogger? Logger { get; set; }
30+
public LocalizerBuilder SetDefaultStringResourcesFileName(string fileName)
31+
{
32+
this.defaultStringResourcesFileName = fileName;
33+
return this;
34+
}
35+
36+
public LocalizerBuilder SetStringResourcesFileXPath(string xPath)
37+
{
38+
this.stringResourcesFileXPath = xPath;
39+
return this;
40+
}
2941

3042
public LocalizerBuilder SetLogger(ILogger<Localizer> logger)
3143
{
@@ -44,15 +56,22 @@ public LocalizerBuilder AddStringResourcesFolderForLanguageDictionaries(
4456
{
4557
try
4658
{
47-
string languageFilePath = Path.Combine(languageFolderPath, resourcesFileName);
59+
foreach (string stringResourcesFileFullPath in Directory.GetFiles(languageFolderPath, "*.resw"))
60+
{
61+
string fileName = Path.GetFileName(stringResourcesFileFullPath);
62+
string sourceName = fileName == this.defaultStringResourcesFileName
63+
? string.Empty
64+
: Path.GetFileNameWithoutExtension(fileName);
4865

4966
if (CreateLanguageDictionaryFromStringResourcesFile(
50-
languageFilePath,
51-
StringResourcesFileXPath) is LanguageDictionary dictionary)
67+
sourceName,
68+
stringResourcesFileFullPath,
69+
this.stringResourcesFileXPath) is LanguageDictionary dictionary)
5270
{
5371
this.languageDictionaries.Add(dictionary);
5472
}
5573
}
74+
}
5675
catch
5776
{
5877
if (ignoreExceptions is false)
@@ -121,9 +140,10 @@ public async Task<ILocalizer> Build()
121140
return localizer;
122141
}
123142

124-
private static LanguageDictionary? CreateLanguageDictionaryFromStringResourcesFile(string filePath, string fileXPath)
143+
private static LanguageDictionary? CreateLanguageDictionaryFromStringResourcesFile(string sourceName, string filePath, string fileXPath)
125144
{
126145
if (CreateStringResourceItemsFromResourcesFile(
146+
sourceName,
127147
filePath,
128148
fileXPath) is StringResourceItems stringResourceItems)
129149
{
@@ -159,7 +179,7 @@ private static LanguageDictionary.Item CreateLanguageDictionaryItem(StringResour
159179
stringResourceItem.Name);
160180
}
161181

162-
private static StringResourceItems? CreateStringResourceItemsFromResourcesFile(string filePath, string xPath = "//root/data")
182+
private static StringResourceItems? CreateStringResourceItemsFromResourcesFile(string sourceName, string filePath, string xPath = "//root/data")
163183
{
164184
DirectoryInfo directoryInfo = new(filePath);
165185

@@ -171,29 +191,34 @@ private static LanguageDictionary.Item CreateLanguageDictionaryItem(StringResour
171191
if (document.SelectNodes(xPath) is XmlNodeList nodeList)
172192
{
173193
List<StringResourceItem> items = new();
174-
items.AddRange(CreateStringResourceItems(nodeList));
194+
IEnumerable<StringResourceItem> stringResourceItems = CreateStringResourceItems(sourceName, nodeList);
195+
items.AddRange(stringResourceItems);
175196
return new StringResourceItems(language, items);
176197
}
177198
}
178199

179200
return null;
180201
}
181202

182-
private static IEnumerable<StringResourceItem> CreateStringResourceItems(XmlNodeList nodeList)
203+
private static IEnumerable<StringResourceItem> CreateStringResourceItems(string sourceName, XmlNodeList nodeList)
183204
{
184205
foreach (XmlNode node in nodeList)
185206
{
186-
if (CreateStringResourceItem(node) is StringResourceItem item)
207+
if (CreateStringResourceItem(sourceName, node) is StringResourceItem item)
187208
{
188209
yield return item;
189210
}
190211
}
191212
}
192213

193-
private static StringResourceItem? CreateStringResourceItem(XmlNode node)
214+
private static StringResourceItem? CreateStringResourceItem(string sourceName, XmlNode node)
194215
{
216+
string prefix = string.IsNullOrEmpty(sourceName) is false
217+
? $"/{sourceName}/"
218+
: string.Empty;
219+
195220
return new StringResourceItem(
196-
Name: node.Attributes?["name"]?.Value ?? string.Empty,
221+
Name: $"{prefix}{node.Attributes?["name"]?.Value ?? string.Empty}",
197222
Value: node["value"]?.InnerText ?? string.Empty,
198223
Comment: string.Empty);
199224
}

0 commit comments

Comments
 (0)