-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPoFileHelper.cs
More file actions
42 lines (34 loc) · 1.21 KB
/
PoFileHelper.cs
File metadata and controls
42 lines (34 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
using Karambolo.PO;
namespace PoTranslator;
public static class PoFileHelper
{
public static POCatalog Parse(string filePath) => ParlotPoParser.Parse(filePath);
public static POCatalog ParseKarambolo(string filePath)
{
using var stream = File.OpenRead(filePath);
var parser = new POParser();
var result = parser.Parse(stream);
if (!result.Success)
{
var diagnostics = result.Diagnostics;
var errors = string.Join(Environment.NewLine, diagnostics.Select(d => d.ToString()));
throw new InvalidOperationException($"Failed to parse PO file '{filePath}': {errors}");
}
return result.Catalog;
}
public static void Write(POCatalog catalog, string filePath)
{
var directory = Path.GetDirectoryName(filePath);
if (!string.IsNullOrEmpty(directory) && !Directory.Exists(directory))
{
Directory.CreateDirectory(directory);
}
var generator = new POGenerator(new POGeneratorSettings
{
IgnoreEncoding = true,
IgnoreLongLines = true,
});
using var stream = File.Create(filePath);
generator.Generate(stream, catalog);
}
}