예제 #1
0
    private void Localize(SimpleLocalizationLangs language)
    {
        LocalizationKey tempKey =
            LocalizationSystem.Instance.LocAsset.localizationKeys.Find(
                elem => elem.key == keyLocalization
                );

        if (tempKey != null)
        {
            LangText tempLangText =
                tempKey.value.Find(
                    lang => lang.key == language
                    );

            if (tempLangText != null)
            {
                textToLocalize.text = tempLangText.value;
            }
            else
            {
                Debug.LogWarning(string.Format("WRN - Could not find language {0} in the key {1}.", language, tempKey.key));
            }
        }
        else
        {
            Debug.LogWarning(string.Format("WRN - Could not find key {0} in the asset.", keyLocalization));
        }
    }
예제 #2
0
        public string GetHeader(PropertyToken property)
        {
            var localizationKey = new LocalizationKey(property.StringTokenKey);

            return(_localeCache
                   .Retrieve(localizationKey, () => _missingHandler.FindMissingProperty(property, _localeCache.Culture)));
        }
 protected virtual void OnLocalKeyChange(LocalizationKey localKey)
 {
     m_localKey = localKey;
     if (autoRefresh)
     {
         RefreshContent();
     }
 }
예제 #4
0
        public void Concat()
        {
            var left     = LocalizationKey.CreateRaw("hoge");
            var right    = LocalizationKey.CreateRaw("fuga");
            var hogefuga = left.Concat(right);

            Assert.AreEqual("hogefuga", hogefuga.Localize());
        }
예제 #5
0
        public string Get(LocalizationKey key)
        {
            if (key == null)
            {
                throw new ArgumentNullException(nameof(key));
            }

            return(@"--->" + key.Name);
        }
예제 #6
0
        public Task ShowMessageAsync(LocalizationKey localizationKey)
        {
            if (localizationKey == null)
            {
                throw new ArgumentNullException(nameof(localizationKey));
            }

            return(this.GetService <IModalDialog>().ShowAsync(PermissionResult.Deny(this.GetLocalized(localizationKey))));
        }
 protected virtual void Start()
 {
     m_localKey = LocalizationManager.Ins.localKey;
     if (autoRefresh)
     {
         RefreshContent();
     }
     LocalizationManager.Ins.onLocalKeyChange += OnLocalKeyChange;
 }
예제 #8
0
        public string GetLocalized(LocalizationKey key)
        {
            if (key == null)
            {
                throw new ArgumentNullException(nameof(key));
            }

            return(this.GetService <LocalizationManager>().Get(key));
        }
예제 #9
0
 public void Fixed()
 {
     Assert.True(LocalizationKey.CreateRaw("hoge").Fixed);
     Assert.False(new LocalizationKey("hoge").Fixed);
     Assert.False(new LocalizationKey(0).Fixed);
     Assert.True(LocalizationKey.CreateRaw("hoge").Replace(("hoge", "piyo")).Fixed);
     Assert.False(LocalizationKey.CreateRaw("hoge").Replace(("hoge", new LocalizationKey("k"))).Fixed);
     Assert.True(LocalizationKey.CreateRaw("hoge").Replace(("hoge", LocalizationKey.CreateRaw("piyo"))).Fixed);
 }
예제 #10
0
        public void Replace()
        {
            var hoge = LocalizationKey.CreateRaw("hogehoge");
            var fuga = hoge.Replace(("hoge", "fuga"));
            var piyo = fuga.Replace(("fuga", "piyo"));

            Assert.AreEqual("fugafuga", fuga.Localize());
            Assert.AreEqual("piyopiyo", piyo.Localize());
        }
예제 #11
0
        void ILocalizableWidget.Localize(string namespc)
        {
            Label l = this.Child as Label;

            if (LocalizationKey == null)
            {
                LocalizationKey = l.Text;
            }
            l.Text = LocalizationKey.Localized(namespc);
        }
예제 #12
0
        public void Select()
        {
            var hoge     = LocalizationKey.CreateRaw("hoge");
            var hogepiyo = hoge.Select(_ => _ + "piyo");

            Assert.AreEqual("hogepiyo", hogepiyo.Localize());
            var fuga = hoge.Select((_lang, _) => _.Replace("hoge", "fuga"));

            Assert.AreEqual("fuga", fuga.Localize());
        }
예제 #13
0
        public void Join()
        {
            var key = LocalizationKey.Join(
                LocalizationKey.CreateRaw(", "),
                LocalizationKey.CreateRaw("hoge"),
                LocalizationKey.CreateRaw("fuga"),
                LocalizationKey.CreateRaw("piyo"));

            Assert.AreEqual("hoge, fuga, piyo", key.Localize());
        }
예제 #14
0
        public string Retrieve(LocalizationKey key, Func<string> missing)
        {
            var text = initialRead(key);

            if (text == null)
            {
                text = missing();
                Append(key, text);
            }

            return text;
        }
예제 #15
0
        /// <summary>
        /// Inserts new localization key
        /// </summary>
        /// <param name="localizationKey"></param>
        public void InsertLocalizationKey(LocalizationKey localizationKey)
        {
            using (var connection = new SqlConnection(Configuration.ConnectionString))
            {
                connection.Open();
                var sql     = $"INSERT INTO [{Configuration.LocalizationKeyTableName}]([Name]) VALUES(@KeyName)";
                var command = new SqlCommand(sql, connection);
                command.Parameters.Add("KeyName", SqlDbType.NVarChar);
                command.Parameters["KeyName"].Value = localizationKey.Name;

                command.ExecuteNonQuery();
            }
        }
예제 #16
0
 protected override void Awake()
 {
     base.Awake();
     if (LocalizationManager.isIns)
     {
         m_localKey = LocalizationManager.Ins.localKey;
     }
     if (m_dic == null)
     {
         InitDic();
     }
     LocalizationManager.Ins.onLocalKeyChange += OnLocalizationChange;
 }
예제 #17
0
 public void Append(LocalizationKey key, string value)
 {
     _lock.Write(() =>
     {
         if (_data.ContainsKey(key))
         {
             _data[key] = value;
         }
         else
         {
             _data.Add(key, value);
         }
     });
 }
        public void CompareStringBuilder()
        {
            var builder = new LocalizationKeyBuilder();

            builder.AppendLine();
            builder.Append(LocalizationKey.CreateRaw("hoge"));
            builder.AppendLine(LocalizationKey.CreateRaw("fuga"));

            var stringBuilder = new System.Text.StringBuilder();

            stringBuilder.AppendLine();
            stringBuilder.Append("hoge");
            stringBuilder.AppendLine("fuga");

            Assert.AreEqual(stringBuilder.ToString(), builder.ToLocalizationKey().Localize());
        }
예제 #19
0
        public ThreadSafeLocaleCache(CultureInfo culture, IEnumerable<LocalString> strings)
        {
            _data = new Dictionary<LocalizationKey, string>();
            strings.Each(s =>
            {
                var localizationKey = new LocalizationKey(s.value);
                if (_data.ContainsKey(localizationKey))
                {
                    throw new ArgumentException("Could not add localization key '{0}' to the cache as it already exists.".ToFormat(s.value));
                }

                _data.Add(localizationKey, s.display);
            });

            _culture = culture;
        }
        private string _getLocalizationString(LocalizationContext context, CultureInfo culture, string baseKey, string mainKey, bool updateLastUsed)
        {
            var cultureName = culture.Name;
            var dbCulture   = context.SupportedCultures.SingleOrDefault(c => c.Name == cultureName && c.IsSupported);

            if (dbCulture == null)
            {
                return(null);
            }

            var dbKey = context.LocalizationKeys.SingleOrDefault(k => k.Base == baseKey && k.Key == mainKey);

            if (dbKey == null)
            {
                // Key doesn't exist, create!
                dbKey = new LocalizationKey()
                {
                    Base    = baseKey,
                    Key     = mainKey,
                    Comment = LocalizationKey.DEFAULT_COMMENT
                };
                try
                {
                    context.LocalizationKeys.Add(dbKey);
                    context.SaveChanges();
                }
                catch (DbUpdateException ex) when((ex.InnerException as System.Data.SqlClient.SqlException)?.Number == 2601)
                {
                    // Key already exists, (concurrent request probably added it): reload entity.
                    context.Entry(dbKey).State = EntityState.Detached;
                    dbKey = context.LocalizationKeys.SingleOrDefault(k => k.Base == baseKey && k.Key == mainKey);
                }
            }

            var localization = context.LocalizationRecords
                               .Where(r => r.LocalizationKey.Id == dbKey.Id && r.Culture.Id == dbCulture.Id)
                               .SingleOrDefault();

            if (updateLastUsed && localization != null)
            {
                localization.LastUsed = DateTime.Now;
                context.SaveChanges();
            }

            return(localization?.Text);
        }
예제 #21
0
        public async Task <IActionResult> Add(KeyVM model)
        {
            if (model == null)
            {
                return(View(model));
            }
            if (string.IsNullOrWhiteSpace(model.Base))
            {
                return(View(model));
            }
            if (string.IsNullOrWhiteSpace(model.Main))
            {
                return(View(model));
            }

            try
            {
                var key = new LocalizationKey()
                {
                    Base    = model.Base,
                    Comment = model.Comment,
                    Key     = model.Main
                };

                _context.Add(key);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Edit), new { baseKey = model.Base, mainKey = model.Main, saveResult = SaveResult.Success }));
            }
            catch (DbUpdateException ex) when((ex.InnerException as System.Data.SqlClient.SqlException)?.Number == 2601)
            {
                _context.ChangeTracker.Entries().ToList().ForEach(e => e.State = EntityState.Detached);
                model.SaveResult = SaveResult.Duplicate;
                return(View(model));
            }
            catch (Exception)
            {
                model.SaveResult = SaveResult.UnknownFailure;
                return(View(model));
            }
        }
        public void Add_writes_to_database()
        {
            var options = new DbContextOptionsBuilder <LocalizationContext>()
                          .UseInMemoryDatabase(databaseName: "Add_writes_to_database")
                          .Options;

            using (var context = new LocalizationContext(options))
            {
                var culture = new SupportedCulture()
                {
                    IsSupported = true, Name = "nl"
                };
                context.SupportedCultures.Add(culture);

                var key = new LocalizationKey()
                {
                    Base = "Hello.World.Test", Key = "HELLO_WORLD"
                };
                context.LocalizationKeys.Add(key);

                context.LocalizationRecords.Add(new LocalizationRecord()
                {
                    Culture         = culture,
                    LocalizationKey = key,
                    Status          = RecordStatus.HumanTranslated,
                    Text            = "Hallo wereld!"
                }).State = EntityState.Added;

                context.SaveChanges();
            }

            using (var context = new LocalizationContext(options))
            {
                Assert.Equal(1, context.SupportedCultures.Count());
                Assert.Equal("nl", context.SupportedCultures.Single().Name);
                Assert.Equal("Hallo wereld!", context.LocalizationRecords.Single().Text);
            }
        }
예제 #23
0
 public void SetTextToTip(LocalizationKey tipKey, params object[] parameters)
 {
     this.SetTextToTip(LocalizationSystem.Get(tipKey, parameters));
 }
예제 #24
0
        /// <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();
                }
            }
        }
예제 #25
0
 private string initialRead(LocalizationKey key)
 {
     return _lock.Read(() => !_data.ContainsKey(key) ? null : _data[key]);
 }
 public string GetHeader(PropertyToken property)
 {
     var localizationKey = new LocalizationKey(property.StringTokenKey);
     return _localeCache
         .Retrieve(localizationKey, () => _missingHandler.FindMissingProperty(property, _localeCache.Culture));
 }
 /// <inheritdoc/>
 public override int GetHashCode()
 {
     return(unchecked (((17 * 23 + Identifier.GetHashCode()) * 23
                        + (LocalizationKey?.GetHashCode() ?? 0)) * 23
                       + (Value?.GetHashCode() ?? 0)));
 }
예제 #28
0
 private void OnEnable()
 {
     _tgt = (LocalizationKey)target;
 }
예제 #29
0
 private void OnLocalizationChange(LocalizationKey localKey)
 {
     m_localKey = localKey;
     InitDic();
 }
예제 #30
0
 //OnKeySet event. When some key changed this value
 void OnKeySet(string languageCode, LocalizationKey changedKey)
 {
     UpdateScoreText();
 }
예제 #31
0
 /// <summary>
 /// Inserts new localization key
 /// </summary>
 /// <param name="localizationKey"></param>
 public void InsertLocalizationKey(LocalizationKey localizationKey)
 {
     throw new NotSupportedException();
 }