/// <summary> /// Inserts new localization collection /// </summary> /// <param name="localizationCollection"></param> public void InsertOrUpdateLocalizationCollection(LocalizationCollection localizationCollection) { using (var connection = new SqlConnection(Configuration.ConnectionString)) { connection.Open(); var sql = $@" IF EXISTS ( SELECT * FROM [{Configuration.LocalizationCollectionTableName}] WHERE Id = {localizationCollection.Id} ) UPDATE [{Configuration.LocalizationCollectionTableName}] SET Name = @CollName, LocalizationCategoryId = {localizationCollection.LocalizationCategoryId} ELSE INSERT INTO [{Configuration.LocalizationCollectionTableName}]([Name], [LocalizationCategoryId]) VALUES(@CollName, {localizationCollection.LocalizationCategoryId}) "; var command = new SqlCommand(sql, connection); command.Parameters.Add("CollName", SqlDbType.NVarChar); command.Parameters["CollName"].Value = localizationCollection.Name; command.ExecuteNonQuery(); } }
private void AddResourceTokens(Web web, LocalizationCollection localizations, FileConnectorBase connector) { if (localizations != null && localizations.Any()) { //https://github.com/SharePoint/PnP-Provisioning-Schema/issues/301 //fixing issue to allow multiple resx files in the template. i.e: //<pnp:Localization LCID="1033" Name="core" ResourceFile="core.en-us.resx" /> //<pnp:Localization LCID="3082" Name="core" ResourceFile="core.es-es.resx" /> //<pnp:Localization LCID="1033" Name="intranet" ResourceFile="intranet.en-us.resx" /> //<pnp:Localization LCID="3082" Name="intranet" ResourceFile="intranet.es-es.resx" /> var resourcesFilesCount = localizations.GroupBy(l => l.Name).Count(); // Read all resource keys in a list List <Tuple <string, uint, string> > resourceEntries = new List <Tuple <string, uint, string> >(); foreach (var localizationEntry in localizations) { var filePath = localizationEntry.ResourceFile; using (var stream = connector.GetFileStream(filePath)) { if (stream != null) { #if !NETSTANDARD2_0 using (ResXResourceReader resxReader = new ResXResourceReader(stream)) { foreach (DictionaryEntry entry in resxReader) { // One can have multiple resource files in a single file, by adding tokens with resource file name and without we allow both scenarios to resolve resourceEntries.Add(new Tuple <string, uint, string>($"{localizationEntry.Name}:{entry.Key}", (uint)localizationEntry.LCID, entry.Value.ToString().Replace("\"", """))); resourceEntries.Add(new Tuple <string, uint, string>(entry.Key.ToString(), (uint)localizationEntry.LCID, entry.Value.ToString().Replace("\"", """))); } } #else var xElement = XElement.Load(stream); foreach (var dataElement in xElement.Descendants("data")) { var key = dataElement.Attribute("name").Value; var value = dataElement.Value; resourceEntries.Add(new Tuple <string, uint, string>($"{localizationEntry.Name}:{key}", (uint)localizationEntry.LCID, value.ToString().Replace("\"", """))); resourceEntries.Add(new Tuple <string, uint, string>(key.ToString(), (uint)localizationEntry.LCID, value.ToString().Replace("\"", """))); } #endif } } } var uniqueKeys = resourceEntries.Select(k => k.Item1).Distinct(); foreach (var key in uniqueKeys) { var matches = resourceEntries.Where(k => k.Item1 == key); var entries = matches.Select(k => new ResourceEntry() { LCID = k.Item2, Value = k.Item3 }).ToList(); LocalizationToken token = new LocalizationToken(web, key, entries); _tokens.Add(token); } } }
public bool SelectLanguage(string languageName) { Debug.Assert(this.languageIndex.ContainsKey(languageName)); if (this.currentLanguage.Language == languageName) { return(false); } this.currentLanguage = this.languages[this.languageIndex[languageName]]; return(true); }
private void Start() { this.languageIndex.Clear(); this.availableLanguages = new string[this.languages.Length]; for (int index = 0; index < this.availableLanguages.Length; ++index) { this.availableLanguages[index] = this.languages[index].Language; this.languageIndex[this.availableLanguages[index]] = index; } this.currentLanguage = this.languages[0]; }
private void AddResourceTokens(Web web, LocalizationCollection localizations, FileConnectorBase connector) { if (localizations != null && localizations.Any()) { // Read all resource keys in a list List <Tuple <string, uint, string> > resourceEntries = new List <Tuple <string, uint, string> >(); foreach (var localizationEntry in localizations) { var filePath = localizationEntry.ResourceFile; using (var stream = connector.GetFileStream(filePath)) { if (stream != null) { #if !NETSTANDARD2_0 using (ResXResourceReader resxReader = new ResXResourceReader(stream)) #else using (ResourceReader resxReader = new ResourceReader(stream)) #endif { foreach (DictionaryEntry entry in resxReader) { resourceEntries.Add(new Tuple <string, uint, string>(entry.Key.ToString(), (uint)localizationEntry.LCID, entry.Value.ToString().Replace("\"", """))); } } } } } var uniqueKeys = resourceEntries.Select(k => k.Item1).Distinct(); foreach (var key in uniqueKeys) { var matches = resourceEntries.Where(k => k.Item1 == key); var entries = matches.Select(k => new ResourceEntry() { LCID = k.Item2, Value = k.Item3 }).ToList(); LocalizationToken token = new LocalizationToken(web, key, entries); _tokens.Add(token); } } }
/// <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(); } } }
static Localizator() { collections = Resources.Load <LocalizationCollection>("LocalizationCollection"); SetLanguage(); }
/// <summary> /// Inserts new localization collection /// </summary> /// <param name="localizationCollection"></param> public void InsertOrUpdateLocalizationCollection(LocalizationCollection localizationCollection) { throw new NotSupportedException(); }
static Localizator() { collections = Resources.Load <LocalizationCollection>("LocalizationCollection"); Debug.LogFormat("Loading localization data: {0}", collections); SetLanguage(); }