/// <summary> /// Resets variables to defaults, in preparation for loading /// a new file. /// </summary> private void Init() { lock (_syncLock) { _storage = new Dictionary <string, Message>(); _headers = new Dictionary <string, string>(); _nplurals = 2; _plural = "(n != 1)"; _pluralEvaluator = new DefaultPluralEvaluator(); } }
/// <summary> /// Loads plural information and evaluator, based plural forms string. /// </summary> /// <param name="pluralForms">Plural-Forms header.</param> /// <example> /// LoadPluralEvaluator("nplurals=2; plural=(n != 1);") /// </example> private void LoadPluralEvaluator(string pluralForms) { var match = Regex.Match(pluralForms, @"^nplurals=(?<nplurals>[0-9]+);\s*plural=(?<plural>[0-9n\?:\(\)!=\s%<>|&]+);?$"); if (!match.Success) throw new ArgumentException("Invalid Plural-Forms value: " + pluralForms); // Read number and rules _nplurals = Convert.ToInt32(match.Groups["nplurals"].Value); _plural = match.Groups["plural"].Value; // Create script var script = @" using System; using nettext; public class PluralEvaluator : IPluralEvaluator { public int Eval(int n) { // A plural like `(n != 1)` would result in a bool // in C#, use Convert to guarantee an int result. return Convert.ToInt32(" + _plural + @"); } } "; // Prepare compiler var compiler = new CSharpCodeProvider(); var parameters = new CompilerParameters(); parameters.ReferencedAssemblies.Add(typeof(IPluralEvaluator).Assembly.Location); parameters.GenerateExecutable = false; parameters.GenerateInMemory = true; // Compile, throw if compilation failed var result = compiler.CompileAssemblyFromSource(parameters, script); foreach (CompilerError err in result.Errors) throw new FormatException("Failed to compile plural evaluator: " + err.ErrorText); // Get type var type = result.CompiledAssembly.GetType("PluralEvaluator"); if (type == null) throw new TypeLoadException("Failed to generate plural evaluator, no PluralEvaluator."); // Instantiate evaluator _pluralEvaluator = Activator.CreateInstance(type) as IPluralEvaluator; if (_pluralEvaluator == null) throw new TypeLoadException("Failed to generate plural evaluator, no IPluralEvaluator."); }
/// <summary> /// Resets variables to defaults, in preparation for loading /// a new file. /// </summary> private void Init() { _storage = new Dictionary<string, Message>(); _headers = new Dictionary<string, string>(); _nplurals = 2; _plural = "(n != 1)"; _pluralEvaluator = new DefaultPluralEvaluator(); }
/// <summary> /// Loads plural information and evaluator, based plural forms string. /// </summary> /// <param name="pluralForms">Plural-Forms header.</param> /// <example> /// LoadPluralEvaluator("nplurals=2; plural=(n != 1);") /// </example> private void LoadPluralEvaluator(string pluralForms) { var match = Regex.Match(pluralForms, @"^nplurals=(?<nplurals>[0-9]+);\s*plural=(?<plural>[0-9n\?:\(\)!=\s%<>|&]+);?$"); if (!match.Success) { throw new ArgumentException("Invalid Plural-Forms value: " + pluralForms); } // Read number and rules lock (_syncLock) { _nplurals = Convert.ToInt32(match.Groups["nplurals"].Value); _plural = match.Groups["plural"].Value; // Check for known evaluators var knownEvaluator = DefaultPluralEvaluator.GetKnownEvaluator(pluralForms); if (knownEvaluator != null) { _pluralEvaluator = knownEvaluator; return; } // Create script var script = @" using System; using nettext; public class PluralEvaluator : IPluralEvaluator { public int Eval(int n) { // A plural like `(n != 1)` would result in a bool // in C#, use Convert to guarantee an int result. return Convert.ToInt32(" + _plural + @"); } } " ; // Prepare compiler var compiler = new CSharpCodeProvider(); var parameters = new CompilerParameters(); parameters.ReferencedAssemblies.Add(typeof(IPluralEvaluator).Assembly.Location); parameters.GenerateExecutable = false; parameters.GenerateInMemory = true; // Compile, throw if compilation failed var result = compiler.CompileAssemblyFromSource(parameters, script); foreach (CompilerError err in result.Errors) { throw new FormatException("Failed to compile plural evaluator: " + err.ErrorText); } // Get type var type = result.CompiledAssembly.GetType("PluralEvaluator"); if (type == null) { throw new TypeLoadException("Failed to generate plural evaluator, no PluralEvaluator."); } // Instantiate evaluator _pluralEvaluator = Activator.CreateInstance(type) as IPluralEvaluator; if (_pluralEvaluator == null) { throw new TypeLoadException("Failed to generate plural evaluator, no IPluralEvaluator."); } } }