コード例 #1
0
        /// <summary>
        /// Save app texts to resource files
        /// </summary>
        /// <param name="allResourceTexts">The texts to be saved</param>
        public async Task SaveServiceTexts(Dictionary <string, Dictionary <string, Designer.Models.TextResourceElement> > allResourceTexts)
        {
            // Language, key, TextResourceElement
            var resourceTexts = new Dictionary <string, Dictionary <string, Designer.Models.TextResourceElement> >();

            foreach (KeyValuePair <string, Dictionary <string, Designer.Models.TextResourceElement> > text in allResourceTexts)
            {
                string textResourceElementId = text.Key;
                foreach (KeyValuePair <string, Designer.Models.TextResourceElement> localizedText in text.Value)
                {
                    string language = localizedText.Key;
                    Designer.Models.TextResourceElement textResourceElement = localizedText.Value;
                    if (!resourceTexts.ContainsKey(language))
                    {
                        resourceTexts.Add(language, new Dictionary <string, Designer.Models.TextResourceElement>());
                    }

                    if (!resourceTexts[language].ContainsKey(textResourceElementId))
                    {
                        resourceTexts[language].Add(textResourceElementId, new Designer.Models.TextResourceElement {
                            Id = textResourceElementId, Value = textResourceElement.Value, Variables = textResourceElement.Variables
                        });
                    }
                }
            }

            string textResourcesDirectory = Path.Combine(CONFIG_FOLDER_PATH, LANGUAGE_RESOURCE_FOLDER_NAME);

            // loop through each language set of text resources
            foreach (KeyValuePair <string, Dictionary <string, Designer.Models.TextResourceElement> > processedResource in resourceTexts)
            {
                var textResource = new Designer.Models.TextResource
                {
                    Language  = processedResource.Key,
                    Resources = new List <Designer.Models.TextResourceElement>()
                };

                foreach (KeyValuePair <string, Designer.Models.TextResourceElement> actualResource in processedResource.Value)
                {
                    textResource.Resources.Add(actualResource.Value);
                }

                string resourceString = JsonConvert.SerializeObject(textResource, new JsonSerializerSettings {
                    Formatting = Newtonsoft.Json.Formatting.Indented, NullValueHandling = NullValueHandling.Ignore
                });
                string resourceFilePath = $"{textResourcesDirectory}/resource.{processedResource.Key}.json";
                await WriteTextByRelativePathAsync(resourceFilePath, resourceString, true);
            }
        }
コード例 #2
0
        private static void GetTextResourceForLanguage(Dictionary <string, Dictionary <string, Designer.Models.TextResourceElement> > allResourceTexts, Designer.Models.TextResource textResource, string language)
        {
            foreach (Designer.Models.TextResourceElement textResourceElement in textResource.Resources)
            {
                string key   = textResourceElement.Id;
                string value = textResourceElement.Value;

                if (key == null && value == null)
                {
                    continue;
                }

                if (!allResourceTexts.ContainsKey(key))
                {
                    allResourceTexts.Add(key, new Dictionary <string, Designer.Models.TextResourceElement>());
                }

                if (!allResourceTexts[key].ContainsKey(language))
                {
                    allResourceTexts[key].Add(language, textResourceElement);
                }
            }
        }