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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
|
// Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information.
namespace WixToolset.Data
{
using System;
using System.Collections.Generic;
using SimpleJson;
using WixToolset.Data.Bind;
/// <summary>
/// Object that represents a localization file.
/// </summary>
public sealed class Localization
{
private Dictionary<string, BindVariable> variables = new Dictionary<string, BindVariable>();
private Dictionary<string, LocalizedControl> localizedControls = new Dictionary<string, LocalizedControl>();
/// <summary>
/// Instantiates a new localization object.
/// </summary>
public Localization(int codepage, string culture, IDictionary<string, BindVariable> variables, IDictionary<string, LocalizedControl> localizedControls)
{
this.Codepage = codepage;
this.Culture = culture?.ToLowerInvariant() ?? String.Empty;
this.variables = new Dictionary<string, BindVariable>(variables);
this.localizedControls = new Dictionary<string, LocalizedControl>(localizedControls);
}
/// <summary>
/// Gets the codepage.
/// </summary>
/// <value>The codepage.</value>
public int Codepage { get; private set; }
/// <summary>
/// Gets the culture.
/// </summary>
/// <value>The culture.</value>
public string Culture { get; private set; }
/// <summary>
/// Gets the variables.
/// </summary>
/// <value>The variables.</value>
public ICollection<BindVariable> Variables => this.variables.Values;
/// <summary>
/// Gets the localized controls.
/// </summary>
/// <value>The localized controls.</value>
public ICollection<KeyValuePair<string, LocalizedControl>> LocalizedControls => this.localizedControls;
internal JsonObject Serialize()
{
var jsonObject = new JsonObject
{
{ "codepage", this.Codepage },
};
jsonObject.AddIsNotNullOrEmpty("culture", this.Culture);
// Serialize bind variables.
if (this.Variables.Count > 0)
{
var variablesJson = new JsonArray(this.Variables.Count);
foreach (var variable in this.Variables)
{
var variableJson = variable.Serialize();
variablesJson.Add(variableJson);
}
jsonObject.Add("variables", variablesJson);
}
// Serialize localized control.
if (this.LocalizedControls.Count > 0)
{
var controlsJson = new JsonObject();
foreach (var controlWithKey in this.LocalizedControls)
{
var controlJson = controlWithKey.Value.Serialize();
controlsJson.Add(controlWithKey.Key, controlJson);
}
jsonObject.Add("controls", controlsJson);
}
return jsonObject;
}
internal static Localization Deserialize(JsonObject jsonObject)
{
var codepage = jsonObject.GetValueOrDefault("codepage", 0);
var culture = jsonObject.GetValueOrDefault<string>("culture");
var variables = new Dictionary<string, BindVariable>();
var variablesJson = jsonObject.GetValueOrDefault("variables", new JsonArray());
foreach (JsonObject variableJson in variablesJson)
{
var bindPath = BindVariable.Deserialize(variableJson);
variables.Add(bindPath.Id, bindPath);
}
var controls = new Dictionary<string, LocalizedControl>();
var controlsJson = jsonObject.GetValueOrDefault<JsonObject>("controls");
if (controlsJson != null)
{
foreach (var controlJsonWithKey in controlsJson)
{
var control = LocalizedControl.Deserialize((JsonObject)controlJsonWithKey.Value);
controls.Add(controlJsonWithKey.Key, control);
}
}
return new Localization(codepage, culture, variables, controls);
}
}
}
|