main
cs 170 lines 6.4 KB
Raw
1 // 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.
2
3 namespace WixToolset.Data
4 {
5 using System;
6 using System.Collections.Generic;
7 using SimpleJson;
8 using WixToolset.Data.Bind;
9
10 /// <summary>
11 /// Object that represents a localization file.
12 /// </summary>
13 public sealed class Localization
14 {
15 private readonly Dictionary<string, BindVariable> variables = new Dictionary<string, BindVariable>();
16 private readonly Dictionary<string, LocalizedControl> localizedControls = new Dictionary<string, LocalizedControl>();
17
18 /// <summary>
19 /// Instantiates a new localization object with default location.
20 /// </summary>
21 public Localization(int? codepage, int? summaryInformationCodepage, string culture, IDictionary<string, BindVariable> variables, IDictionary<string, LocalizedControl> localizedControls) :
22 this(LocalizationLocation.Source, codepage, summaryInformationCodepage, culture, variables, localizedControls)
23 {
24 }
25
26 /// <summary>
27 /// Instantiates a new localization object.
28 /// </summary>
29 public Localization(LocalizationLocation location, int? codepage, int? summaryInformationCodepage, string culture, IDictionary<string, BindVariable> variables, IDictionary<string, LocalizedControl> localizedControls)
30 {
31 this.Location = location;
32 this.Codepage = codepage;
33 this.SummaryInformationCodepage = summaryInformationCodepage;
34 this.Culture = culture?.ToLowerInvariant() ?? String.Empty;
35 this.variables = new Dictionary<string, BindVariable>(variables);
36 this.localizedControls = new Dictionary<string, LocalizedControl>(localizedControls);
37 }
38
39 /// <summary>
40 /// Gets the location the localization came from.
41 /// </summary>
42 public LocalizationLocation Location { get; private set; }
43
44 /// <summary>
45 /// Gets the codepage.
46 /// </summary>
47 /// <value>The codepage.</value>
48 public int? Codepage { get; private set; }
49
50 /// <summary>
51 /// Gets the summary information codepage.
52 /// </summary>
53 /// <value>The summary information codepage.</value>
54 public int? SummaryInformationCodepage { get; private set; }
55
56 /// <summary>
57 /// Gets the culture.
58 /// </summary>
59 /// <value>The culture.</value>
60 public string Culture { get; private set; }
61
62 /// <summary>
63 /// Gets the variables.
64 /// </summary>
65 /// <value>The variables.</value>
66 public ICollection<BindVariable> Variables => this.variables.Values;
67
68 /// <summary>
69 /// Gets the localized controls.
70 /// </summary>
71 /// <value>The localized controls.</value>
72 public ICollection<KeyValuePair<string, LocalizedControl>> LocalizedControls => this.localizedControls;
73
74 /// <summary>
75 /// Updates the location, if the location is a higher state than the current state.
76 /// </summary>
77 /// <param name="location">Location to update to.</param>
78 /// <returns>This localization object.</returns>
79 public Localization UpdateLocation(LocalizationLocation location)
80 {
81 if (this.Location < location)
82 {
83 this.Location = location;
84 }
85
86 return this;
87 }
88
89 internal JsonObject Serialize()
90 {
91 var jsonObject = new JsonObject()
92 {
93 { "location", this.Location.ToString().ToLowerInvariant() }
94 };
95
96 if (this.Codepage.HasValue)
97 {
98 jsonObject.Add("codepage", this.Codepage.Value);
99 }
100
101 if (this.SummaryInformationCodepage.HasValue)
102 {
103 jsonObject.Add("summaryCodepage", this.SummaryInformationCodepage.Value);
104 }
105
106 jsonObject.AddIsNotNullOrEmpty("culture", this.Culture);
107
108 // Serialize bind variables.
109 if (this.Variables.Count > 0)
110 {
111 var variablesJson = new JsonArray(this.Variables.Count);
112
113 foreach (var variable in this.Variables)
114 {
115 var variableJson = variable.Serialize();
116
117 variablesJson.Add(variableJson);
118 }
119
120 jsonObject.Add("variables", variablesJson);
121 }
122
123 // Serialize localized control.
124 if (this.LocalizedControls.Count > 0)
125 {
126 var controlsJson = new JsonObject();
127
128 foreach (var controlWithKey in this.LocalizedControls)
129 {
130 var controlJson = controlWithKey.Value.Serialize();
131
132 controlsJson.Add(controlWithKey.Key, controlJson);
133 }
134
135 jsonObject.Add("controls", controlsJson);
136 }
137
138 return jsonObject;
139 }
140
141 internal static Localization Deserialize(JsonObject jsonObject)
142 {
143 var location = jsonObject.GetEnumOrDefault("location", LocalizationLocation.Source);
144 var codepage = jsonObject.GetValueOrDefault("codepage", null);
145 var summaryCodepage = jsonObject.GetValueOrDefault("summaryCodepage", null);
146 var culture = jsonObject.GetValueOrDefault<string>("culture");
147
148 var variables = new Dictionary<string, BindVariable>();
149 var variablesJson = jsonObject.GetValueOrDefault("variables", new JsonArray());
150 foreach (JsonObject variableJson in variablesJson)
151 {
152 var bindPath = BindVariable.Deserialize(variableJson);
153 variables.Add(bindPath.Id, bindPath);
154 }
155
156 var controls = new Dictionary<string, LocalizedControl>();
157 var controlsJson = jsonObject.GetValueOrDefault<JsonObject>("controls");
158 if (controlsJson != null)
159 {
160 foreach (var controlJsonWithKey in controlsJson)
161 {
162 var control = LocalizedControl.Deserialize((JsonObject)controlJsonWithKey.Value);
163 controls.Add(controlJsonWithKey.Key, control);
164 }
165 }
166
167 return new Localization(location, codepage, summaryCodepage, culture, variables, controls);
168 }
169 }
170 }