Skip to content

Commit 0335e65

Browse files
committed
Moved XLIFF constant strings to X.cs to avoid further typo.
1 parent bbb2869 commit 0335e65

File tree

3 files changed

+45
-19
lines changed

3 files changed

+45
-19
lines changed

lrx/X.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,32 @@ public static class X
3636

3737
public static readonly XName XSPACE = XNamespace.Xml + "space";
3838

39+
public static readonly XName DATATYPE = "datatype";
3940

41+
public static readonly XName ORIGINAL = "original";
42+
43+
public static readonly XName SOURCELANG = "source-language";
44+
45+
public static readonly XName TARGETLANG = "target-language";
46+
47+
public static readonly XName VERSION = "version";
48+
49+
public static readonly XName ID = "id";
50+
51+
public static readonly XName RESNAME = "resname";
52+
53+
public static readonly XName CTYPE = "context-type";
54+
55+
public const string PRESERVE = "preserve";
56+
57+
public const string LOCRES_NAME = "x-locres-namespace";
58+
59+
public const string LOCRES_KEY = "x-locres-key";
60+
61+
public const string LOCRES_HASH = "x-locres-hash";
62+
63+
public const string LOCRES_FORMAT_OLD = "x-locres-0";
64+
65+
public const string LOCRES_FORMAT_NEW = "x-locres-1";
4066
}
4167
}

lrx/XliffBuilder.cs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,27 +24,27 @@ public class XliffBuilder
2424
public void Add(string name, string key, int hash, string source, string target)
2525
{
2626
Units.Add(new XElement(X.TU,
27-
new XAttribute("id", id++),
28-
new XAttribute("resname", key),
27+
new XAttribute(X.ID, id++),
28+
new XAttribute(X.RESNAME, key),
2929
new XElement(X.SOURCE, new XAttribute(X.XLANG, SourceLang), source),
3030
(target == null ? null : new XElement(X.TARGET, new XAttribute(X.XLANG, TargetLang), target)),
3131
new XElement(X.CGROUP,
32-
(name == null ? null : new XElement(X.CONTEXT, new XAttribute("context-type", "x-locres-namespace"), name)),
33-
new XElement(X.CONTEXT, new XAttribute("context-type", "x-locres-key"), key),
34-
new XElement(X.CONTEXT, new XAttribute("context-type", "x-locres-hash"), hash.ToString("X08")))));
32+
(name == null ? null : new XElement(X.CONTEXT, new XAttribute(X.CTYPE, X.LOCRES_NAME), name)),
33+
new XElement(X.CONTEXT, new XAttribute(X.CTYPE, X.LOCRES_KEY), key),
34+
new XElement(X.CONTEXT, new XAttribute(X.CTYPE, X.LOCRES_HASH), hash.ToString("X08")))));
3535
}
3636

3737
public XElement GetDocument()
3838
{
3939
return new XElement(X.XLIFF,
40-
new XAttribute("version", "1.2"),
40+
new XAttribute(X.VERSION, "1.2"),
4141
new XAttribute(X.XNS, X.XLIFF_NS.NamespaceName),
4242
new XElement(X.FILE,
43-
new XAttribute("original", Origin),
44-
new XAttribute("source-language", SourceLang),
45-
new XAttribute("target-language", TargetLang),
46-
new XAttribute("datatype", LocResFormat == LocResFormat.New ? "x-locres-1" : "x-locres-0"),
47-
new XAttribute(X.XSPACE, "preserve"),
43+
new XAttribute(X.ORIGINAL, Origin),
44+
new XAttribute(X.SOURCELANG, SourceLang),
45+
new XAttribute(X.TARGETLANG, TargetLang),
46+
new XAttribute(X.DATATYPE, LocResFormat == LocResFormat.New ? X.LOCRES_FORMAT_NEW : X.LOCRES_FORMAT_OLD),
47+
new XAttribute(X.XSPACE, X.PRESERVE),
4848
new XElement(X.BODY,
4949
Units)));
5050
}

lrx/XliffCruncher.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,24 +23,24 @@ public void Read(string filename)
2323
public LocRes Crunch()
2424
{
2525
LocResFormat format = LocResFormat.Auto;
26-
switch ((string)Xliff.Element(X.FILE).Attribute("datatype"))
26+
switch ((string)Xliff.Element(X.FILE).Attribute(X.DATATYPE))
2727
{
28-
case "x-locres-0": format = LocResFormat.Old; break;
29-
case "x-locres-1": format = LocResFormat.New; break;
28+
case X.LOCRES_FORMAT_OLD: format = LocResFormat.Old; break;
29+
case X.LOCRES_FORMAT_NEW: format = LocResFormat.New; break;
3030
}
3131

32-
var names = Xliff.Descendants(X.TU).Select(tu => GetContext(tu, "x-locres-namespace")).Distinct().ToArray();
32+
var names = Xliff.Descendants(X.TU).Select(tu => GetContext(tu, X.LOCRES_NAME)).Distinct().ToArray();
3333
var tables = new List<LocRes.Table>(names.Length);
3434
foreach (var n in names)
3535
{
3636
var entries = new List<LocRes.Entry>();
37-
foreach (var tu in Xliff.Descendants(X.TU).Where(tu => GetContext(tu, "x-locres-namespace") == n))
37+
foreach (var tu in Xliff.Descendants(X.TU).Where(tu => GetContext(tu, X.LOCRES_NAME) == n))
3838
{
3939
var text = (string)tu.Element(X.TARGET);
4040
if (text == null) continue;
4141

42-
var key = GetContext(tu, "x-locres-key");
43-
var hash_text = GetContext(tu, "x-locres-hash");
42+
var key = GetContext(tu, X.LOCRES_KEY);
43+
var hash_text = GetContext(tu, X.LOCRES_HASH);
4444
if (key == null || hash_text == null) throw new FormatException();
4545
int hash;
4646
if (!int.TryParse(hash_text, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out hash))
@@ -64,7 +64,7 @@ public LocRes Crunch()
6464
/// <returns>Content string of a context element whose context-type matches <paramref name="context_type"/>, or null if no such context exists.</returns>
6565
private static string GetContext(XElement tu, string context_type)
6666
{
67-
return (string)tu.Element(X.CGROUP)?.Elements(X.CONTEXT)?.FirstOrDefault(c => (string)c.Attribute("context-type") == context_type);
67+
return (string)tu.Element(X.CGROUP)?.Elements(X.CONTEXT)?.FirstOrDefault(c => (string)c.Attribute(X.CTYPE) == context_type);
6868
}
6969
}
7070
}

0 commit comments

Comments
 (0)