Exemplo n.º 1
0
        /// <summary>
        /// Retrieves the translation instance from the inner cache if the entry exists.
        /// If not, use the ITranslationService service to retrieve it from the database (and stores it into the inner cache).
        /// </summary>
        ///
        /// <param name="translationKey">
        /// Translation key.
        /// </param>
        ///
        /// <returns>
        /// Translation object.
        /// </returns>
        public Translation Resolve(TranslationEnum translationKey)
        {
            if (translationKey == TranslationEnum.None)             // special case
            {
                return(Translation.None);
            }

            string key = translationKey.ToString();

            var translation = this.GetFromCache(key);

            if (translation == null)
            {
                lock (_innerLocker)
                {
                    translation = this.GetFromCache(key);
                    if (translation == null)
                    {
                        using (var service = new ServiceProxy <ITranslationService>())
                        {
                            translation = service.Proxy.Resolve(ClientContext.Anonymous, translationKey);
                        }

                        if (translation != null)
                        {
                            _innerCacheService.Add(new CacheItem {
                                Key = key, Data = translation
                            });
                        }
                    }
                }
            }

            return(translation);
        }
Exemplo n.º 2
0
        private void PerformTranslationTest(string testName, string folderName, TranslationEnum translationTypeEnum, AbnfToAntlrTranslator translator)
        {
            var pathPrefix = Path.Combine(@"..\..\FileDrivenTests", folderName);

            var inputFileName  = testName + ".input.txt";
            var outputFileName = testName + ".output." + translationTypeEnum.ToString().ToLowerInvariant() + ".txt";

            var inputPath          = Path.Combine(pathPrefix, inputFileName);
            var expectedOutputPath = Path.Combine(pathPrefix, outputFileName);

            string inputText = null;

            if (File.Exists(inputPath))
            {
                // do nothing
            }
            else
            {
                File.WriteAllText(inputPath, "");
            }

            inputText = File.ReadAllText(inputPath);

            if (string.IsNullOrWhiteSpace(inputText))
            {
                TestContext.WriteLine(inputPath);
                throw new InvalidOperationException("File cannot be empty (\"" + inputPath + "\").");
            }

            var performDirectTranslation = (translationTypeEnum == TranslationEnum.Direct);

            var actualOutput = translator.Translate(inputText, performDirectTranslation);

            if (File.Exists(expectedOutputPath))
            {
                // do nothing
            }
            else
            {
                File.WriteAllText(expectedOutputPath, actualOutput);
            }

            var expectedOutput = File.ReadAllText(expectedOutputPath);

            if (actualOutput.Equals(expectedOutput, StringComparison.Ordinal))
            {
                // do nothing
            }
            else
            {
                TestContext.WriteLine(outputFileName);
                Assert.AreEqual(expectedOutput, actualOutput);
            }
        }
        public Translation Resolve(IUserContext userContext, TranslationEnum translationKey)
        {
            if (translationKey == TranslationEnum.None)             // special case
            {
                return(Translation.None);
            }

            using (var et = new ExecutionTracerService())
            {
                string key = translationKey.ToString();

                var translation = this.GetFromCache(key);
                if (translation == null)
                {
                    lock (_innerLocker)
                    {
                        translation = this.GetFromCache(key);
                        if (translation == null)
                        {
                            var options = new SearchOptions();

                            options.Filters.Add(Translation.ColumnNames.Key, FilterOperator.Equals, key);
                            options.MaxRecords = 1;

                            using (var db = new TranslationCrud(userContext))
                            {
                                translation = db.Search(ref options).FirstOrDefault();
                            }

                            if (translation != null)
                            {
                                _innerCacheService.Add(new CacheItem {
                                    Key = key, Data = translation
                                });
                            }
                        }
                    }
                }

                return(translation);
            }
        }
Exemplo n.º 4
0
 private static string ToMessage(TranslationEnum translation)
 {
     return(translation.ToString());
 }