Example #1
0
        public object Invoke(TemplateContext context, ScriptNode callerContext, ScriptArray arguments, ScriptBlockStatement blockStatement)
        {
            if (arguments.Count == 0)
            {
                throw new ArgumentException("Argument can't be empty", nameof(arguments));
            }

            var baseName = Path.Combine(
                Path.GetDirectoryName(context.CurrentSourceFile),
                Path.GetFileNameWithoutExtension(context.CurrentSourceFile)
                )
                           .Replace("\\", ".");

            context.TagsCurrentLocal.TryGetValue(LocalizeAssemblyFunction.CurrentAssemblyKey, out object assembly);
            if (assembly is null)
            {
                context.Tags.TryGetValue(LocalizeAssemblyFunction.CurrentAssemblyKey, out assembly);
            }
            if (assembly is null)
            {
                throw new ArgumentNullException("To use localize function you must specify an assembly from where to load the ressource \"locassembly 'assembly name'\"");
            }

            var stringLocalizer = this.stringLocalizerFactory.Create(baseName, (assembly as Assembly).FullName);

            var key  = arguments[0].ToString();
            var args = arguments.Skip(1).ToList();

            return(stringLocalizer.GetString(key, args));
        }
Example #2
0
        private string GetString(ScriptArray arguments)
        {
            if (arguments.IsNullOrEmpty())
            {
                return(string.Empty);
            }

            var name = arguments[0];

            if (name == null || name.ToString().IsNullOrWhiteSpace())
            {
                return(string.Empty);
            }

            var args = arguments.Skip(1).Where(x => x != null && !x.ToString().IsNullOrWhiteSpace()).ToArray();

            return(args.Any() ? _localizer[name.ToString(), args] : _localizer[name.ToString()]);
        }