Пример #1
0
        public JsonResult GetTranslations(string lang)
        {
            var languages = _locConfig.Value.Languages.Select(x => x.Identifier);

            if (!languages.Contains(lang))
            {
                return(Json(null));
            }

            var translations = _localize.GetAllForLanguage(lang);
            var json         = translations.ToDictionary(trans => trans.Name, trans => trans.Value);

            return(Json(json));
        }
Пример #2
0
        /// <summary>
        /// Add language
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool AddLanguage(AddLanguageViewModel model)
        {
            var existsInConfig =
                _locConfig.Value.Languages.Any(m => m.Identifier == model.Identifier && m.Name == model.Name);

            var cPaths = new[]
            {
                _env.ContentRootPath,
                _locConfig.Value.Path,
                $"{model.Identifier}.json"
            };

            var filePath   = Path.Combine(cPaths);
            var fileExists = File.Exists(filePath);

            if (existsInConfig || fileExists)
            {
                return(false);
            }

            using (Stream stream =
                       new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Write))
                using (var sWriter = new StreamWriter(stream))
                    using (var writer = new JsonTextWriter(sWriter))
                    {
                        var keys = _localizer.GetAllForLanguage("en").Select(ls => ls.Name);
                        var dict = keys.ToDictionary <string, string, string>(key => key, key => null);
                        var obj  = JObject.FromObject(dict);

                        writer.Formatting = Formatting.Indented;
                        obj.WriteTo(writer);
                    }

            var langFile = _env.ContentRootFileProvider.GetFileInfo("appsettings.json");

            using (Stream str = new FileStream(langFile.PhysicalPath, FileMode.Open, FileAccess.Read,
                                               FileShare.ReadWrite))
                using (var sReader = new StreamReader(str))
                    using (var reader = new JsonTextReader(sReader))
                    {
                        var fileObj = JObject.Load(reader);
                        _locConfig.Value.Languages.Add(new LanguageCreateViewModel
                        {
                            Identifier = model.Identifier,
                            Name       = model.Name,
                            IsDisabled = false
                        });
                        var newLangs = JArray.FromObject(_locConfig.Value.Languages);
                        fileObj[nameof(LocalizationConfig)][nameof(LocalizationConfig.Languages)] = newLangs;
                        reader.Close();
                        File.WriteAllText(langFile.PhysicalPath, fileObj.ToString());
                    }

            return(true);
        }
Пример #3
0
        public IActionResult Translation(string lang)
        {
            if (string.IsNullOrEmpty(lang))
            {
                return(NotFound());
            }

            var translations = JsonConvert.SerializeObject(_localizer.GetAllForLanguage(lang));
            var result       = JsonConvert.DeserializeObject <IEnumerable <LocalizationListView> >(translations);

            ViewBag.lang = result;
            return(View());
        }
        /// <summary>
        /// Add language
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public ResultModel AddLanguage(AddLanguageViewModel model)
        {
            var response = new ResultModel {
                IsSuccess = true
            };
            var existsInConfig =
                _locConfig.Value.Languages.Any(m => m.Identifier == model.Identifier && m.Name == model.Name);

            var cPaths = new[]
            {
                _env.ContentRootPath,
                _locConfig.Value.Path,
                $"{model.Identifier}.json"
            };

            var filePath   = Path.Combine(cPaths);
            var fileExists = File.Exists(filePath);

            if (existsInConfig || fileExists)
            {
                response.Errors.Add(new ErrorModel
                {
                    Key     = string.Empty,
                    Message = "Language already exists."
                });
                response.IsSuccess = false;
            }
            else
            {
                using (Stream stream =
                           new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Write))
                    using (var sWriter = new StreamWriter(stream))
                        using (var writer = new JsonTextWriter(sWriter))
                        {
                            var keys = _localizer.GetAllForLanguage("en").ToList();
                            var dict = new Dictionary <string, string>();
                            foreach (var item in keys)
                            {
                                var translated = _externalTranslationProvider.TranslateText(item.Value, "en", model.Identifier);
                                dict.Add(item.Name, translated);
                            }
                            var obj = JObject.FromObject(dict);

                            writer.Formatting = Formatting.Indented;
                            obj.WriteTo(writer);
                        }

                var langsFile = _env.ContentRootFileProvider.GetFileInfo(ResourceProvider.AppSettingsFilepath(_env));

                using (Stream str = new FileStream(langsFile.PhysicalPath, FileMode.Open, FileAccess.Read,
                                                   FileShare.ReadWrite))
                    using (var sReader = new StreamReader(str))
                        using (var reader = new JsonTextReader(sReader))
                        {
                            var fileObj = JObject.Load(reader);
                            _locConfig.Value.Languages.Add(new LanguageCreateViewModel
                            {
                                Identifier = model.Identifier,
                                Name       = model.Name,
                                IsDisabled = false
                            });
                            var newLangs = JArray.FromObject(_locConfig.Value.Languages);
                            fileObj[nameof(LocalizationConfig)][nameof(LocalizationConfig.Languages)] = newLangs;
                            reader.Close();
                            File.WriteAllText(langsFile.PhysicalPath, fileObj.ToString());
                        }
            }

            return(response);
        }