internal async Task <Dictionary <string, string> > AddTranslationsPackageIntoCache(string packageName)
        {
            string translations = await BlobStoreHelper.GetBlobAsString(_blobPrefix + packageName, BlobContainerType.Internal);

            DistributedCache.Set(TranslationConstants._cachePrefix + packageName, Encoding.UTF8.GetBytes(translations));

            return(JsonConvert.DeserializeObject <Dictionary <string, string> >(translations));
        }
Example #2
0
        private static void InitializeBlobStorage(TestContext context)
        {
            var blobStoreConnectionString = context.Properties["BlobStoreConnectionString"].ToString();

            _containerName = context.Properties["BlobStoreContainer"].ToString();
            CloudStorageAccount storageAccount = CloudStorageAccount.Parse(blobStoreConnectionString);
            var cloudBlobClient = storageAccount.CreateCloudBlobClient();

            _cloudBlobContainer = cloudBlobClient.GetContainerReference(_containerName);
            _cloudBlobContainer.CreateIfNotExists();

            var logMock = new Mock <IActivityLogger>();

            logMock.Setup(l => l.Write(It.IsAny <string>()));
            _blobStoreHelper = new BlobStoreHelper(logMock.Object, blobStoreConnectionString, _containerName);
        }
 public TranslationHelper(KeyVaultHelper keyVaultHelper, IDistributedCache distributedCache, BlobStoreHelper blobStoreHelper)
 {
     KeyVaultHelper   = keyVaultHelper;
     BlobStoreHelper  = blobStoreHelper;
     DistributedCache = distributedCache;
 }
Example #4
0
        static async Task Main(string[] args)
        {
            Console.WriteLine("Resolving translations");

            CultureInfo[] cultures                  = CultureInfo.GetCultures(CultureTypes.NeutralCultures);
            string        rootPath                  = AppDomain.CurrentDomain.BaseDirectory,
                          unresolvedTranslationPath = Path.Combine(rootPath, "UnResolvedTranslations"),
                          resolvedUploadPath        = Path.Combine(rootPath, "Upload", "Internal", "Translations");

            Dictionary <string, Dictionary <string, string> > fallbackTranslations = JsonConvert.DeserializeObject <Dictionary <string, Dictionary <string, string> > >(File.ReadAllText(Path.Combine(unresolvedTranslationPath, "en.json")));

            List <string> sections = new List <string>();

            foreach (string section in fallbackTranslations.Keys)
            {
                string resolvedSectionUploadPath = Path.Combine(resolvedUploadPath, section);
                if (!Directory.Exists(resolvedSectionUploadPath))
                {
                    Directory.CreateDirectory(resolvedSectionUploadPath);
                }
            }

            foreach (CultureInfo currentCulture in cultures)
            {
                string locale = currentCulture.TwoLetterISOLanguageName,
                                 unresolvedPath = Path.Combine(unresolvedTranslationPath, $"{locale}.json");

                if (locale == "")
                {
                    continue;
                }

                if (!File.Exists(unresolvedPath))
                {
                    foreach (string section in fallbackTranslations.Keys)
                    {
                        File.WriteAllText(Path.Combine(resolvedUploadPath, section, $"{locale}.json"), JsonConvert.SerializeObject(fallbackTranslations[section]));

                        sections.Add($"{section}/{locale}.json");
                    }
                }

                else
                {
                    Dictionary <string, Dictionary <string, string> > unresolvedTranslations = JsonConvert.DeserializeObject <Dictionary <string, Dictionary <string, string> > >(File.ReadAllText(unresolvedPath));

                    foreach (string section in fallbackTranslations.Keys)
                    {
                        if (!unresolvedTranslations.ContainsKey(section))
                        {
                            File.WriteAllText(Path.Combine(resolvedUploadPath, section, $"{locale}.json"), JsonConvert.SerializeObject(fallbackTranslations[section]));
                        }

                        else
                        {
                            Dictionary <string, string> fallbackTranslationsSection   = fallbackTranslations[section],
                                                        unresolvedTranslationsSection = unresolvedTranslations[section];

                            foreach (KeyValuePair <string, string> translation in fallbackTranslationsSection)
                            {
                                if (!unresolvedTranslationsSection.ContainsKey(translation.Key))
                                {
                                    unresolvedTranslationsSection.Add(translation.Key, translation.Value);
                                }
                            }

                            File.WriteAllText(Path.Combine(resolvedUploadPath, section, $"{locale}.json"), JsonConvert.SerializeObject(unresolvedTranslationsSection));
                        }

                        sections.Add($"{section}/{locale}.json");
                    }
                }
            }

            Console.WriteLine("Resolving tile definitions");

            string tileDefinitions                   = Path.Combine(rootPath, "Upload", "Public", "DataFiles", "V1"),
                   unresolvedTileDefinitions         = Path.Combine(tileDefinitions, "TileDefinition.json"),
                   resolvedTileDefinitionsUploadPath = Path.Combine(tileDefinitions, "TileSearch.json");

            IEnumerable <UnResolvedTag> tags = JsonConvert.DeserializeObject <TileConfiguration>(File.ReadAllText(unresolvedTileDefinitions)).tiles.SelectMany(t => t.tags);

            List <SelectFilter>  selectFilters  = new List <SelectFilter>();
            List <DateFilter>    dateFilters    = new List <DateFilter>();
            List <BooleanFilter> booleanFilters = new List <BooleanFilter>();

            foreach (UnResolvedTag tag in tags)
            {
                string titleTranslation = $"tag{tag.name}TitleTranslation";

                if (!string.IsNullOrWhiteSpace(tag.selectValue))
                {
                    SelectFilter       currentSelectFilter = selectFilters.FirstOrDefault(t => t.name == tag.name);
                    SelectFilterOption currentOption       = new SelectFilterOption
                    {
                        value       = tag.selectValue,
                        translation = $"tag{tag.name}Value{tag.selectValue}Translation"
                    };

                    if (currentSelectFilter == null)
                    {
                        selectFilters.Add(new SelectFilter()
                        {
                            name             = tag.name,
                            titleTranslation = titleTranslation,
                            options          = new List <SelectFilterOption>
                            {
                                currentOption
                            },
                            optionTranslations = new Dictionary <string, string>
                            {
                                {
                                    currentOption.value,
                                    currentOption.translation
                                }
                            }
                        });
                    }

                    else if (!currentSelectFilter.options.Any(t => t.value == tag.selectValue))
                    {
                        currentSelectFilter.options.Add(currentOption);
                        currentSelectFilter.optionTranslations.Add(currentOption.value, currentOption.translation);
                    }
                }
                else if (tag.dateValue.HasValue)
                {
                    DateFilter currentDateFilter = dateFilters.FirstOrDefault(t => t.name == tag.name);
                    DateTime   currentDate       = tag.dateValue.Value;

                    if (currentDateFilter == null)
                    {
                        dateFilters.Add(new DateFilter
                        {
                            name             = tag.name,
                            titleTranslation = titleTranslation,
                            minimumValue     = currentDate,
                            maximumValue     = currentDate
                        });
                    }

                    else if (currentDate < currentDateFilter.minimumValue)
                    {
                        currentDateFilter.minimumValue = currentDate;
                    }

                    else if (currentDate > currentDateFilter.maximumValue)
                    {
                        currentDateFilter.maximumValue = currentDate;
                    }
                }
                else if (tag.booleanValue.HasValue && !booleanFilters.Any(t => t.name == tag.name))
                {
                    booleanFilters.Add(new BooleanFilter
                    {
                        name             = tag.name,
                        titleTranslation = titleTranslation
                    });
                }
            }


            Dictionary <string, SelectFilter>  selectFiltersByName  = new Dictionary <string, SelectFilter>();
            Dictionary <string, DateFilter>    dateFiltersByName    = new Dictionary <string, DateFilter>();
            Dictionary <string, BooleanFilter> booleanFiltersByName = new Dictionary <string, BooleanFilter>();

            File.WriteAllText(resolvedTileDefinitionsUploadPath, JsonConvert.SerializeObject(new
            {
                selectFilters,
                selectFiltersByName = selectFilters.ToDictionary(sf => sf.name, sf => sf),
                dateFilters,
                dateFiltersByName = dateFilters.ToDictionary(sf => sf.name, sf => sf),
                booleanFilters,
                booleanFiltersByName = booleanFilters.ToDictionary(sf => sf.name, sf => sf)
            }));

            Console.WriteLine("Putting all the files into blob");

            var provider = new ServiceCollection()
                           .AddMemoryCache()
                           .BuildServiceProvider();

            var cache = provider.GetService <IMemoryCache>();

            var keyVaultHelper = new KeyVaultHelper(
                Options.Create(
                    new KeyVaultConfiguration()
            {
                BaseSecretURL = "https://cvvault.vault.azure.net/secrets/"
            }
                    ), cache);

            var blobHelper = new BlobStoreHelper(
                Options.Create(
                    new BlobStoreConfiguration()
            {
                InternalStoreSecretURL = "internalFilesStoreConnectionString",
                PublicStoreSecretURL   = "publicFilesStoreConnectionString"
            }
                    ),
                keyVaultHelper
                );

            await blobHelper.UploadFolderToBlob(Path.Combine(rootPath, "Upload", "Internal"), BlobContainerType.Internal);

            await blobHelper.UploadFolderToBlob(Path.Combine(rootPath, "Upload", "Public"), BlobContainerType.Public);

            Console.WriteLine("Sending update message to subscribers");

            await new ServiceBusHelper(
                Options.Create(
                    new ServiceBusConfiguration()
            {
                ServiceBusSecretURL = "serviceBusConnectionString"
            }
                    ),
                keyVaultHelper
                ).SendMessage(new ResetTranslationsCacheMessage(ServiceBusTopic.ResetTranslationsCache, sections));
        }