Beispiel #1
0
        /// <summary>
        /// Add a translation resource to the context.
        ///
        /// The translation resource must be a proper FluentResource
        /// parsed by `MessageContext.parseResource`.
        ///
        ///     let res = MessageContext.parseResource("foo = Foo");
        ///     ctx.addResource(res);
        ///     ctx.getMessage('foo');
        ///
        ///     // Returns a raw representation of the 'foo' message.
        ///
        /// Parsed entities should be formatted with the `format` method in case they
        /// contain logic (references, select expressions etc.).
        /// </summary>
        /// <param name="resource">The resource object</param>
        /// <returns>A list of errors encountered during adding or parsing the resource</returns>
        public IList <ParseException> AddResource(FluentResource resource)
        {
            var errors = new List <ParseException>(resource.Errors);

            foreach (var entry in resource.Entries)
            {
#if FALSE
                if (!(entry is Ast.MessageTermBase))
                {
                    // Ast.Comment or Ast.Junk
                    continue;
                }

                if (entry is Ast.Term t)
                {
                    if (_terms.ContainsKey(t.Id.Name))
                    {
                        errors.Add($"Attempt to override an existing term: \"{t.Id.Name}\"");
                        continue;
                    }
                    _terms.Add(t.Id.Name, t);
                }
                else if (entry is Ast.Message m)
                {
                    if (_messages.ContainsKey(m.Id.Name))
                    {
                        errors.Add($"Attempt to override an existing message: \"{m.Id.Name}\"");
                        continue;
                    }
                    _messages.Add(m.Id.Name, m);
                }
                // else Ast.Comment or Ast.Junk
#endif
                if (entry.Key.StartsWith("-"))
                {
                    if (_terms.ContainsKey(entry.Key))
                    {
                        errors.Add(new OverrideError(
                                       $"Attempt to override an existing term: \"{entry.Key}\""));
                        continue;
                    }
                    _terms.Add(entry);
                }
                else
                {
                    if (_messages.ContainsKey(entry.Key))
                    {
                        errors.Add(new OverrideError(
                                       $"Attempt to override an existing message: \"{entry.Key}\""));
                        continue;
                    }
                    _messages.Add(entry);
                }
            }
            return(errors);
        }
Beispiel #2
0
 /// <summary>
 /// Add a translation resource to the context.
 ///
 /// The translation resource must use the Fluent syntax.  It will be parsed by
 /// the context and each translation unit (message) will be available in the
 /// context by its identifier.
 ///
 ///     ctx.addMessages('foo = Foo');
 ///     ctx.getMessage('foo');
 ///
 ///     // Returns a raw representation of the 'foo' message.
 ///
 /// Parsed entities should be formatted with the `format` method in case they
 /// contain logic (references, select expressions etc.).
 /// </summary>
 /// @param   {string} source - Text resource with translations.
 /// @returns {Array<Error>}
 ///
 public IList <ParseException> AddMessages(TextReader source)
 {
     return(AddResource(FluentResource.FromReader(source)));
 }