Ejemplo n.º 1
0
 public I18N(
     IOptions <JsonLocalizationOptions> options,
     IKeyProvider keyProvider
     )
 {
     _options     = options?.Value ?? throw new ArgumentNullException(nameof(options));
     _keyProvider = keyProvider ?? throw new ArgumentNullException(nameof(keyProvider));
 }
Ejemplo n.º 2
0
        public static void BuildDictionary(IDictionaryBuilder dictionaryBuilder, JsonLocalizationOptions options)
        {
            if (dictionaryBuilder == null)
            {
                throw new ArgumentNullException(nameof(dictionaryBuilder));
            }

            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            ClearDictionary();

            dictionaryBuilder.Build(Translations, options);
        }
Ejemplo n.º 3
0
        public virtual void Build(ConcurrentDictionary <string, string> dictionary, JsonLocalizationOptions options)
        {
            if (options.ResourceFolders == null)
            {
                throw new ArgumentNullException(nameof(options.ResourceFolders));
            }

            if (string.IsNullOrEmpty(options.LocaleFileExtension))
            {
                throw new ArgumentNullException(nameof(options.LocaleFileExtension));
            }

            foreach (var path in options.ResourceFolders)
            {
                foreach (var file in DirectoryGetFiles(path, $"*.{options.LocaleFileExtension}",
                                                       SearchOption.TopDirectoryOnly))
                {
                    try
                    {
                        var locale = Path.GetFileNameWithoutExtension(file)?.ToLower();
                        if (string.IsNullOrWhiteSpace(locale))
                        {
                            continue;
                        }

                        var jobj = JObject.Parse(ReadAllText(file));
                        foreach (var property in jobj.Properties())
                        {
                            var value    = property.Value?.ToString();
                            var cacheKey = GetCacheKey(path, file, property.Name);
                            dictionary.AddOrUpdate(cacheKey, value, (k, oldValue) => value);
                        }
                    }
                    catch
                    {
                        // ignored
                    }
                }
            }
        }