private async void GenerateTranslationCatalog(string catalogPath, OperationMode mode) { await Task.Run(() => { if (mode == OperationMode.Delete) { if (File.Exists(catalogPath)) { File.Delete(catalogPath); } return; } var translationSerializer = new DataContractJsonSerializer(typeof(TranslationCatalog), new DataContractJsonSerializerSettings { UseSimpleDictionaryFormat = true }); var translationCatalog = new TranslationCatalog(); foreach (var translationKey in typeof(TranslationKeys).GetFields() .Where(f => f.FieldType == typeof(string)) .Select(f => f.GetValue(f) as string)) { translationCatalog.AddTranslation(translationKey); } using (var stream = File.Create(catalogPath)) { translationSerializer.WriteObject(stream, translationCatalog); } }); }
public static void DeleteTranslation(this TranslationCatalog translationCatalog, string targetTranslation) { if (translationCatalog.Translations.ContainsKey(targetTranslation)) { translationCatalog.Translations.Remove(targetTranslation); } }
private void LoadTranslationCatalog(string language) { var jsonContent = File.ReadAllText( Path.Combine(Constants.LocalizationPath, $"{string.Format(Constants.TranslationJsonName, language)}")); var translationSerializer = new DataContractJsonSerializer(typeof(TranslationCatalog), new DataContractJsonSerializerSettings { UseSimpleDictionaryFormat = true }); using (var memoryStream = new MemoryStream(Encoding.Unicode.GetBytes(jsonContent))) { _translationCatalog = translationSerializer.ReadObject(memoryStream) as TranslationCatalog; } }
public static void AddTranslation(this TranslationCatalog translationCatalog, string newTranslation) { translationCatalog.Translations.Add(newTranslation, NotImplementedTranslation); }