IEnumerator InitLocalizationData(SystemLanguage newLanguage = SystemLanguage.Unknown, bool notify = false) { // clear the vocabulary vocabulary = new Dictionary <string, string>(); currentLanguage = ConformSystemLanguage(newLanguage); string languageName = currentLanguage.ToString(); //Debug.Log("Application language is " + languageName); if (availableLocalizations.Contains(currentLanguage)) { int entriesRead = 0; string localizationFileName = LocalizationFileName(currentLanguage); StartCoroutine(LoadStreamableAsset(localizationFileName, (stream, exists) => { if (!exists) { //Debug.Log("No localization files for " + languageName + " found"); } else { stream.BaseStream.Seek(0, SeekOrigin.Begin); while (!stream.EndOfStream) { string srcline = stream.ReadLine(); if (srcline != null) { // explicit JSON parsing LocalizationEntry entry = JsonUtility.FromJson <LocalizationEntry>(srcline); if (entry.language == languageName) { if (!vocabulary.ContainsKey(entry.id)) { vocabulary.Add(entry.id, NormalizeString(entry.value)); entriesRead++; } else { //Debug.Log("Entry " + entry.id + " is already in vocabulary!"); } } } } } })); while (!IsLoaded) { yield return(null); } //Debug.Log(entriesRead.ToString() + " entries read for " + languageName + " language"); if (notify) { GlobalManager.MInstantMessage.DeliverMessage(InstantMessageType.LanguageChanged, this, currentLanguage); } } else { //Debug.Log("No localization files for " + languageName + " found"); } processFlag = true; }
/// <summary> /// Render razor template using model /// </summary> /// <typeparam name="T"></typeparam> /// <param name="entry"></param> /// <param name="model"></param> /// <returns></returns> public static string RenderTemplate <T>(this LocalizationEntry entry, T model) { if (entry == null) { return(null); } if (model == null) { return(entry.Value); } var engine = new RazorLightEngineBuilder() .UseMemoryCachingProvider() .Build(); var key = $"{entry?.LocalizationCollection?.LocalizationCategory?.Name}-{entry?.LocalizationCollection?.Name}-{entry?.LocalizationKey?.Name}"; string result = engine .CompileRenderAsync(key, entry.Value, model) .GetAwaiter() .GetResult(); result = HttpUtility.HtmlDecode(result); return(result); }
private static bool AddOrUpdateEntry(Guid currentUserId, Localization model, LocalizationEntry loadedEntry) { bool entriesUpdated; if (loadedEntry.UserId == Guid.Empty) { loadedEntry.UserId = currentUserId; } LocalizationEntry modelEntry = model.Entries.FirstOrDefault(x => x.TermId == loadedEntry.TermId && x.UserId == currentUserId && x.Language == loadedEntry.Language); if (modelEntry == null) { model.Entries.Add(loadedEntry); entriesUpdated = true; } else { loadedEntry.Id = modelEntry.Id; loadedEntry.CreateDate = modelEntry.CreateDate; loadedEntry.UserId = modelEntry.UserId; modelEntry.Value = loadedEntry.Value; entriesUpdated = true; } return(entriesUpdated); }
public DomainActionPerformed AddEntry(Guid projectId, LocalizationEntry entry) { IQueryable <LocalizationEntry> existing = repository.GetEntries(projectId, entry.Language, entry.TermId); bool oneIsMine = existing.Any(x => x.UserId == entry.UserId); if (oneIsMine) { entry.Id = existing.First(x => x.UserId == entry.UserId).Id; repository.UpdateEntry(projectId, entry); return(DomainActionPerformed.Update); } else { entry.Value = entry.Value.Trim(); bool existsWithSameValue = existing.Any(x => x.Value.Equals(entry.Value)); if (!existing.Any() || !existsWithSameValue) { repository.AddEntry(projectId, entry); return(DomainActionPerformed.Create); } } return(DomainActionPerformed.None); }
public Option(XmlElement element) { Localization = new LocalizationEntry { Key = element.GetAttribute("LocalizationKey"), Localization = element.GetAttribute("Localization"), File = LocalizationFiles.DropDownFile }; Trigger = element.InnerText; }
void LocalizationEntry_ParseNode_False(string xml) { var xmlDoc = new XmlDocument(); xmlDoc.LoadXml(xml); Assert.False(LocalizationEntry.ParseNode(xmlDoc.LastChild, out var localizationEntry)); Assert.True(localizationEntry == default); }
public void RejectEntry(Guid projectId, Guid entryId) { LocalizationEntry entry = repository.GetEntry(projectId, entryId); if (entry != null) { entry.Accepted = false; repository.UpdateEntry(projectId, entry); } }
void LocalizationEntry_ParseLocalizationFile() { var files = GetAllLocalizationFiles(); foreach (string file in files) { string xml = File.ReadAllText(file); Assert.True(LocalizationEntry.ParseLocalizationFile(xml).Any()); } }
public SimpleCustomCheckBoxVisualElement(string variable, string icon, string localization, string positiveTrigger, string negativeTrigger) : base(variable) { Icon = icon; Localization = new LocalizationEntry { Key = variable, Localization = localization, File = LocalizationFiles.TraitFile }; NegativeTrigger = negativeTrigger; PositiveTrigger = positiveTrigger; Variable = variable; }
public CustomCheckBoxVisualGroup(XmlElement element) { Variable = element.GetAttribute(nameof(Variable)); Elements = element.ChildNodes.OfType <XmlElement>().Select(t => Trait.All[t.GetAttribute("Name")]).Cast <SimpleCheckBoxVisualElement>().ToArray(); Icon = element.GetAttribute(nameof(Icon)); Localization = new LocalizationEntry() { Key = Variable, Localization = element.GetAttribute(nameof(Localization)), File = LocalizationFiles.TraitFile }; All[Variable] = this; }
void LocalizationEntry_ParseNode_True() { var validXmls = GetValidXmls(); foreach (string xml in validXmls) { var xmlDoc = new XmlDocument(); xmlDoc.LoadXml(xml); Assert.True(LocalizationEntry.ParseNode(xmlDoc.LastChild, out var localizationEntry)); Assert.False(localizationEntry == default); } }
public SixPackLayout(XmlElement element) { Name = element.GetAttribute("LocalizationKey"); Localization = new LocalizationEntry() { Key = Name, Localization = element.GetAttribute("Localization"), File = LocalizationFiles.TraitFile }; Elements = element.ChildNodes.OfType <XmlElement>() .Select(x => SimpleCheckBoxVisualElement.All[x.GetAttribute("Name")]) .Cast <ICheckBoxVisualElement>() .ToArray(); }
public async Task <bool> AddEntry(Guid translationProjectId, LocalizationEntry entry) { entry.Id = Guid.NewGuid(); FilterDefinition <Localization> filter = Builders <Localization> .Filter.Where(x => x.Id == translationProjectId); UpdateDefinition <Localization> add = Builders <Localization> .Update.AddToSet(c => c.Entries, entry); UpdateResult result = await DbSet.UpdateOneAsync(filter, add); return(result.IsAcknowledged && result.MatchedCount > 0); }
public void UpdateEntry(Guid translationProjectId, LocalizationEntry entry) { FilterDefinition <Localization> filter = Builders <Localization> .Filter.And( Builders <Localization> .Filter.Eq(x => x.Id, translationProjectId), Builders <Localization> .Filter.ElemMatch(x => x.Entries, x => x.Id == entry.Id)); UpdateDefinition <Localization> update = Builders <Localization> .Update .Set(c => c.Entries[-1].Value, entry.Value) .Set(c => c.Entries[-1].Language, entry.Language) .Set(c => c.Entries[-1].Accepted, entry.Accepted); Context.AddCommand(() => DbSet.UpdateOneAsync(filter, update)); }
IEnumerable <LocalizationEntry> LoadLocalizationEntries() { var files = new DirectoryInfo(LocalizationFolderName).GetFiles("text_*.xml", SearchOption.AllDirectories); foreach (var file in files) { string text = File.ReadAllText(file.FullName); var fileEntries = LocalizationEntry.ParseLocalizationFile(text); foreach (var fileEntry in fileEntries) { yield return(fileEntry); } } }
bool ScanLocalizationEntry(LocalizationEntry entry) { bool valid = true; if (!Guid.TryParse(entry.GUID, out Guid _)) { entry.GUIDIsValid = valid = false; } if (string.IsNullOrWhiteSpace(entry.Speaker)) { entry.SpeakerIsValid = valid = false; } return(valid); }
public void DeserializeExtraData(BinaryReader reader) { Entries = new List <LocalizationEntry>(LanguageCount); for (int i = 0; i < LanguageCount; i++) { var entry = new LocalizationEntry(); ushort textLength = reader.ReadUInt16(); entry.Text = Encoding.UTF8.GetString(reader.ReadBytesStrict(textLength)); ushort noteLength = reader.ReadUInt16(); entry.Notes = Encoding.UTF8.GetString(reader.ReadBytesStrict(noteLength)); entry.Flags = reader.ReadByte(); Entries.Add(entry); } }
private static void MergeDictionaries(LocalizationDictionary current, LocalizationDictionary modified, bool addContext) { int added = 0; int deleted = 0; int updated = 0; foreach (var key in modified.Keys.ToList()) { if (!current.ContainsKey(key)) { Logger.Write("+ " + key); added++; current[key] = new LocalizationEntry() { Text = modified[key].Text, Context = modified[key].Context }; } else { var currentEntry = current[key]; var newEntry = modified[key]; if (currentEntry.Context != newEntry.Context) { Logger.Write("Context updated for: " + key); updated++; currentEntry.Context = newEntry.Context; } } if (!addContext) { current[key].Context = ""; } } foreach (var key in current.Keys.ToList()) { if (!modified.ContainsKey(key) && !LocalizationDictionary.IsComment(key)) { Logger.Write("- " + key); deleted++; current.Remove(key); } } Logger.Write("Added {0}\nDeleted {1}\nContext updated {2}", added, deleted, updated); }
/// <summary> /// Inserts new localization entry /// </summary> /// <param name="entry"></param> public void InsertLocalizationEntry(LocalizationEntry entry) { using (var connection = new SqlConnection(Configuration.ConnectionString)) { connection.Open(); var sql = $@" INSERT INTO [{Configuration.LocalizationEntryTableName}] ([Value], LocalizationCollectionId, LocalizationKeyId, LocalizationLanguageId, CreatedOn, UpdatedOn) VALUES(@Entry, {entry.LocalizationCollectionId}, {entry.LocalizationKeyId}, {entry.LocalizationLanguageId}, '{entry.CreatedOn:s}', '{entry.UpdatedOn:s}') "; var command = new SqlCommand(sql, connection); command.Parameters.Add("Entry", SqlDbType.NVarChar); command.Parameters["Entry"].Value = entry.Value; command.ExecuteNonQuery(); } }
public DropDown(XmlElement element) { Name = element.GetAttribute(nameof(Name)); DefaultValue = element.GetAttribute(nameof(DefaultValue)).ToInt(); IsSpecial = element.GetAttribute(nameof(IsSpecial)).ToBool(); Localization = new LocalizationEntry { Key = element.GetAttribute("LocalizationKey"), Localization = element.GetAttributeNode("Localization")?.Value, File = LocalizationFiles.DropDownFile }; Options = element.ChildNodes.OfType <XmlElement>().Select(e => new Option(e) { Owner = this }).ToArray(); All.Add(this); if (IsSpecial) { ISavable.All.Add(this); } }
private static string GenerateLanguageXml(Localization project, SupportedLanguage language, bool fillGaps) { StringBuilder sb = new StringBuilder(); sb.AppendLine("<resources>"); sb.AppendLine(); sb.AppendLine(String.Format("<string id=\"lang_name\">{0}</string>", language.ToDisplayName())); sb.AppendLine(String.Format("<string id=\"lang_index\">{0}</string>", (int)language)); sb.AppendLine(); for (int i = 0; i < project.Terms.Count; i++) { string langValue = null; LocalizationTerm term = project.Terms.ElementAt(i); IOrderedEnumerable <LocalizationEntry> entries = project.Entries.Where(x => x.TermId == term.Id && x.Language == language).OrderBy(x => x.Accepted); if (entries.Any()) { LocalizationEntry lastAccepted = entries.LastOrDefault(x => !x.Accepted.HasValue || x.Accepted == true); if (lastAccepted != null) { langValue = lastAccepted.Value; } } else { if (fillGaps) { langValue = term.Value; } } if (!string.IsNullOrWhiteSpace(langValue)) { sb.AppendLine(String.Format("<string id=\"{0}\">{1}</string>", term.Key, langValue)); } } sb.AppendLine(); sb.AppendLine("</resources>"); return(sb.ToString()); }
void LocalizationEntry_Compare_True() { var validXmls = GetValidXmls(); foreach (string xml in validXmls) { var xmlDoc1 = new XmlDocument(); xmlDoc1.LoadXml(xml); Assert.True(LocalizationEntry.ParseNode(xmlDoc1.LastChild, out var localizationEntry1)); Assert.False(localizationEntry1 == default); var xmlDoc2 = new XmlDocument(); xmlDoc2.LoadXml(xml); Assert.True(LocalizationEntry.ParseNode(xmlDoc2.LastChild, out var localizationEntry2)); Assert.False(localizationEntry2 == default); Assert.True(localizationEntry1 == localizationEntry2); } }
public OperationResultVo SaveEntry(Guid currentUserId, Guid projectId, bool currentUserIsOwner, bool currentUserHelped, LocalizationEntryViewModel vm) { int pointsEarned = 0; try { LocalizationEntry entry = mapper.Map <LocalizationEntry>(vm); DomainActionPerformed actionPerformed = translationDomainService.AddEntry(projectId, entry); if (actionPerformed == DomainActionPerformed.None) { return(new OperationResultVo("Another user already sent that translation!")); } if (!currentUserIsOwner && actionPerformed == DomainActionPerformed.Create) { pointsEarned += gamificationDomainService.ProcessAction(currentUserId, PlatformAction.LocalizationHelp); } unitOfWork.Commit(); vm.Id = entry.Id; if (!currentUserHelped && !currentUserIsOwner) { bool badgeUpdated = gamificationDomainService.SetBadgeOccurence(currentUserId, BadgeType.Babel, projectId); if (badgeUpdated) { unitOfWork.Commit(); } } UserProfile profile = GetCachedProfileByUserId(entry.UserId); vm.AuthorName = profile.Name; return(new OperationResultVo <LocalizationEntryViewModel>(vm, pointsEarned, "Translation saved!")); } catch (Exception ex) { return(new OperationResultVo(ex.Message)); } }
public void SaveEntries(Guid projectId, IEnumerable <LocalizationEntry> entries) { List <LocalizationEntry> existingEntrys = repository.GetEntries(projectId).ToList(); foreach (LocalizationEntry entry in entries) { LocalizationEntry existing = existingEntrys.FirstOrDefault(x => x.TermId == entry.TermId && x.UserId == entry.UserId && x.Language == entry.Language); if (existing == null) { entry.CreateDate = DateTime.Now; repository.AddEntry(projectId, entry); } else { existing.Value = entry.Value; existing.LastUpdateDate = DateTime.Now; repository.UpdateEntry(projectId, existing); } } }
/// <summary> /// Updates an existing localizaiton entry /// </summary> /// <param name="entry"></param> public void UpdateLocalizationEntry(LocalizationEntry entry) { using (var connection = new SqlConnection(Configuration.ConnectionString)) { connection.Open(); var sql = $@" UPDATE [{Configuration.LocalizationEntryTableName}] SET [Value] = @Entry, [LocalizationLanguageId] = {entry.LocalizationLanguageId}, [LocalizationKeyId] = {entry.LocalizationKeyId}, [LocalizationCollectionId] = {entry.LocalizationCollectionId}, [UpdatedOn] = '{entry.UpdatedOn:s}' WHERE [Id] = {entry.Id} "; var command = new SqlCommand(sql, connection); command.Parameters.Add("Entry", SqlDbType.NVarChar); command.Parameters["Entry"].Value = entry.Value; command.ExecuteNonQuery(); } }
static void Main(string[] args) { LocalizationEntry entry = new LocalizationEntry() { CatalogName = "Catalog", Identifier = "Id", Translations = { { "PL", "jabłko" }, { "EN", "apple" }, { "DE", "apfel" } } }; using (MemoryStream stream = new MemoryStream()) { XmlSerializer serializer = new XmlSerializer(typeof(LocalizationEntry)); serializer.Serialize(stream, entry); stream.Seek(0, SeekOrigin.Begin); LocalizationEntry deserializedEntry = (LocalizationEntry)serializer.Deserialize(stream); serializer.Serialize(Console.Out, deserializedEntry); } }
private static void FillEntries(DataTable dataTable, List <LocalizationTerm> terms, List <LocalizationEntry> entries, IEnumerable <KeyValuePair <int, SupportedLanguage> > columns) { foreach (DataRow row in dataTable.Rows) { object term = row.ItemArray[0]; foreach (KeyValuePair <int, SupportedLanguage> col in columns) { int colNumber = col.Key - 1; if (col.Key > row.ItemArray.Length) { break; } object translation = row.ItemArray[colNumber]; if (term == null || string.IsNullOrWhiteSpace(term.ToString()) || translation == null || string.IsNullOrWhiteSpace(translation.ToString())) { continue; } LocalizationTerm existingTerm = terms.FirstOrDefault(x => SanitizeKey(x.Key).Equals(SanitizeKey(term.ToString()))); if (existingTerm != null) { LocalizationEntry newEntry = new LocalizationEntry { TermId = existingTerm.Id, Value = translation.ToString().Trim(), Language = col.Value }; entries.Add(newEntry); } } } }
public LocalizationEntry GetEntry(Guid projectId, Guid entryId) { LocalizationEntry entry = DbSet.AsQueryable().Where(x => x.Id == projectId).SelectMany(x => x.Entries).FirstOrDefault(x => x.Id == entryId); return(entry); }
/// <summary> /// Updates an existing localizaiton entry /// </summary> /// <param name="entry"></param> public void UpdateLocalizationEntry(LocalizationEntry entry) { throw new NotSupportedException(); }
/// <summary> /// Sets new translation for the provided key and language (or current culture language if not provided) /// </summary> /// <param name="key"></param> /// <param name="value"></param> /// <param name="collection"></param> /// <param name="category"></param> /// <param name="culture"></param> public void Set(string key, string value, string collection, string category, CultureInfo culture = null) { lock (Lock) { // Get the localization key Id. If it does not exist, create it first var localizationKey = LocalizationKeys.FirstOrDefault(x => x.Name.ToLower() == key.ToLower()); if (localizationKey == null) { localizationKey = new LocalizationKey { Name = key }; Provider.InsertLocalizationKey(localizationKey); LocalizationKeys = Provider.LoadLocalizationKeys(); localizationKey = LocalizationKeys.FirstOrDefault(x => x.Name.ToLower() == key.ToLower()); } if (localizationKey == null) { return; } // Get the localization language. If it does not exist, create it first var langName = GetLanguageNameFromCulture(culture).ToLower(); var langDisplayName = GetLanguageDisplayNameFromCulture(culture); var localizationLanguage = LocalizationLanguages.FirstOrDefault(x => x.Name.ToLower() == GetLanguageNameFromCulture(culture).ToLower()); if (localizationLanguage == null) { localizationLanguage = new LocalizationLanguage { Name = langName, Value = langDisplayName }; Provider.InsertLocalizationLanguage(localizationLanguage); LocalizationLanguages = Provider.LoadLocalizationLanguages(); localizationLanguage = LocalizationLanguages.FirstOrDefault(x => x.Name.ToLower() == GetLanguageNameFromCulture(culture).ToLower()); } if (localizationLanguage == null) { return; } // Get the localization collection. If it does not exist, create it first var localizationCategory = LocalizationCategories.FirstOrDefault(x => x.Name.ToLower() == category.ToLower()); if (localizationCategory == null) { localizationCategory = new LocalizationCategory { Name = category }; Provider.InsertOrUpdateLocalizationCategory(localizationCategory); LocalizationCategories = Provider.LoadLocalizationCategories(); localizationCategory = LocalizationCategories.FirstOrDefault(x => x.Name.ToLower() == category.ToLower()); } if (localizationCategory == null) { return; } // Get the localization collection. If it does not exist, create it first var localizationCollection = LocalizationCollections.FirstOrDefault(x => x.Name.ToLower() == collection.ToLower() && x.LocalizationCategoryId == localizationCategory.Id); if (localizationCollection == null) { localizationCollection = new LocalizationCollection { Name = collection, LocalizationCategoryId = localizationCategory.Id }; Provider.InsertOrUpdateLocalizationCollection(localizationCollection); LocalizationCollections = Provider.LoadLocalizationCollections(); localizationCollection = LocalizationCollections.FirstOrDefault(x => x.Name.ToLower() == collection.ToLower() && x.LocalizationCategoryId == localizationCategory.Id); } if (localizationCollection == null) { return; } localizationCollection.LocalizationCategory = localizationCategory; localizationCollection.LocalizationCategoryId = localizationCategory.Id; // Don't check the collection since only one combination of key and language is allowed across collections var entry = LocalizationEntries.FirstOrDefault(x => x.LocalizationKeyId == localizationKey.Id && x.LocalizationLanguageId == localizationLanguage.Id); if (entry == null) { // Create entry = new LocalizationEntry { Value = value, LocalizationCollectionId = localizationCollection.Id, LocalizationKeyId = localizationKey.Id, LocalizationLanguageId = localizationLanguage.Id, CreatedOn = DateTime.UtcNow, UpdatedOn = DateTime.UtcNow }; Provider.InsertLocalizationEntry(entry); LocalizationEntries = Provider.LoadLocalizationEntries(); } } }