// 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; /// /// Object that represents a localization file. /// public sealed class Localization { private Dictionary variables = new Dictionary(); private Dictionary localizedControls = new Dictionary(); /// /// Instantiates a new localization object. /// public Localization(int codepage, string culture, IDictionary variables, IDictionary localizedControls) { this.Codepage = codepage; this.Culture = culture?.ToLowerInvariant() ?? String.Empty; this.variables = new Dictionary(variables); this.localizedControls = new Dictionary(localizedControls); } /// /// Gets the codepage. /// /// The codepage. public int Codepage { get; private set; } /// /// Gets the culture. /// /// The culture. public string Culture { get; private set; } /// /// Gets the variables. /// /// The variables. public ICollection Variables => this.variables.Values; /// /// Gets the localized controls. /// /// The localized controls. public ICollection> 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("culture"); var variables = new Dictionary(); 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(); var controlsJson = jsonObject.GetValueOrDefault("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); } } }