Пример #1
0
        private TemplateInfo LocalizeTemplate(ITemplateInfo template, ILocalizationLocator localizationInfo)
        {
            TemplateInfo localizedTemplate = new TemplateInfo
            {
                GeneratorId        = template.GeneratorId,
                ConfigPlace        = template.ConfigPlace,
                ConfigMountPointId = template.ConfigMountPointId,
                Name                        = localizationInfo?.Name ?? template.Name,
                Tags                        = LocalizeCacheTags(template, localizationInfo),
                CacheParameters             = LocalizeCacheParameters(template, localizationInfo),
                ShortName                   = template.ShortName,
                Classifications             = template.Classifications,
                Author                      = localizationInfo?.Author ?? template.Author,
                Description                 = localizationInfo?.Description ?? template.Description,
                GroupIdentity               = template.GroupIdentity ?? string.Empty,
                Precedence                  = template.Precedence,
                Identity                    = template.Identity,
                DefaultName                 = template.DefaultName,
                LocaleConfigPlace           = localizationInfo?.ConfigPlace ?? null,
                LocaleConfigMountPointId    = localizationInfo?.MountPointId ?? Guid.Empty,
                HostConfigMountPointId      = template.HostConfigMountPointId,
                HostConfigPlace             = template.HostConfigPlace,
                ThirdPartyNotices           = template.ThirdPartyNotices,
                BaselineInfo                = template.BaselineInfo,
                HasScriptRunningPostActions = template.HasScriptRunningPostActions
            };

            if (template is IShortNameList templateWithShortNameList)
            {
                localizedTemplate.ShortNameList = templateWithShortNameList.ShortNameList;
            }

            return(localizedTemplate);
        }
Пример #2
0
        private TemplateInfo LocalizeTemplate(ITemplateInfo template, ILocalizationLocator localizationInfo)
        {
            TemplateInfo localizedTemplate = new TemplateInfo
            {
                GeneratorId        = template.GeneratorId,
                ConfigPlace        = template.ConfigPlace,
                ConfigMountPointId = template.ConfigMountPointId,
                Name                     = localizationInfo?.Name ?? template.Name,
                Tags                     = LocalizeCacheTags(template, localizationInfo),
                CacheParameters          = LocalizeCacheParameters(template, localizationInfo),
                ShortName                = template.ShortName,
                Classifications          = template.Classifications,
                Author                   = localizationInfo?.Author ?? template.Author,
                Description              = localizationInfo?.Description ?? template.Description,
                GroupIdentity            = template.GroupIdentity ?? string.Empty,
                Precedence               = template.Precedence,
                Identity                 = template.Identity,
                DefaultName              = template.DefaultName,
                LocaleConfigPlace        = localizationInfo?.ConfigPlace ?? null,
                LocaleConfigMountPointId = localizationInfo?.MountPointId ?? Guid.Empty,
                HostConfigMountPointId   = template.HostConfigMountPointId,
                HostConfigPlace          = template.HostConfigPlace
            };

            return(localizedTemplate);
        }
Пример #3
0
        private void AddLocalizationToMemoryCache(ILocalizationLocator locator)
        {
            if (!_localizationMemoryCache.TryGetValue(locator.Locale, out IDictionary <string, ILocalizationLocator> localeLocators))
            {
                localeLocators = new Dictionary <string, ILocalizationLocator>();
                _localizationMemoryCache.Add(locator.Locale, localeLocators);
            }

            localeLocators[locator.Identity] = locator;
        }
Пример #4
0
        private void AddLocalizationToMemoryCache(ILocalizationLocator locator)
        {
            if (!_localizationMemoryCache.TryGetValue(locator.Locale, out IDictionary <string, ILocalizationLocator> value))
            {
                value = new Dictionary <string, ILocalizationLocator>();
                _localizationMemoryCache.Add(locator.Locale, value);
            }

            value[locator.Identity] = locator;
        }
Пример #5
0
        private void WriteTemplateCacheForLocale(string locale, string existingCacheVersion)
        {
            IReadOnlyList <TemplateInfo> existingTemplatesForLocale = GetTemplatesForLocale(locale, existingCacheVersion);
            IDictionary <string, ILocalizationLocator> existingLocatorsForLocale;
            bool hasContentChanges = false;

            if (existingTemplatesForLocale.Count == 0)
            {   // the cache for this locale didn't exist previously. Start with the neutral locale as if it were the existing (no locales)
                existingTemplatesForLocale = GetTemplatesForLocale(null, existingCacheVersion);
                existingLocatorsForLocale  = new Dictionary <string, ILocalizationLocator>();
                hasContentChanges          = true;
            }
            else
            {
                existingLocatorsForLocale = GetLocalizationsFromTemplates(existingTemplatesForLocale, locale);
            }

            HashSet <string>     foundTemplates     = new HashSet <string>();
            List <ITemplateInfo> mergedTemplateList = new List <ITemplateInfo>();

            // These are from langpacks being installed... identity -> locator
            if (string.IsNullOrEmpty(locale) ||
                !_localizationMemoryCache.TryGetValue(locale, out IDictionary <string, ILocalizationLocator> newLocatorsForLocale))
            {
                newLocatorsForLocale = new Dictionary <string, ILocalizationLocator>();
            }
            else
            {
                hasContentChanges = true;   // there are new langpacks for this locale
            }

            foreach (ITemplate newTemplate in _templateMemoryCache.Values)
            {
                ILocalizationLocator locatorForTemplate = GetPreferredLocatorForTemplate(newTemplate.Identity, existingLocatorsForLocale, newLocatorsForLocale);
                TemplateInfo         localizedTemplate  = LocalizeTemplate(newTemplate, locatorForTemplate);
                mergedTemplateList.Add(localizedTemplate);
                foundTemplates.Add(newTemplate.Identity);

                hasContentChanges = true;   // new template
            }

            foreach (TemplateInfo existingTemplate in existingTemplatesForLocale)
            {
                if (!foundTemplates.Contains(existingTemplate.Identity))
                {
                    ILocalizationLocator locatorForTemplate = GetPreferredLocatorForTemplate(existingTemplate.Identity, existingLocatorsForLocale, newLocatorsForLocale);
                    TemplateInfo         localizedTemplate  = LocalizeTemplate(existingTemplate, locatorForTemplate);
                    mergedTemplateList.Add(localizedTemplate);
                    foundTemplates.Add(existingTemplate.Identity);
                }
            }

            _environmentSettings.SettingsLoader.WriteTemplateCache(mergedTemplateList, locale, hasContentChanges);
        }
Пример #6
0
        private void WriteTemplateCacheForLocale(string locale, string existingCacheVersion)
        {
            IReadOnlyList <TemplateInfo> templatesForLocale = GetTemplatesForLocale(locale, existingCacheVersion);
            bool hasContentChanges = false;
            IDictionary <string, ILocalizationLocator> existingLocatorsForLocale;

            if (templatesForLocale.Count == 0)
            {
                templatesForLocale        = GetTemplatesForLocale(null, existingCacheVersion);
                existingLocatorsForLocale = new Dictionary <string, ILocalizationLocator>();
                hasContentChanges         = true;
            }
            else
            {
                existingLocatorsForLocale = GetLocalizationsFromTemplates(templatesForLocale, locale);
            }

            HashSet <string>     hashSet = new HashSet <string>();
            List <ITemplateInfo> list    = new List <ITemplateInfo>();

            if (string.IsNullOrEmpty(locale) ||
                !_localizationMemoryCache.TryGetValue(locale, out IDictionary <string, ILocalizationLocator> value))
            {
                value = new Dictionary <string, ILocalizationLocator>();
            }
            else
            {
                hasContentChanges = true;
            }

            foreach (ITemplate value2 in _templateMemoryCache.Values)
            {
                ILocalizationLocator preferredLocatorForTemplate =
                    GetPreferredLocatorForTemplate(value2.Identity, existingLocatorsForLocale, value);
                TemplateInfo item = LocalizeTemplate(value2, preferredLocatorForTemplate);
                list.Add(item);
                hashSet.Add(value2.Identity);
                hasContentChanges = true;
            }

            foreach (TemplateInfo item3 in templatesForLocale)
            {
                if (!hashSet.Contains(item3.Identity))
                {
                    ILocalizationLocator preferredLocatorForTemplate2 =
                        GetPreferredLocatorForTemplate(item3.Identity, existingLocatorsForLocale, value);
                    TemplateInfo item2 = LocalizeTemplate(item3, preferredLocatorForTemplate2);
                    list.Add(item2);
                    hashSet.Add(item3.Identity);
                }
            }

            _environmentSettings.SettingsLoader.WriteTemplateCache(list, locale, hasContentChanges);
        }
Пример #7
0
        private static void UpdateTemplateLocalization(TemplateInfo template, IDictionary <string, ILocalizationLocator> locatorsForLocale)
        {
            ILocalizationLocator localizationInfo = null;

            if (locatorsForLocale == null ||
                !locatorsForLocale.TryGetValue(template.Identity, out localizationInfo))
            {
                return;
            }

            template.LocaleConfigPlace        = localizationInfo.ConfigPlace ?? null;
            template.LocaleConfigMountPointId = localizationInfo.MountPointId;
        }
Пример #8
0
        public void PicksCorrectLocator(string currentCulture, string?expectedLocator)
        {
            CultureInfo persistedCulture = CultureInfo.CurrentUICulture;

            try
            {
                if (currentCulture != "invariant")
                {
                    CultureInfo.CurrentUICulture = new CultureInfo(currentCulture);
                }
                else
                {
                    currentCulture = "";
                    CultureInfo.CurrentUICulture = CultureInfo.InvariantCulture;
                }
                string[] availableLocales = new[] { "cs", "de", "en", "es", "fr", "it", "ja", "ko", "pl", "pt-BR", "ru", "tr", "zh-Hans", "zh-Hant" };

                ITemplate template = A.Fake <ITemplate>();
                A.CallTo(() => template.Identity).Returns("testIdentity");
                List <ILocalizationLocator> locators = new List <ILocalizationLocator>();
                foreach (string locale in availableLocales)
                {
                    ILocalizationLocator locator = A.Fake <ILocalizationLocator>();
                    A.CallTo(() => locator.Identity).Returns("testIdentity");
                    A.CallTo(() => locator.Locale).Returns(locale);
                    A.CallTo(() => locator.Name).Returns(locale + " name");
                    locators.Add(locator);
                }
                IMountPoint mountPoint = A.Fake <IMountPoint>();
                A.CallTo(() => mountPoint.MountPointUri).Returns("testMount");

                ScanResult result = new ScanResult(mountPoint, new[] { template }, locators, Array.Empty <(string AssemblyPath, Type InterfaceType, IIdentifiedComponent Instance)>());

                TemplateCache templateCache = new TemplateCache(new[] { result }, new Dictionary <string, DateTime>(), NullLogger.Instance);

                Assert.Equal(currentCulture, templateCache.Locale);
                Assert.Equal("testIdentity", templateCache.TemplateInfo.Single().Identity);
                Assert.Equal(string.IsNullOrEmpty(expectedLocator) ? "" : expectedLocator + " name", templateCache.TemplateInfo.Single().Name);
            }
            finally
            {
                CultureInfo.CurrentUICulture = persistedCulture;
            }
        }
Пример #9
0
        // returns TemplateInfo for all the known templates.
        // if the locale is matches localization for the template, the loc info is included.
        private static IList <TemplateInfo> NewTemplateInfoForLocale(string locale)
        {
            IList <TemplateInfo> templatesForLocale = new List <TemplateInfo>();
            IDictionary <string, ILocalizationLocator> locatorsForLocale;

            if (string.IsNullOrEmpty(locale) ||
                !_localizationMemoryCache.TryGetValue(locale, out locatorsForLocale))
            {
                locatorsForLocale = null;
            }

            foreach (ITemplate template in _templateMemoryCache.Values)
            {
                ILocalizationLocator localizationInfo = null;
                if (locatorsForLocale != null)
                {
                    locatorsForLocale.TryGetValue(template.Identity, out localizationInfo);
                }

                TemplateInfo localizedTemplate = new TemplateInfo
                {
                    GeneratorId        = template.Generator.Id,
                    ConfigPlace        = template.Configuration.FullPath,
                    ConfigMountPointId = template.Configuration.MountPoint.Info.MountPointId,
                    Name                     = template.Name,
                    Tags                     = template.Tags,
                    ShortName                = template.ShortName,
                    Classifications          = template.Classifications,
                    Author                   = template.Author,
                    GroupIdentity            = template.GroupIdentity,
                    Identity                 = template.Identity,
                    DefaultName              = template.DefaultName,
                    LocaleConfigPlace        = localizationInfo?.ConfigPlace ?? null,
                    LocaleConfigMountPointId = localizationInfo?.MountPointId ?? Guid.Empty
                };

                templatesForLocale.Add(localizedTemplate);
            }

            return(templatesForLocale);
        }
Пример #10
0
        private TemplateInfo LocalizeTemplate(ITemplateInfo template, ILocalizationLocator localizationInfo)
        {
            TemplateInfo localizedTemplate = new TemplateInfo
            {
                GeneratorId        = template.GeneratorId,
                ConfigPlace        = template.ConfigPlace,
                ConfigMountPointId = template.ConfigMountPointId,
                Name                     = localizationInfo?.Name ?? template.Name,
                Tags                     = template.Tags,
                ShortName                = template.ShortName,
                Classifications          = template.Classifications,
                Author                   = localizationInfo?.Author ?? template.Author,
                Description              = localizationInfo?.Description ?? template.Description,
                GroupIdentity            = template.GroupIdentity,
                Identity                 = template.Identity,
                DefaultName              = template.DefaultName,
                LocaleConfigPlace        = localizationInfo?.ConfigPlace ?? null,
                LocaleConfigMountPointId = localizationInfo?.MountPointId ?? Guid.Empty
            };

            return(localizedTemplate);
        }
Пример #11
0
        private IReadOnlyDictionary <string, ICacheParameter> LocalizeCacheParameters(ITemplateInfo template, ILocalizationLocator localizationInfo)
        {
            if (localizationInfo == null || localizationInfo.ParameterSymbols == null)
            {
                return(template.CacheParameters);
            }

            IReadOnlyDictionary <string, ICacheParameter> templateCacheParameters = template.CacheParameters;
            IReadOnlyDictionary <string, IParameterSymbolLocalizationModel> localizedParameterSymbols = localizationInfo.ParameterSymbols;


            Dictionary <string, ICacheParameter> localizedCacheParams = new Dictionary <string, ICacheParameter>();

            foreach (KeyValuePair <string, ICacheParameter> templateParam in templateCacheParameters)
            {
                if (localizedParameterSymbols.TryGetValue(templateParam.Key, out IParameterSymbolLocalizationModel localizationForParam))
                {   // there is loc info for this symbol
                    ICacheParameter localizedParam = new CacheParameter
                    {
                        DataType     = templateParam.Value.DataType,
                        DefaultValue = templateParam.Value.DefaultValue,
                        Description  = localizationForParam.Description ?? templateParam.Value.Description
                    };

                    localizedCacheParams.Add(templateParam.Key, localizedParam);
                }
                else
                {
                    localizedCacheParams.Add(templateParam.Key, templateParam.Value);
                }
            }

            return(localizedCacheParams);
        }
Пример #12
0
        private IReadOnlyDictionary <string, ICacheTag> LocalizeCacheTags(ITemplateInfo template, ILocalizationLocator localizationInfo)
        {
            if (localizationInfo == null || localizationInfo.ParameterSymbols == null)
            {
                return(template.Tags);
            }

            IReadOnlyDictionary <string, ICacheTag> templateTags = template.Tags;
            IReadOnlyDictionary <string, IParameterSymbolLocalizationModel> localizedParameterSymbols = localizationInfo.ParameterSymbols;

            Dictionary <string, ICacheTag> localizedCacheTags = new Dictionary <string, ICacheTag>();

            foreach (KeyValuePair <string, ICacheTag> templateTag in templateTags)
            {
                if (localizedParameterSymbols.TryGetValue(templateTag.Key, out IParameterSymbolLocalizationModel localizationForTag))
                {   // there is loc for this symbol
                    Dictionary <string, string> localizedChoicesAndDescriptions = new Dictionary <string, string>();

                    foreach (KeyValuePair <string, string> templateChoice in templateTag.Value.ChoicesAndDescriptions)
                    {
                        if (localizationForTag.ChoicesAndDescriptions.TryGetValue(templateChoice.Key, out string localizedDesc) && !string.IsNullOrWhiteSpace(localizedDesc))
                        {
                            localizedChoicesAndDescriptions.Add(templateChoice.Key, localizedDesc);
                        }
                        else
                        {
                            localizedChoicesAndDescriptions.Add(templateChoice.Key, templateChoice.Value);
                        }
                    }

                    string    tagDescription = localizationForTag.Description ?? templateTag.Value.Description;
                    ICacheTag localizedTag   = new CacheTag(tagDescription, localizedChoicesAndDescriptions, templateTag.Value.DefaultValue);
                    localizedCacheTags.Add(templateTag.Key, localizedTag);
                }
                else
                {
                    localizedCacheTags.Add(templateTag.Key, templateTag.Value);
                }
            }

            return(localizedCacheTags);
        }
Пример #13
0
        private IReadOnlyDictionary <string, ICacheTag> LocalizeCacheTags(ITemplateInfo template, ILocalizationLocator localizationInfo)
        {
            if (localizationInfo == null || localizationInfo.ParameterSymbols == null)
            {
                return(template.Tags);
            }

            IReadOnlyDictionary <string, ICacheTag> templateTags = template.Tags;
            IReadOnlyDictionary <string, IParameterSymbolLocalizationModel> localizedParameterSymbols = localizationInfo.ParameterSymbols;

            Dictionary <string, ICacheTag> localizedCacheTags = new Dictionary <string, ICacheTag>();

            foreach (KeyValuePair <string, ICacheTag> templateTag in templateTags)
            {
                if (!localizedParameterSymbols.TryGetValue(templateTag.Key, out IParameterSymbolLocalizationModel localizationForTag))
                {
                    // There is no localization for this symbol. Use the symbol as is.
                    localizedCacheTags.Add(templateTag.Key, templateTag.Value);
                    continue;
                }

                // There is localization. Create a localized instance, starting with the choices.
                var localizedChoices = new Dictionary <string, ParameterChoice>();

                foreach (KeyValuePair <string, ParameterChoice> templateChoice in templateTag.Value.Choices)
                {
                    ParameterChoice localizedChoice = new ParameterChoice(
                        templateChoice.Value.DisplayName,
                        templateChoice.Value.Description);

                    if (localizationForTag.Choices.TryGetValue(templateChoice.Key, out ParameterChoiceLocalizationModel locModel))
                    {
                        localizedChoice.Localize(locModel);
                    }

                    localizedChoices.Add(templateChoice.Key, localizedChoice);
                }

                ICacheTag localizedTag = new CacheTag(
                    localizationForTag.DisplayName ?? templateTag.Value.DisplayName,
                    localizationForTag.Description ?? templateTag.Value.Description,
                    localizedChoices,
                    templateTag.Value.DefaultValue,
                    (templateTag.Value as IAllowDefaultIfOptionWithoutValue)?.DefaultIfOptionWithoutValue);

                localizedCacheTags.Add(templateTag.Key, localizedTag);
            }

            return(localizedCacheTags);
        }
Пример #14
0
        private IReadOnlyDictionary <string, ICacheParameter> LocalizeCacheParameters(ITemplateInfo template,
                                                                                      ILocalizationLocator localizationInfo)
        {
            if (localizationInfo == null || localizationInfo.ParameterSymbols == null)
            {
                return(template.CacheParameters);
            }

            IReadOnlyDictionary <string, ICacheParameter> cacheParameters = template.CacheParameters;
            IReadOnlyDictionary <string, IParameterSymbolLocalizationModel> parameterSymbols = localizationInfo.ParameterSymbols;
            Dictionary <string, ICacheParameter> dictionary = new Dictionary <string, ICacheParameter>();

            foreach (KeyValuePair <string, ICacheParameter> item in cacheParameters)
            {
                if (parameterSymbols.TryGetValue(item.Key, out IParameterSymbolLocalizationModel value))
                {
                    ICacheParameter value2 = new CacheParameter
                    {
                        DataType     = item.Value.DataType,
                        DefaultValue = item.Value.DefaultValue,
                        Description  = value.Description ?? item.Value.Description
                    };
                    dictionary.Add(item.Key, value2);
                }
                else
                {
                    dictionary.Add(item.Key, item.Value);
                }
            }

            return(dictionary);
        }
Пример #15
0
        private IReadOnlyDictionary <string, ICacheTag> LocalizeCacheTags(ITemplateInfo template, ILocalizationLocator localizationInfo)
        {
            if (localizationInfo == null || localizationInfo.ParameterSymbols == null)
            {
                return(template.Tags);
            }

            IReadOnlyDictionary <string, ICacheTag> tags = template.Tags;
            IReadOnlyDictionary <string, IParameterSymbolLocalizationModel> parameterSymbols = localizationInfo.ParameterSymbols;
            Dictionary <string, ICacheTag> dictionary = new Dictionary <string, ICacheTag>();

            foreach (KeyValuePair <string, ICacheTag> item in tags)
            {
                if (parameterSymbols.TryGetValue(item.Key, out IParameterSymbolLocalizationModel value))
                {
                    Dictionary <string, string> dictionary2 = new Dictionary <string, string>();
                    foreach (KeyValuePair <string, string> choicesAndDescription in item.Value.ChoicesAndDescriptions)
                    {
                        if (value.ChoicesAndDescriptions.TryGetValue(choicesAndDescription.Key, out string value2) &&
                            !string.IsNullOrWhiteSpace(value2))
                        {
                            dictionary2.Add(choicesAndDescription.Key, value2);
                        }
                        else
                        {
                            dictionary2.Add(choicesAndDescription.Key, choicesAndDescription.Value);
                        }
                    }

                    string description = value.Description ?? item.Value.Description;
                    IAllowDefaultIfOptionWithoutValue allowDefaultIfOptionWithoutValue = item.Value as IAllowDefaultIfOptionWithoutValue;
                    dictionary.Add(value: allowDefaultIfOptionWithoutValue == null ?
                                   new CacheTag(description, dictionary2, item.Value.DefaultValue) :
                                   new CacheTag(description,
                                                dictionary2,
                                                item.Value.DefaultValue,
                                                allowDefaultIfOptionWithoutValue.DefaultIfOptionWithoutValue),
                                   key: item.Key);
                }
                else
                {
                    dictionary.Add(item.Key, item.Value);
                }
            }

            return(dictionary);
        }
Пример #16
0
 public void AddLocalization(ILocalizationLocator locater)
 {
     _localizations.Add(locater);
 }