public void ShouldReturnLocalizationFromDictionary() {
			var dictionary = new LocalizationDictionary { 
				{"sv-se","k","Key"}
			};
			var t = new FallbackDictionaryLocalizer(dictionary, "sv-se");
			Assert.Equal("Key", t.Localize("k"));
		}
 public void ShouldReturnValueFromDefaultLocalizerIfNotFound() {
   var dictionary = new LocalizationDictionary();
   var t = new FallbackDictionaryLocalizer(dictionary, "sv-se") {
     DefaultLocalizer = (k, p) => new LocalizedString(k, "default")
   };
   Assert.Equal("default", t.Localize("k"));
 }
		public void ShouldReturnFallbackLocalizationIfFullLocaleNotFound() {
			var dictionary = new LocalizationDictionary { 
				{"sv","k","Fallback"}
			};
			var t = new FallbackDictionaryLocalizer(dictionary, "sv-se","sv");
			Assert.Equal("Fallback", t.Localize("k"));
		}
		public FallbackDictionaryLocalizer(LocalizationDictionary dictionary, params string[] locales) {
		  if(dictionary == null) throw new ArgumentNullException("dictionary");
		  if(locales == null) throw new ArgumentNullException("locales");
      DefaultLocalizer = NullLocalizer.Instance;
      this.dictionary = dictionary;
			this.locales = locales;
		}
 public void ReadsIncludedDictionaries() {
   var resolver = Mock.Of<IResolver>(t =>
     t.Open("file.xml") == 
       ToStream(
         @"<?xml version=""1.0""?>
           <?mios-localization include=""second.xml"" prefix=""pfx""?>
           <dictionary>
             <key id=""first"">
               <val for=""fi"">abc</val>
               <val for=""sv-fi"">def</val>
             </key>
           </dictionary>") &&
     t.Open("second.xml") == 
       ToStream(
         @"<?xml version=""1.0""?>
           <dictionary>
             <key id=""second"">
               <val for=""fi"">ghi</val>
               <val for=""sv-fi"">jkl</val>
             </key>
           </dictionary>")
   );
   Mock.Get(resolver).Setup(t => t.Combine(It.IsAny<string>(), It.IsAny<string>())).Returns<string, string>((a, b) => b);
   var dictionary = new LocalizationDictionary();
   new XmlLocalizationReader("file.xml") {
     Resolver = resolver
   }.Read(dictionary);
   Assert.Equal("abc", dictionary["fi", "first"]);
   Assert.Equal("ghi", dictionary["fi", "pfxsecond"]);
 }
		public void AddPerservesKeyNameCase() {
			var dictionary = new LocalizationDictionary();
			dictionary["fi-fi", "UPPERCASE"] = "true";
			dictionary["fi-fi", "lowercase"] = "true";
			var keys = dictionary.Keys.ToArray();
			Assert.Contains("UPPERCASE", keys);
			Assert.Contains("lowercase", keys);
		}
		public void AddPerservesKeyOrder() {
			var dictionary = new LocalizationDictionary();
			dictionary["fi-fi", "UPPERCASE"] = "true";
			dictionary["sv-se", "lowercase"] = "true";
			dictionary["fi-se", "UPPERCASE"] = "true";
			var keys = dictionary.Keys.ToArray();
			Assert.Equal(new [] { "UPPERCASE", "lowercase"}, keys.ToArray());
		}
 private static void WriteIncludes(LocalizationDictionary dictionary, XmlWriter writer) {
   foreach(var include in dictionary.Includes) {
     var data = "include=\""+include.Path+"\"";
     if(!String.IsNullOrEmpty(include.Prefix)) {
       data += " prefix=\""+include.Prefix+"\"";
     }
     writer.WriteProcessingInstruction("mios-localization", data);
   }
 }
		public LocalizationDictionary Get(LocalizationDictionary request)
		{
			LocalizationRepository repository = GetRepository();
			if (request.Locale == null)
				return null;

			LocalizationCollectionEntity collection = repository.Read(request.Locale);

			return TranslateToResponse(collection);
		}
		public static string GetLabelFromLocalizationList(LocalizationDictionary localizationList, string name)
		{
			foreach (var obj in localizationList.LocalizationData)
			{
				if (obj.Key.Equals(name))
				{
					return obj.Value;
				}
			}
			return name;
		}
 public void WritesKeysToStream() {
   var dic = new LocalizationDictionary();
   dic["fi", "first"] = "ensimmäinen";
   dic["sv", "first"] = "första";
   dic["sv-fi", "first"] = "först";
   var writer = new StringWriter();
   new LegacyXmlLocalizationWriter().Write(dic, writer);
   var doc = XDocument.Parse(writer.ToString());
   var e = doc.Root.Element("key");
   Assert.Equal("first", e.Attribute("id").Value);
   Assert.Equal("ensimmäinen", e.Element("fi").Value);
   Assert.Equal("första", e.Element("sv").Value);
   Assert.Equal("först", e.Element("svFI").Value);
 }
 public void WritesIncludesToStream() {
   var dic = new LocalizationDictionary {
     Includes = { 
       new LocalizationDictionary.Include { Path = "second.xml", Prefix = "pfx" },
       new LocalizationDictionary.Include { Path = "third.xml" }
     }
   };
   dic["fi", "first"] = "ensimmäinen";
   var writer = new StringWriter();
   new XmlLocalizationWriter().Write(dic, writer);
   var doc = XDocument.Parse(writer.ToString());
   var pi = doc.Nodes().OfType<XProcessingInstruction>().First();
   Assert.Equal("mios-localization", pi.Target);
   Assert.Equal("include=\"second.xml\" prefix=\"pfx\"", pi.Data);
   pi = doc.Nodes().OfType<XProcessingInstruction>().Skip(1).First();
   Assert.Equal("mios-localization", pi.Target);
   Assert.Equal("include=\"third.xml\"", pi.Data);
 }
 public void RecordsIncludes() {
   var resolver = Mock.Of<IResolver>(t =>
     t.Open("file.xml") == 
       ToStream(
         @"<?xml version=""1.0"" encoding=""utf-8""?>
           <?mios-localization include=""second.xml""?>
           <dictionary>
             <key id=""test"">
               <val for=""fi""><![CDATA[Testaus]]></val>
               <val for=""sv""><![CDATA[Testning]]></val>
             </key>
           </dictionary>")
   );
   var dictionary = new LocalizationDictionary();
   new XmlLocalizationReader("file.xml") {
     Resolver = resolver, Recursive = false
   }.Read(dictionary);
   Assert.Contains(new LocalizationDictionary.Include { Path = "second.xml" }, dictionary.Includes);
 }
    public void WritesKeysToStream() {
      var dic = new LocalizationDictionary();
      dic["fi", "first"] = "ensimmäinen";
      dic["sv", "first"] = "första";
      dic["sv-fi", "first"] = "först";
      var writer = new StringWriter();
      new XmlLocalizationWriter().Write(dic, writer);
      var doc = XDocument.Parse(writer.ToString());
      Assert.Equal("dictionary", doc.Root.Name);
      var e = doc.Root.Element("key");
      Assert.Equal("first", e.Attribute("id").Value);

      var elements = e.Elements("val").ToArray();
      Assert.Equal("fi", elements[0].Attribute("for").Value);
      Assert.Equal("ensimmäinen", elements[0].Value);
      Assert.Equal("sv", elements[1].Attribute("for").Value);
      Assert.Equal("första", elements[1].Value);
      Assert.Equal("sv-fi", elements[2].Attribute("for").Value);
      Assert.Equal("först", elements[2].Value);
    }
		public static string GetL18nLabel(string name)
		{
			name = "_" + name + "_";

			//Add l18n here
			AuthService authService = new AuthService();
			var language = "nl-nl";
			try
			{
				var currentUser = authService.Get(new CurrentUserAuthRequest());
				language = currentUser.Language;
			}
			catch (Exception) { }

			LocalizationDictionary request = new LocalizationDictionary();
			request.LocalizationData.Add(new LocalizationItem() { Key = name });
			request.Locale = language;
			LocalizationDictionary localizationList = _client.Get<LocalizationDictionary>(request);

			return GetLabelFromLocalizationList(localizationList, name);
		}
		public static void ApplyLanguagingToLabels(List<INameLabel> responseList)
		{
			//Add l18n here
			AuthService authService = new AuthService();
			var language = "nl-nl";
			try
			{
				var currentUser = authService.Get(new CurrentUserAuthRequest());
				language = currentUser.Language;
			}
			catch (Exception) { }

			LocalizationDictionary request = new LocalizationDictionary();
			request.Locale = language;
			LocalizationDictionary localizationList = _client.Get<LocalizationDictionary>(request);

			foreach (var obj in responseList)
			{
				obj.Label = GetLabelFromLocalizationList(localizationList, "_" + obj.Name + "_");
			}
		}
 public void ReadsIncludedDictionariesOnlyIfSpecified() {
   var resolver = Mock.Of<IResolver>(t =>
     t.Open("file.xml") == 
       ToStream(
         @"<?xml version=""1.0""?>
           <?mios-localization include=""second.xml""?>
           <dictionary>
             <key id=""first"">
               <val for=""fi"">abc</val>
               <val for=""sv-fi"">def</val>
             </key>
           </dictionary>") &&
     t.Open("second.xml") == 
       ToStream(@"<?xml version=""1.0""?><dictionary/>")
   );
   Mock.Get(resolver).Setup(t => t.Combine(It.IsAny<string>(), It.IsAny<string>())).Returns<string, string>((a, b) => b);
   var dictionary = new LocalizationDictionary();
   new XmlLocalizationReader("file.xml") {
     Resolver = resolver, Recursive = false
   }.Read(dictionary);
   Mock.Get(resolver).Verify(t => t.Open("second.xml"), Times.Never());
 }
 public void RemapsLocaleStrings() {
   const string source = @"<?xml version=""1.0""?><page><key id=""first""><fi>ensimmäinen</fi><sv>första</sv><svFI>först</svFI></key></page>";
   var dic = new LocalizationDictionary();
   new LegacyXmlLocalizationReader().Read(new StringReader(source),dic);
   Assert.Equal("först", dic["sv-fi", "first"]);
 }
		private LocalizationDictionary PutPostLocalizationDictionary(LocalizationDictionary request)
		{
			LocalizationCollectionEntity collection = TranslateToEntity(request);
			LocalizationRepository repository = GetRepository();

			return TranslateToResponse(repository.Create(collection));
		}
		private static LocalizationCollectionEntity TranslateToEntity(LocalizationDictionary request)
		{
			LocalizationCollectionEntity response = request.TranslateTo<LocalizationCollectionEntity>();

			request.LocalizationData.ForEach(x => response.LocalizationItems.Add(x.TranslateTo<LocalizationEntity>()));

			return response;
		}
 public void ShouldReturnEmptyLocalizedStringIfKeyIsNull() {
   var dictionary = new LocalizationDictionary();
   var t = new FallbackDictionaryLocalizer(dictionary, "sv-se");
   Assert.Null(t.Localize(null).Localization);
 }
		public LocalizationDictionary Put(LocalizationDictionary request)
		{
			return PutPostLocalizationDictionary(request);
		}
		public void Delete(LocalizationDictionary request)
		{
			var repository = GetRepository();
			repository.Delete(request.Locale);
		}
 public void SkipsUnknownElements() {
   var resolver = Mock.Of<IResolver>(t =>
     t.Open(It.IsAny<string>()) == 
       ToStream(
         @"<?xml version=""1.0""?>
           <dictionary>
             <garbage/>
             <key id=""xyz"">
               <random>This is text</random>
               <val for=""fi"">abc</val>
               <val for=""sv-fi"">def</val>
             </key>
           </dictionary>")
         );
   var dictionary = new LocalizationDictionary();
   new XmlLocalizationReader("file.xml") {
     Resolver = resolver
   }.Read(dictionary);
   Assert.Equal("abc", dictionary["fi", "xyz"]);
   Assert.Equal("def", dictionary["sv-fi", "xyz"]);
 }
    public void ReadsCompactFiles() {
      var resolver = Mock.Of<IResolver>(t =>
        t.Open(It.IsAny<string>()) == 
          ToStream(
@"<?xml version=""1.0""?>
<dictionary>
<key id=""xyz""><val for=""fi""></val></key>
<key id=""pqr""><val for=""fi""></val></key>
</dictionary>")
      );
      var dictionary = new LocalizationDictionary();
      new XmlLocalizationReader("file.xml") {
        Resolver = resolver
      }.Read(dictionary);
      Assert.Contains("xyz", dictionary.Keys);
      Assert.Contains("pqr", dictionary.Keys);
      Assert.Equal("", dictionary["fi", "xyz"]);
      Assert.Equal("", dictionary["fi", "pqr"]);
    }
 public void ReadsMultipleKeyAndValues() {
   var resolver = Mock.Of<IResolver>(t =>
     t.Open(It.IsAny<string>()) == 
       ToStream(
         @"<?xml version=""1.0""?>
           <dictionary>
             <key id=""xyz"">
               <val for=""fi"">abc</val>
               <val for=""sv-fi"">def</val>
             </key>
             <key id=""pqr"">
               <val for=""fi"">123</val>
               <val for=""en"">456</val>
             </key>
           </dictionary>")
   );
   var dictionary = new LocalizationDictionary();
   new XmlLocalizationReader("file.xml") {
     Resolver = resolver
   }.Read(dictionary);
   Assert.Equal("abc", dictionary["fi", "xyz"]);
   Assert.Equal("def", dictionary["sv-fi", "xyz"]);
   Assert.Equal("123", dictionary["fi", "pqr"]);
   Assert.Equal("456", dictionary["en", "pqr"]);
 }
    public void ReadsRelativeDictionaries() {
      var resolver = Mock.Of<IResolver>(t =>
        t.Combine("a/file.xml","../b/second.xml") == "b/second.xml" &&
        t.Open("a/file.xml") == 
          ToStream(
            @"<?xml version=""1.0""?>
              <?mios-localization include=""../b/second.xml""?>
              <dictionary>
                <key id=""first"">
                  <val for=""fi"">abc</val>
                  <val for=""sv-fi"">def</val>
                </key>
              </dictionary>") &&
        t.Open("b/second.xml") == 
          ToStream(
            @"<?xml version=""1.0""?>
              <dictionary>
                <key id=""second"">
                  <val for=""fi"">ghi</val>
                  <val for=""sv-fi"">jkl</val>
                </key>
              </dictionary>")

      );
      var dictionary = new LocalizationDictionary();
      new XmlLocalizationReader("a/file.xml") {
        Resolver = resolver
      }.Read(dictionary);
      Assert.Equal("abc", dictionary["fi", "first"]);
      Assert.Equal("ghi", dictionary["fi", "second"]);
    }