private TypeLocalizationDocument GetOrCreateLanguage(CultureInfo culture)
        {
            TypeLocalizationDocument document;

            lock (Cache)
            {
                if (Cache.TryGetValue(culture.LCID, out document))
                {
                    return(document);
                }
            }

            document = _documentSession.Load <TypeLocalizationDocument>(culture.Name);
            if (document == null)
            {
                _logger.Debug("Failed to find document for " + culture.Name + ", creating it.");
                var defaultLang = DefaultUICulture.Is(culture)
                                                                                                                        ? new TypeLocalizationDocument {
                    Id = culture.Name, Prompts = new List <TypePromptDocument>()
                }
                                                                                                                        : GetOrCreateLanguage(DefaultUICulture.Value);

                document = defaultLang.Clone(culture);
                _documentSession.Store(document);
                _documentSession.SaveChanges();
            }

            lock (Cache)
                Cache[culture.LCID] = document;

            return(document);
        }
Ejemplo n.º 2
0
        public void ChangeCultureToGbAndVerify()
        {
            DefaultUICulture.Set(new CultureInfo("en-gb"));

            Assert.AreEqual(new CultureInfo("en-gb").LCID, DefaultUICulture.LCID);
            Assert.IsTrue(DefaultUICulture.Is(new CultureInfo("en")));
            Assert.IsTrue(DefaultUICulture.Is(new CultureInfo("en-gb")));
            Assert.IsFalse(DefaultUICulture.IsActive);
        }
Ejemplo n.º 3
0
        public void ChangeCultureToSwedishAndVerify()
        {
            DefaultUICulture.Set(new CultureInfo(1053));

            Assert.AreEqual(1053, DefaultUICulture.LCID);
            Assert.IsFalse(DefaultUICulture.Is(new CultureInfo("en")));
            Assert.IsTrue(DefaultUICulture.Is(new CultureInfo(1053)));
            Assert.IsFalse(DefaultUICulture.IsActive);
            Assert.IsFalse(DefaultUICulture.IsEnglish);
        }
Ejemplo n.º 4
0
        public MvcHtmlString DisplayFor <TProperty>(Expression <Func <TModel, TProperty> > property)
        {
            var languageProvider = DependencyResolver.Current.GetService <ILocalizedStringProvider>();

            var metadata = ModelMetadata.FromStringExpression("", this.ViewData);

            if (metadata.Model == null)
            {
                return(new MvcHtmlString(metadata.NullDisplayText));
            }

            var value  = metadata.Model.ToString();
            var result =
                metadata.Model is Enum?
                languageProvider.GetEnumString(getNonNullableModelType(metadata), value) ?? DefaultUICulture.FormatUnknown(value) :
                    languageProvider.GetModelString(metadata.ModelType, value) ?? DefaultUICulture.FormatUnknown(value);

            return(new MvcHtmlString(result));
        }
Ejemplo n.º 5
0
        public void TestNotTranslatedProperty()
        {
            DefaultUICulture.Reset();
            var stringProvider = new Mock <ILocalizedStringProvider>();
            var provider       = new LocalizedModelMetadataProvider(stringProvider.Object);

            stringProvider.Setup(
                k =>
                k.GetModelString(It.Is <Type>(t => t == typeof(TestModel)), It.Is <string>(t => t == "Required"))).
            Returns((string)null).Verifiable();

            var subject = new TestModel
            {
                RequiredStringLength10 = "Arne",
                Required = "hej"
            };

            var actual = provider.GetMetadataForProperty(() => subject, typeof(TestModel), "Required");

            Assert.Equal(null, actual.DisplayName);
        }
Ejemplo n.º 6
0
 public void DefaulIsUsEnglish()
 {
     Assert.IsTrue(DefaultUICulture.Is(new CultureInfo("en-us")));
 }
Ejemplo n.º 7
0
 public void DefaultIsAlsoNeutral()
 {
     Assert.IsTrue(DefaultUICulture.Is(new CultureInfo("en")));
 }
Ejemplo n.º 8
0
 public void ResetCulture()
 {
     DefaultUICulture.Reset();
     Thread.CurrentThread.CurrentUICulture = new CultureInfo(1033);
 }
Ejemplo n.º 9
0
 /// <summary>
 /// Format output for a missing prompt
 /// </summary>
 /// <param name="text">View text</param>
 /// <returns>Plain text if default culture; otherwise culture tagged text.</returns>
 /// <example>
 /// Default culture:
 /// <code>
 /// localizer.FormatMissingPrompt("Hello world"); // --> hello world
 /// </code>
 /// Another culture:
 /// <code>
 /// localizer.FormatMissingPrompt("Hello world"); // --> [sv-se: hello world]
 /// </code>
 /// </example>
 protected virtual string FormatMissingPrompt(string text)
 {
     return(DefaultUICulture.Is(CultureInfo.CurrentUICulture)
                ? text
                : string.Format("{1}:[{0}]", text, CultureInfo.CurrentUICulture));
 }
 public DefaultCultureTests()
 {
     DefaultUICulture.Reset();
 }