| 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.Core.Link |
| 4 | { |
| 5 | using System; |
| 6 | using System.Collections.Generic; |
| 7 | using System.Linq; |
| 8 | using WixToolset.Data; |
| 9 | using WixToolset.Extensibility.Services; |
| 10 | |
| 11 | internal class CollateLocalizationsCommand |
| 12 | { |
| 13 | public CollateLocalizationsCommand(IMessaging messaging, IEnumerable<Localization> localizations) |
| 14 | { |
| 15 | this.Messaging = messaging; |
| 16 | this.Localizations = localizations; |
| 17 | } |
| 18 | |
| 19 | private IMessaging Messaging { get; } |
| 20 | |
| 21 | private IEnumerable<Localization> Localizations { get; } |
| 22 | |
| 23 | public Dictionary<string, Localization> Execute() |
| 24 | { |
| 25 | var localizationsByCulture = new Dictionary<string, Localization>(StringComparer.OrdinalIgnoreCase); |
| 26 | |
| 27 | foreach (var localization in this.Localizations) |
| 28 | { |
| 29 | if (localizationsByCulture.TryGetValue(localization.Culture, out var existingCulture)) |
| 30 | { |
| 31 | var merged = this.Merge(existingCulture, localization); |
| 32 | localizationsByCulture[localization.Culture] = merged; |
| 33 | } |
| 34 | else |
| 35 | { |
| 36 | localizationsByCulture.Add(localization.Culture, localization); |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | return localizationsByCulture; |
| 41 | } |
| 42 | |
| 43 | private Localization Merge(Localization existingLocalization, Localization localization) |
| 44 | { |
| 45 | var variables = existingLocalization.Variables.ToDictionary(v => v.Id); |
| 46 | var controls = existingLocalization.LocalizedControls.ToDictionary(kvp => kvp.Key, kvp => kvp.Value); |
| 47 | |
| 48 | foreach (var newVariable in localization.Variables) |
| 49 | { |
| 50 | if (!variables.TryGetValue(newVariable.Id, out var existingVariable) || (existingVariable.Overridable && !newVariable.Overridable)) |
| 51 | { |
| 52 | variables[newVariable.Id] = newVariable; |
| 53 | } |
| 54 | else if (!newVariable.Overridable) |
| 55 | { |
| 56 | this.Messaging.Write(ErrorMessages.DuplicateLocalizationIdentifier(newVariable.SourceLineNumbers, newVariable.Id)); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | foreach (var localizedControl in localization.LocalizedControls) |
| 61 | { |
| 62 | if (!controls.ContainsKey(localizedControl.Key)) |
| 63 | { |
| 64 | controls.Add(localizedControl.Key, localizedControl.Value); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | return new Localization(existingLocalization.Location, existingLocalization.Codepage ?? localization.Codepage, existingLocalization.SummaryInformationCodepage ?? localization.SummaryInformationCodepage, existingLocalization.Culture, variables, controls); |
| 69 | } |
| 70 | } |
| 71 | } |