private static IRuleBuilderOptions <T, string> WithLocalizedMessage <T>(this IRuleBuilderOptions <T, string> options, string key) { IStringLocalizer localizer = new JsonStringLocalizer(); var localizedValue = localizer[key].Value; return(options.Configure(x => x.CurrentValidator.Options.SetErrorMessage(localizedValue))); }
public ICustomStringLocalizer BuildCustomStringLocalizer() { ILogger loggerStub = new LoggerStub(); IStringLocalizer stringLocalizer = new JsonStringLocalizer( "Resources", "TaskForModule5", loggerStub); return(new CustomStringLocalizer(stringLocalizer)); }
public void NonExistentTranslationFile_KeysReturnedAsValues() { var localizer = new JsonStringLocalizer("does not exist.json", _culture); var result = localizer["Some Key"]; var expected = new LocalizedString("Some Key", "Some Key", resourceNotFound: true); Assert.Equal(expected, result, LocalizedStringComparer.Instance); }
public void InitLocalizer(CultureInfo cultureInfo) { CultureInfo.CurrentUICulture = cultureInfo; localizer = JsonStringLocalizerHelperFactory.Create(new JsonLocalizationOptions() { DefaultCulture = new CultureInfo("en-US"), SupportedCultureInfos = new System.Collections.Generic.HashSet <CultureInfo>() { new CultureInfo("fr-FR") }, ResourcesPath = "json", }); }
public void InitLocalizer(CultureInfo cultureInfo) { CultureInfo.CurrentUICulture = cultureInfo; localizer = JsonStringLocalizerHelperFactory.Create(new JsonLocalizationOptions() { DefaultCulture = new CultureInfo("en-US"), SupportedCultureInfos = new System.Collections.Generic.HashSet <CultureInfo>() { new CultureInfo("fr-FR") }, ResourcesPath = $"{AppContext.BaseDirectory}/path", IsAbsolutePath = true }); }
private static JsonStringLocalizer CreateLocalizer(string resourceKey, string resourceText, string cultureName = "en-us") { var localizerFactoryMock = new Mock <IJsonStringLocalizerFactoryInternal>(); var map = new Dictionary <string, string> { [resourceKey] = resourceText, }; var cultureInfo = CultureInfo.GetCultureInfo(cultureName); var localizer = new JsonStringLocalizer(map, cultureInfo, localizerFactoryMock.Object); return(localizer); }
public void InitLocalizer(char seperator = '|') { CultureInfo.CurrentUICulture = new CultureInfo("fr-FR"); localizer = JsonStringLocalizerHelperFactory.Create(new JsonLocalizationOptions() { DefaultCulture = new CultureInfo("en-US"), SupportedCultureInfos = new System.Collections.Generic.HashSet <CultureInfo>() { new CultureInfo("fr-FR"), }, ResourcesPath = "pluralization", PluralSeparator = seperator }); }
public void InitLocalizer(string cultureString) { SetCurrentCulture(cultureString); localizer = JsonStringLocalizerHelperFactory.Create(new JsonLocalizationOptions() { DefaultCulture = null, SupportedCultureInfos = new System.Collections.Generic.HashSet <CultureInfo>() { new CultureInfo("fr"), new CultureInfo("en"), new CultureInfo("zh-CN"), new CultureInfo("en-AU") }, ResourcesPath = "fallback", }); }
/// <summary> /// Main /// </summary> /// <param name="args">json/resx inputDir outputDir exceptionDir1 ...</param> static void Main(string[] args) { Console.WriteLine($"Got arguments: {string.Join(", ", args)}"); Console.WriteLine($"Work directory: {Directory.GetCurrentDirectory()}"); if (args == null || args.Length < 3) { throw new ArgumentException( "Valid arguments: {json/resx} {input directory} {output directory} {exclude directory1}..."); } Console.WriteLine($"Check input directory... "); if (!Path.IsPathRooted(args[1])) { args[1] = Path.GetFullPath(args[1]); } Console.WriteLine($"\t{args[1]}"); if (!Directory.Exists(args[1])) { throw new ArgumentException("Input directory does not exist."); } Console.WriteLine($"Create output directory... "); if (!Path.IsPathRooted(args[2])) { args[2] = Path.GetFullPath(args[2]); } Console.WriteLine($"\t{args[2]}"); if (!Directory.CreateDirectory(args[2]).Exists) { throw new ArgumentException("Failed to create output directory."); } var exclude = args.Length > 3 ? args.Skip(3).Select(x => Path.IsPathRooted(x) ? x : Path.GetFullPath(x)) : Array.Empty <string>(); Console.WriteLine($"Excluded: {string.Join(", ", exclude)}"); CheckResourceType(args[0]); Console.Write($"Analyze resources... "); var resources = Directory.GetFiles(args[1], $"*.{args[0]}", SearchOption.AllDirectories) .Where(x => !exclude.Any(z => x.StartsWith(z))) .Select(x => x.Replace(args[1] + Path.DirectorySeparatorChar, string.Empty)) .Select(x => x.Substring(0, x.IndexOf('.'))).Distinct(); Console.WriteLine("\tok."); Console.Write($"Analyze cultures... "); var cultures = Directory.GetFiles(args[1], $"*.{args[0]}", SearchOption.AllDirectories) .Where(x => !exclude.Any(z => x.StartsWith(z))) .Select(x => x.Replace(args[1] + Path.DirectorySeparatorChar, string.Empty)) .Select(x => x.Replace($".{args[0]}", string.Empty)) .Where(x => x.Contains(".")) .Select(x => x.Substring(x.LastIndexOf('.') + 1, 5)).Distinct(); Console.WriteLine("\tok."); foreach (var culture in cultures) { Console.WriteLine($"\tFor culture: {culture}: "); CultureInfo.CurrentCulture = new CultureInfo(culture); CultureInfo.CurrentUICulture = new CultureInfo(culture); var result = new Dictionary <string, Dictionary <string, string> >(); foreach (var resource in resources) { Console.WriteLine($"\t\tresource: {resource}"); var section = new JsonStringLocalizer(args[1], resource, NullLogger.Instance) .GetAllStrings() .OrderBy(x => x.Name) .ToDictionary(x => x.Name, x => x.Value); result.Add(resource, section); } var resultString = JsonConvert.SerializeObject(result, Formatting.Indented); var path = Path.Combine(args[2], $"{culture}.json"); Console.WriteLine($"\tWrite result to: {path}"); File.WriteAllText(path, resultString, System.Text.Encoding.UTF8); } Console.WriteLine($"Done."); }
public InfoController(IStringLocalizer <InfoController> localizer, JsonStringLocalizer stringLocalizer) { _localizer = localizer; this.stringLocalizer = stringLocalizer; }