Пример #1
0
        /// <summary>
        /// Create translate resource
        /// </summary>
        /// <param name="template"></param>
        /// <typeparam name="TTemplate"></typeparam>
        /// <returns></returns>
        /// <exception cref="ArgumentNullException"></exception>
        public static ITranslateResource Create <TTemplate>(TTemplate template) where TTemplate : StandardLocalizationTemplate, ILocalizationTemplate, new()
        {
            if (template == null)
            {
                throw new ArgumentNullException(nameof(template));
            }

            var anonymousTemplate = template.IsAnonymousTemplate();

            if (!anonymousTemplate && string.IsNullOrWhiteSpace(template.Name))
            {
                throw new ArgumentNullException(nameof(template.Name));
            }

            var languageTag = LanguageTagManager.Create(template.Language);

            if (languageTag == null)
            {
                throw new ArgumentException($"Cannot resolve an instance of LanguageTag by '{template.Language}'.");
            }

            return(anonymousTemplate
                ? new TranslateResource(
                       languageTag,
                       TranslationManager.ANONYMOUS_RESOURCE_NAME,
                       template.Contents.ToDictionary(k => k.Key.GetHashCode(), v => v.Value))
                : new TranslateResource(
                       languageTag,
                       template.Name,
                       template.Contents.ToDictionary(k => k.Key.GetHashCode(), v => v.Value)));
        }
Пример #2
0
 private void MatchTagHelper1(
     int expected,
     string lhs,
     string rhs,
     MatchLevel matchLevel = MatchLevel.LanguageMatch)
 {
     Assert.Equal(expected, (LanguageTagManager.Create(lhs).Match(LanguageTagManager.Create(rhs), matchLevel)));
     Assert.Equal(expected, (LanguageTagManager.Create(rhs).Match(LanguageTagManager.Create(lhs), matchLevel)));
 }
Пример #3
0
        /// <summary>
        /// Create Anonymous Localization Translate resource
        /// </summary>
        /// <param name="template"></param>
        /// <returns></returns>
        /// <exception cref="ArgumentNullException"></exception>
        /// <exception cref="InvalidOperationException"></exception>
        /// <exception cref="ArgumentException"></exception>
        public static ITranslateResource Create(AnonymousLocalizationTemplate template)
        {
            if (template == null)
            {
                throw new ArgumentNullException(nameof(template));
            }

            if (!template.CheckMySelf())
            {
                throw new InvalidOperationException("The AnonymousLocalizationTemplate has been illegally tampered with.");
            }

            var languageTag = LanguageTagManager.Create(template.Language);

            if (languageTag == null)
            {
                throw new ArgumentException($"Cannot resolve an instance of LanguageTag by '{template.Language}'.");
            }

            return(new TranslateResource(
                       languageTag,
                       TranslationManager.ANONYMOUS_RESOURCE_NAME,
                       template.Contents.ToDictionary(k => k.Key.GetHashCode(), v => v.Value)));
        }