Ejemplo n.º 1
0
 public void SetUp()
 {
     _translator = new DefaultTranslator(_backend, null, _pluralResolver, _interpolator);
     _options    = new TranslationOptions {
         DefaultNamespace = "test"
     };
 }
Ejemplo n.º 2
0
        public void WhenIDecryptValuesExpectCorrectResponse()
        {
            ITranslator translator = new DefaultTranslator();

            Assert.Equal("A", translator.Decrypt(".-"));
            Assert.Equal("D", translator.Decrypt("-.."));
        }
Ejemplo n.º 3
0
        public void WithInt_Translate_ReturnsInt()
        {
            var translator = new DefaultTranslator <int>();

            var value = translator.Translate("100");

            value.ShouldBe(100);
        }
Ejemplo n.º 4
0
        public void WithBool_Translate_ReturnsBool(string arg)
        {
            var translator = new DefaultTranslator <bool>();

            var value = translator.Translate(arg);

            value.ShouldBeTrue();
        }
Ejemplo n.º 5
0
        public void WithString_Translate_ReturnsString()
        {
            var translator = new DefaultTranslator <string>();

            var value = translator.Translate("hello");

            value.ShouldBe("hello");
        }
Ejemplo n.º 6
0
        public void Setup()
        {
            SetupBackend();

            var translator = new DefaultTranslator(_backend);

            _i18Next = new I18NextNet(_backend, translator);
        }
            public void RepeatedReDimInOutermostScope()
            {
                var source = @"
                    ReDim a(0)
                    ReDim a(1)
                    ReDim a(2)";

                var trimmedTranslatedStatements = DefaultTranslator.Translate(source, new string[0], OuterScopeBlockTranslator.OutputTypeOptions.Executable)
                                                  .Select(s => s.Content.Trim())
                                                  .ToArray();

                Assert.Equal(1, trimmedTranslatedStatements.Count(s => s == "a = null;"));
                Assert.Equal(1, trimmedTranslatedStatements.Count(s => s == "public object a { get; set; }"));
            }
Ejemplo n.º 8
0
        private DefaultTranslator DefaultTranslatorFactory(IServiceProvider c)
        {
            var backend        = c.GetRequiredService <ITranslationBackend>();
            var logger         = c.GetRequiredService <ILogger>();
            var pluralResolver = c.GetRequiredService <IPluralResolver>();
            var interpolator   = c.GetRequiredService <IInterpolator>();
            var postProcessors = c.GetRequiredService <IEnumerable <IPostProcessor> >();

            var instance = new DefaultTranslator(backend, logger, pluralResolver, interpolator);

            instance.PostProcessors.AddRange(postProcessors);

            return(instance);
        }
Ejemplo n.º 9
0
        public static void Main(string[] args)
        {
            SetupBackend();

            var translator = new DefaultTranslator(_backend);

            var i18next = new I18NextNet(_backend, translator);

            Console.WriteLine("English translation:");
            i18next.Language = "en";
            Console.WriteLine(i18next.T("exampleKey"));

            Console.WriteLine("German translation:");
            i18next.Language = "de";
            Console.WriteLine(i18next.T("exampleKey"));

            Console.ReadKey();
        }
Ejemplo n.º 10
0
        private static void SampleOne()
        {
            Console.WriteLine("Sample one: Without Microsoft.Extensions.DependencyInjection");

            var translator = new DefaultTranslator(_backend);

            var i18next = new I18NextNet(_backend, translator);

            Console.WriteLine("English translation:");
            i18next.Language = "en";
            Console.WriteLine(i18next.T("exampleKey"));

            Console.WriteLine("German translation:");
            i18next.Language = "de";
            Console.WriteLine(i18next.T("exampleKey"));

            Console.WriteLine();
        }
Ejemplo n.º 11
0
        public static void Main(string[] args)
        {
            SetupBackend();

            var translator = new DefaultTranslator(_backend);

            var i18Next = new I18NextNet(_backend, translator);

            Console.WriteLine("English translation:");
            i18Next.Language = "en";
            Console.WriteLine(i18Next.T("exampleKey"));

            Console.WriteLine("German translation:");
            i18Next.Language = "de";
            Console.WriteLine(i18Next.T("exampleKey"));

            i18Next.SetFallbackLanguages("en");
            Console.WriteLine(i18Next.T("exampleKey2")); // should output "My English text." because of fallback language

            Console.ReadKey();
        }
        /// <summary>
        /// This will never return null or an array containing any nulls, blank values or values with leading or trailing whitespace or values containing line
        /// returns (this format makes the Assert.Equals easier, where it can make array comparisons easily but not any IEnumerable implementation)
        /// </summary>
        public static string[] GetTranslatedStatements(string content, NonNullImmutableList <string> externalDependencies)
        {
            if (content == null)
            {
                throw new ArgumentNullException("content");
            }
            if (externalDependencies == null)
            {
                throw new ArgumentNullException("externalDependencies");
            }

            return(DefaultTranslator
                   .Translate(
                       content,
                       externalDependencies,
                       OuterScopeBlockTranslator.OutputTypeOptions.WithoutScaffolding,
                       renderCommentsAboutUndeclaredVariables: false
                       )
                   .Select(s => s.Content)
                   .Where(s => s != "")
                   .ToArray());
        }
Ejemplo n.º 13
0
        private static DefaultTranslator DefaultTranslatorFactory(IServiceProvider c)
        {
            var backend            = c.GetRequiredService <ITranslationBackend>();
            var logger             = c.GetRequiredService <ILogger>();
            var pluralResolver     = c.GetRequiredService <IPluralResolver>();
            var interpolator       = c.GetRequiredService <IInterpolator>();
            var postProcessors     = c.GetService <IEnumerable <IPostProcessor> >();
            var missingKeyHandlers = c.GetService <IEnumerable <IMissingKeyHandler> >();

            var instance = new DefaultTranslator(backend, logger, pluralResolver, interpolator);

            if (postProcessors != null)
            {
                instance.PostProcessors.AddRange(postProcessors);
            }

            if (missingKeyHandlers != null)
            {
                instance.MissingKeyHandlers.AddRange(missingKeyHandlers);
            }

            return(instance);
        }
Ejemplo n.º 14
0
        public void NoRuntimeDateLiteralPresent()
        {
            var source   = "If (a = #29 5 2015#) Then\nEnd If";
            var expected = @"
				using System;
				using System.Collections;
				using System.Runtime.InteropServices;
				using VBScriptTranslator.RuntimeSupport;
				using VBScriptTranslator.RuntimeSupport.Attributes;
				using VBScriptTranslator.RuntimeSupport.Exceptions;
				using VBScriptTranslator.RuntimeSupport.Compat;

				namespace TranslatedProgram
				{
					public class Runner
					{
						private readonly IProvideVBScriptCompatFunctionalityToIndividualRequests _;
						public Runner(IProvideVBScriptCompatFunctionalityToIndividualRequests compatLayer)
						{
							if (compatLayer == null)
								throw new ArgumentNullException(""compatLayer"");
							_ = compatLayer;
						}

						public GlobalReferences Go()
						{
							return Go(new EnvironmentReferences());
						}
						public GlobalReferences Go(EnvironmentReferences env)
						{
							if (env == null)
								throw new ArgumentNullException(""env"");

							var _env = env;
							var _outer = new GlobalReferences(_, _env);

							if (_.IF(_.EQ(_.NullableDATE(_env.a), _.DateLiteralParser.Parse(""29 5 2015""))))
							{
							}

							return _outer;
						}

						public class GlobalReferences
						{
							private readonly IProvideVBScriptCompatFunctionalityToIndividualRequests _;
							private readonly GlobalReferences _outer;
							private readonly EnvironmentReferences _env;
							public GlobalReferences(IProvideVBScriptCompatFunctionalityToIndividualRequests compatLayer, EnvironmentReferences env)
							{
								if (compatLayer == null)
									throw new ArgumentNullException(""compatLayer"");
								if (env == null)
									throw new ArgumentNullException(""env"");
								_ = compatLayer;
								_env = env;
								_outer = this;
							}
						}

						public class EnvironmentReferences
						{
							public object a { get; set; }
						}
					}
				}"                ;

            Assert.Equal(
                expected.Split(new[] { Environment.NewLine }, StringSplitOptions.None).Select(s => s.Trim()).Where(s => s != "").ToArray(),
                DefaultTranslator.Translate(source, new string[0], OuterScopeBlockTranslator.OutputTypeOptions.Executable).Select(s => s.Content.Trim()).Where(s => s != "").ToArray()
                );
        }
Ejemplo n.º 15
0
        public void WhenIProvideTheTranslatorWithASpaceGetASpaceBack()
        {
            ITranslator translator = new DefaultTranslator();

            Assert.Equal(" ", translator.Decrypt(" "));
        }
        public void UndeclaredLoopVariablesMustBeAddedToTheEnvironmentReferences()
        {
            var source   = @"
				For Each value In Array(1, 2)
				Next
			"            ;
            var expected = @"
				using System;
				using System.Collections;
				using System.Runtime.InteropServices;
				using VBScriptTranslator.RuntimeSupport;
				using VBScriptTranslator.RuntimeSupport.Attributes;
				using VBScriptTranslator.RuntimeSupport.Exceptions;
				using VBScriptTranslator.RuntimeSupport.Compat;
				namespace TranslatedProgram
				{
					public class Runner
					{
						private readonly IProvideVBScriptCompatFunctionalityToIndividualRequests _;
						public Runner(IProvideVBScriptCompatFunctionalityToIndividualRequests compatLayer)
						{
							if (compatLayer == null)
								throw new ArgumentNullException(""compatLayer"");
							_ = compatLayer;
						}
						public GlobalReferences Go()
						{
							return Go(new EnvironmentReferences());
						}
						public GlobalReferences Go(EnvironmentReferences env)
						{
							if (env == null)
								throw new ArgumentNullException(""env"");
							var _env = env;
							var _outer = new GlobalReferences(_, _env);
							var enumerationContent1 = _.ENUMERABLE(_.CALL(this, _, ""ARRAY"", _.ARGS.Val((Int16)1).Val((Int16)2))).GetEnumerator();
							while (true)
							{
								if (!enumerationContent1.MoveNext())
									break;
								_env.value = enumerationContent1.Current;
							}
							return _outer;
						}
						public class GlobalReferences
						{
							private readonly IProvideVBScriptCompatFunctionalityToIndividualRequests _;
							private readonly GlobalReferences _outer;
							private readonly EnvironmentReferences _env;
							public GlobalReferences(IProvideVBScriptCompatFunctionalityToIndividualRequests compatLayer, EnvironmentReferences env)
							{
								if (compatLayer == null)
									throw new ArgumentNullException(""compatLayer"");
								if (env == null)
									throw new ArgumentNullException(""env"");
								_ = compatLayer;
								_env = env;
								_outer = this;
							}
						}
						public class EnvironmentReferences
						{
							public object value { get; set; }
						}
					}
				}"                ;
            var output   = DefaultTranslator
                           .Translate(source, new string[0], renderCommentsAboutUndeclaredVariables: false)
                           .Select(s => s.Content)
                           .Where(s => s != "")
                           .ToArray();

            Assert.Equal(
                expected.Split(new[] { Environment.NewLine }, StringSplitOptions.None).Select(s => s.Trim()).Where(s => s != "").ToArray(),
                output.Select(s => s.Trim()).Where(s => s != "").ToArray()
                );
        }
 public void SetUp()
 {
     _translator = new DefaultTranslator(_backend, null, _pluralResolver, _interpolator);
 }
Ejemplo n.º 18
0
        public void RuntimeDateLiteralPresent()
        {
            var source   = "If (a = #29 May 2015#) Then\nEnd If";
            var expected = @"
				using System;
				using System.Collections;
				using System.Collections.ObjectModel;
				using System.Runtime.InteropServices;
				using VBScriptTranslator.RuntimeSupport;
				using VBScriptTranslator.RuntimeSupport.Attributes;
				using VBScriptTranslator.RuntimeSupport.Exceptions;
				using VBScriptTranslator.RuntimeSupport.Compat;

				namespace TranslatedProgram
				{
					public class Runner
					{
						private readonly IProvideVBScriptCompatFunctionalityToIndividualRequests _;
						public Runner(IProvideVBScriptCompatFunctionalityToIndividualRequests compatLayer)
						{
							if (compatLayer == null)
								throw new ArgumentNullException(""compatLayer"");
							_ = compatLayer;
						}

						public GlobalReferences Go()
						{
							return Go(new EnvironmentReferences());
						}
						public GlobalReferences Go(EnvironmentReferences env)
						{
							if (env == null)
								throw new ArgumentNullException(""env"");

							var _env = env;
							var _outer = new GlobalReferences(_, _env);
							RuntimeDateLiteralValidator.ValidateAgainstCurrentCulture(_);

							if (_.IF(_.EQ(_.NullableDATE(_env.a), _.DateLiteralParser.Parse(""29 May 2015""))))
							{
							}

							return _outer;
						}

						private static class RuntimeDateLiteralValidator
						{
							private static readonly ReadOnlyCollection<Tuple<string, int[]>> _literalsToValidate =
							new ReadOnlyCollection<Tuple<string, int[]>>(new[] {
								Tuple.Create(""29 May 2015"", new[] { 1 })
							});

							public static void ValidateAgainstCurrentCulture(IProvideVBScriptCompatFunctionalityToIndividualRequests compatLayer)
							{
								if (compatLayer == null)
									throw new ArgumentNullException(""compatLayer"");
								foreach (var dateLiteralValueAndLineNumbers in _literalsToValidate)
								{
									try { compatLayer.DateLiteralParser.Parse(dateLiteralValueAndLineNumbers.Item1); }
									catch
									{
										throw new SyntaxError(string.Format(
											""Invalid date literal #{0}# on line{1} {2}"",
											dateLiteralValueAndLineNumbers.Item1,
											(dateLiteralValueAndLineNumbers.Item2.Length == 1) ? """" : ""s"",
											string.Join<int>("", "", dateLiteralValueAndLineNumbers.Item2)
										));
									}
								}
							}
						}

						public class GlobalReferences
						{
							private readonly IProvideVBScriptCompatFunctionalityToIndividualRequests _;
							private readonly GlobalReferences _outer;
							private readonly EnvironmentReferences _env;
							public GlobalReferences(IProvideVBScriptCompatFunctionalityToIndividualRequests compatLayer, EnvironmentReferences env)
							{
								if (compatLayer == null)
									throw new ArgumentNullException(""compatLayer"");
								if (env == null)
									throw new ArgumentNullException(""env"");
								_ = compatLayer;
								_env = env;
								_outer = this;
							}
						}

						public class EnvironmentReferences
						{
							public object a { get; set; }
						}
					}
				}"                ;

            Assert.Equal(
                expected.Split(new[] { Environment.NewLine }, StringSplitOptions.None).Select(s => s.Trim()).Where(s => s != "").ToArray(),
                DefaultTranslator.Translate(source, new string[0], OuterScopeBlockTranslator.OutputTypeOptions.Executable).Select(s => s.Content.Trim()).Where(s => s != "").ToArray()
                );
        }
Ejemplo n.º 19
0
 public Translated()
 {
     Field      = FieldNames.Phrase;
     Translator = new DefaultTranslator();
 }