/// <summary>
        /// Initializes a new instance of the <see cref="TemplateEngineLanguageGenerator"/> class.
        /// </summary>
        /// <param name="lgText">lg template text.</param>
        /// <param name="id">optional label for the source of the templates (used for labeling source of template errors).</param>
        /// <param name="resourceMapping">template resource loader delegate (locale) -> <see cref="ImportResolverDelegate"/>.</param>
        public TemplateEngineLanguageGenerator(string lgText, string id, Dictionary <string, IList <IResource> > resourceMapping)
        {
            this.Id        = id ?? DEFAULTLABEL;
            var(_, locale) = LGResourceLoader.ParseLGFileName(id);
            var importResolver = LanguageGeneratorManager.ResourceExplorerResolver(locale, resourceMapping);

            this.lg = LanguageGeneration.Templates.ParseText(lgText ?? string.Empty, Id, importResolver);
        }
        public TemplateEngineLanguageGenerator(string lgText, string id, Dictionary <string, IList <Resource> > resourceMapping)
        {
            Id             = id ?? DEFAULTLABEL;
            var(_, locale) = LGResourceLoader.ParseLGFileName(id);
            var importResolver = LanguageGeneratorManager.ResourceExplorerResolver(locale, resourceMapping);
            var lgResource     = new LGResource(Id, Id, lgText ?? string.Empty);

            _lg = new Lazy <Task <LanguageGeneration.Templates> >(() => Task.FromResult(LanguageGeneration.Templates.ParseResource(lgResource, importResolver)));
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="TemplateEngineLanguageGenerator"/> class.
        /// </summary>
        /// <param name="filePath">lg template file absolute path.</param>
        /// <param name="resourceMapping">template resource loader delegate (locale) -> <see cref="ImportResolverDelegate"/>.</param>
        public TemplateEngineLanguageGenerator(string filePath, Dictionary <string, IList <IResource> > resourceMapping)
        {
            filePath = PathUtils.NormalizePath(filePath);
            this.Id  = Path.GetFileName(filePath);

            var(_, locale) = LGResourceLoader.ParseLGFileName(Id);
            var importResolver = LanguageGeneratorManager.ResourceExplorerResolver(locale, resourceMapping);

            this.lg = LanguageGeneration.Templates.ParseFile(filePath, importResolver);
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="TemplateEngineLanguageGenerator"/> class.
        /// </summary>
        /// <param name="resource">Resource.</param>
        /// <param name="resourceMapping">template resource loader delegate (locale) -> <see cref="ImportResolverDelegate"/>.</param>
        public TemplateEngineLanguageGenerator(Resource resource, Dictionary <string, IList <Resource> > resourceMapping)
        {
            this.Id = resource.Id;

            var(_, locale) = LGResourceLoader.ParseLGFileName(Id);
            var importResolver = LanguageGeneratorManager.ResourceExplorerResolver(locale, resourceMapping);
            var content        = resource.ReadTextAsync().GetAwaiter().GetResult();
            var lgResource     = new LGResource(Id, resource.FullName, content);

            this.lg = LanguageGeneration.Templates.ParseResource(lgResource, importResolver);
        }
        public TemplateEngineLanguageGenerator(string filePath, Dictionary <string, IList <Resource> > resourceMapping)
        {
            filePath = PathUtils.NormalizePath(filePath);
            Id       = Path.GetFileName(filePath);

            var(_, locale) = LGResourceLoader.ParseLGFileName(Id);
            var importResolver = LanguageGeneratorManager.ResourceExplorerResolver(locale, resourceMapping);
            var resource       = new LGResource(Id, filePath, File.ReadAllText(filePath));

            _lg = new Lazy <Task <LanguageGeneration.Templates> >(() => Task.FromResult(LanguageGeneration.Templates.ParseResource(resource, importResolver)));
        }
        /// <summary>
        /// Loads language generation templates asynchronously.
        /// </summary>
        /// <param name="resource">Resource.</param>
        /// <param name="resourceMapping">template resource loader delegate (locale) -> <see cref="ImportResolverDelegate"/>.</param>
        /// <returns>The loaded language generation templates.</returns>
        private async Task <LanguageGeneration.Templates> CreateTemplatesAsync(Resource resource, Dictionary <string, IList <Resource> > resourceMapping)
        {
            var(_, locale) = LGResourceLoader.ParseLGFileName(Id);
            var importResolver = LanguageGeneratorManager.ResourceExplorerResolver(locale, resourceMapping);
            var content        = await resource.ReadTextAsync().ConfigureAwait(false);

            var lgResource = new LGResource(Id, resource.FullName, content);
            var lg         = LanguageGeneration.Templates.ParseResource(lgResource, importResolver);

            RegisterSourcemap(lg, resource);
            return(lg);
        }
Пример #7
0
        /// <summary>
        /// Returns the resolver to resolve LG import id to template text based on language and a template resource loader delegate.
        /// </summary>
        /// <param name="locale">Locale to identify language.</param>
        /// <param name="resourceMapping">Template resource loader delegate.</param>
        /// <returns>The delegate to resolve the resource.</returns>
        public static ImportResolverDelegate ResourceExplorerResolver(string locale, Dictionary <string, IList <Resource> > resourceMapping)
        {
            return((LGResource lgResource, string id) =>
            {
                var fallbackLocale = LGResourceLoader.FallbackLocale(locale, resourceMapping.Keys.ToList());
                var resources = resourceMapping[fallbackLocale];

                var resourceName = Path.GetFileName(PathUtils.NormalizePath(id));

                var resource = resources.FirstOrDefault(u => LGResourceLoader.ParseLGFileName(u.Id).prefix.ToLowerInvariant() == LGResourceLoader.ParseLGFileName(resourceName).prefix.ToLowerInvariant());
                if (resource == null)
                {
                    throw new InvalidOperationException($"There is no matching LG resource for {resourceName}");
                }

                var content = resource.ReadTextAsync().GetAwaiter().GetResult();
                return new LGResource(resource.Id, resource.FullName, content);
            });
        }