Пример #1
0
 /// <summary>
 /// Restores the specified obj into neutral language.
 /// </summary>
 /// <param name="type">The type.</param>
 /// <param name="obj">The obj.</param>
 /// <param name="translation">The translation.</param>
 public static void Restore(Type type, object obj, Translation translation)
 {
     Invoke(type, MethodRestore,
            new Type[] { obj.GetType(), translation.GetType() },
            new object[] { obj, translation });
 }
Пример #2
0
        /// <summary>
        /// Creates StringBuilder with C# code of translation.
        /// </summary>
        /// <param name="translation">The translation.</param>
        /// <param name="runtimeNamespace">The runtime namespace.</param>
        /// <param name="runtimeClassName">Name of the runtime class.</param>
        /// <returns></returns>
        public static StringBuilder ToCSharpCode(Translation translation,
                                                 string runtimeNamespace, string runtimeClassName)
        {
            StringBuilder str = new StringBuilder();

            str.AppendLine("// NLocalizer (C) Krzysztof Arkadiusz Prusik, NLocalizer.codeplex.com");
            string lang = "";

            foreach (KeyValuePair <string, TranslationClasses> item in translation)
            {
                lang += item.Key + ", ";
            }
            str.AppendLine(String.Format("// Code file to languages: {0} autogenerated {1}",
                                         lang, DateTime.Now.ToShortDateString()));
            str.AppendLine("// $Id" + "$");
            str.AppendLine();
            foreach (string usingName in translation.CodeUsings)
            {
                str.AppendLine(String.Format("using {0};", usingName));
            }
            str.AppendLine();
            str.AppendLine("namespace " + runtimeNamespace);
            str.AppendLine("{");
            str.AppendLine("    public static class " + runtimeClassName);
            str.AppendLine("    {");

            if (translation.Exists("English"))
            {
                foreach (KeyValuePair <string, TranslationProperties> item in translation["English"])
                {
                    str.Append(ToCSharpCode(item.Key, translation));
                }

                str.AppendLine("        public static void Translate()");
                str.AppendLine("        {");
                str.AppendLine("            Translate(Translator.Translation);");
                str.AppendLine("        }");
                str.AppendLine();

                str.AppendLine("        public static void Translate(Translation translation)");
                str.AppendLine("        {");
                str.AppendLine("            Translate(translation.CurrentLanguage, translation);");
                str.AppendLine("        }");
                str.AppendLine();

                str.AppendLine("        public static void Translate(string language, Translation translation)");
                str.AppendLine("        {");
                foreach (string staticClassName in translation.StaticClasses)
                {
                    str.AppendLine(String.Format("            Translate{0}(language, translation);", staticClassName));
                }
                str.AppendLine("        }");
                str.AppendLine();

                str.AppendLine("        public static void Remember()");
                str.AppendLine("        {");
                str.AppendLine("            Remember(Translator.Translation);");
                str.AppendLine("        }");
                str.AppendLine();

                str.AppendLine("        public static void Remember(Translation translation)");
                str.AppendLine("        {");
                foreach (string staticClassName in translation.StaticClasses)
                {
                    str.AppendLine(String.Format("            Remember{0}(translation);", staticClassName));
                }
                str.AppendLine("        }");
                str.AppendLine();

                str.AppendLine("        public static void Restore()");
                str.AppendLine("        {");
                str.AppendLine("            Restore(Translator.Translation);");
                str.AppendLine("        }");
                str.AppendLine();

                str.AppendLine("        public static void Restore(Translation translation)");
                str.AppendLine("        {");
                foreach (string staticClassName in translation.StaticClasses)
                {
                    str.AppendLine(String.Format("            Restore{0}(translation);", staticClassName));
                }
                str.AppendLine("        }");
                str.AppendLine();
            }

            str.AppendLine("    }");
            str.AppendLine("}");
            return(str);
        }
Пример #3
0
 /// <summary>
 /// Translates the specified obj into language.
 /// </summary>
 /// <param name="type">The type.</param>
 /// <param name="language">The language.</param>
 /// <param name="obj">The obj.</param>
 /// <param name="translation">The translation.</param>
 public static void Translate(Type type, string language, object obj, Translation translation)
 {
     Invoke(type, MethodTranslate,
            new Type[] { language.GetType(), obj.GetType(), translation.GetType() },
            new object[] { language, obj, translation });
 }
Пример #4
0
 /// <summary>
 /// Translates the specified obj into current language.
 /// </summary>
 /// <param name="type">The type.</param>
 /// <param name="obj">The obj.</param>
 /// <param name="translation">The translation.</param>
 public static void Translate(Type type, object obj, Translation translation)
 {
     Translate(type, translation.CurrentLanguage, obj, translation);
 }
Пример #5
0
 /// <summary>
 /// Translates the specified type.
 /// </summary>
 /// <param name="type">The type.</param>
 /// <param name="translation">The translation.</param>
 public static void Translate(Type type, Translation translation)
 {
     Invoke(type, MethodTranslate,
            new Type[] { translation.GetType() },
            new object[] { translation });
 }
Пример #6
0
 /// <summary>
 /// Creates StringBuilder from translation.
 /// </summary>
 /// <param name="translation">The translation.</param>
 /// <returns></returns>
 public static StringBuilder ToCSharpCode(Translation translation)
 {
     return(ToCSharpCode(translation, Namespace, ClassName));
 }
Пример #7
0
 /// <summary>
 /// Compiles the specified translation.
 /// </summary>
 /// <param name="translation">The translation.</param>
 /// <returns></returns>
 public static CompilerResults Compile(Translation translation)
 {
     return(Compile(translation, Namespace, ClassName));
 }
Пример #8
0
 /// <summary>
 /// Compiles the specified translation.
 /// </summary>
 /// <param name="translation">The translation.</param>
 /// <param name="runtimeNamespace">The runtime namespace.</param>
 /// <param name="runtimeClassName">Name of the runtime class.</param>
 /// <returns></returns>
 public static CompilerResults Compile(Translation translation,
                                       string runtimeNamespace, string runtimeClassName)
 {
     return(Compile(translation, GetDefaultCompileDirectory(), runtimeNamespace, runtimeClassName));
 }
Пример #9
0
 /// <summary>
 /// Compiles the specified translation in directory.
 /// </summary>
 /// <param name="translation">The translation.</param>
 /// <param name="directoryName">Name of the directory.</param>
 /// <returns></returns>
 public static CompilerResults Compile(Translation translation, string directoryName)
 {
     return(Compile(translation, directoryName, Namespace, ClassName));
 }
Пример #10
0
        /// <summary>
        /// Creates StringBuilder with C# code of translation class.
        /// </summary>
        /// <param name="className">Name of the class.</param>
        /// <param name="translation">The translation.</param>
        /// <returns></returns>
        public static StringBuilder ToCSharpCode(string className, Translation translation)
        {
            StringBuilder str = new StringBuilder();

            if (translation.StaticClasses.Contains(className))
            {
                str.AppendLine(String.Format("        public static void Remember{0}()", className));
                str.AppendLine("        {");
                str.AppendLine(String.Format("            Remember{0}(Translator.Translation);", className));
                str.AppendLine("        }");
                str.AppendLine();

                str.AppendLine(String.Format("        public static void Remember{0}(Translation translation)", className));
                str.AppendLine("        {");

                if (translation.Exists("English"))
                {
                    foreach (KeyValuePair <string, string> item in translation["English"][className])
                    {
                        str.AppendLine(String.Format("            translation.SetText(\"Neutral\", \"{0}\", \"{1}\", {0}.{1});", className, item.Key));
                    }
                }
                str.AppendLine("        }");
                str.AppendLine();

                str.AppendLine(String.Format("        public static void Restore{0}()", className));
                str.AppendLine("        {");
                str.AppendLine(String.Format("            Restore{0}(Translator.Translation);", className));
                str.AppendLine("        }");
                str.AppendLine();

                str.AppendLine(String.Format("        public static void Restore{0}(Translation translation)", className));
                str.AppendLine("        {");
                str.AppendLine(String.Format("            if (translation.Exists(\"Neutral\", \"{0}\"))", className));
                str.AppendLine(String.Format("                Translate{0}(\"Neutral\", translation);", className));
                str.AppendLine("        }");
                str.AppendLine();

                str.AppendLine(String.Format("        public static void Translate{0}(string language)", className));
                str.AppendLine("        {");
                str.AppendLine(String.Format("            Translate{0}(language, Translator.Translation);", className));
                str.AppendLine("        }");
                str.AppendLine();

                str.AppendLine(String.Format("        public static void Translate{0}(string language, Translation translation)", className));
                str.AppendLine("        {");
                str.AppendLine(String.Format("            if (translation.Exists(\"Neutral\", \"{0}\") == false)", className));
                str.AppendLine(String.Format("                Remember{0}(translation);", className));

                str.AppendLine();
                if (translation.Exists("English", className))
                {
                    foreach (KeyValuePair <string, string> item in translation["English"][className])
                    {
                        str.AppendLine(String.Format("            {0}.{1} = translation.GetText(language, \"{0}\", \"{1}\");",
                                                     className, item.Key));
                    }
                }
                str.AppendLine("        }");
            }
            else
            {
                str.AppendLine(String.Format("        public static {0} Create{0}()", className));
                str.AppendLine("        {");
                str.AppendLine(String.Format("            return new {0}();", className));
                str.AppendLine("        }");
                str.AppendLine();

                str.AppendLine(String.Format("        public static void Remember({0} obj)", className));
                str.AppendLine("        {");
                str.AppendLine("            Remember(obj, Translator.Translation);");
                str.AppendLine("        }");
                str.AppendLine();

                str.AppendLine(String.Format("        public static void Remember({0} obj, Translation translation)", className));
                str.AppendLine("        {");
                if (translation.Exists("English"))
                {
                    foreach (KeyValuePair <string, string> item in translation["English"][className])
                    {
                        str.AppendLine(String.Format("            translation.SetText(\"Neutral\", \"{0}\", \"{1}\", obj.{1});", className, item.Key));
                    }

                    foreach (string staticClassName in translation.StaticClasses)
                    {
                        str.AppendLine(String.Format("            if (translation.Exists(\"Neutral\", \"{0}\") == false)", staticClassName));
                        str.AppendLine(String.Format("                Remember{0}(translation);", staticClassName));
                    }
                }
                str.AppendLine("        }");
                str.AppendLine();

                str.AppendLine(String.Format("        public static void Restore({0} obj)", className));
                str.AppendLine("        {");
                str.AppendLine("            Restore(obj, Translator.Translation);");
                str.AppendLine("        }");
                str.AppendLine();

                str.AppendLine(String.Format("        public static void Restore({0} obj, Translation translation)", className));
                str.AppendLine("        {");
                str.AppendLine(String.Format("            if (translation.Exists(\"Neutral\", \"{0}\"))", className));
                str.AppendLine("                Translate(\"Neutral\", obj, translation);");
                str.AppendLine("        }");
                str.AppendLine();

                str.AppendLine(String.Format("        public static void Translate(string language, {0} obj)", className));
                str.AppendLine("        {");
                str.AppendLine("            Translate(language, obj, Translator.Translation);");
                str.AppendLine("        }");
                str.AppendLine();

                str.AppendLine(String.Format("        public static void Translate(string language, {0} obj, Translation translation)", className));
                str.AppendLine("        {");
                str.AppendLine(String.Format("            if (translation.Exists(\"Neutral\", \"{0}\") == false)", className));
                str.AppendLine("                Remember(obj, translation);");
                str.AppendLine();
                if (translation.Exists("English", className))
                {
                    foreach (KeyValuePair <string, string> item in translation["English"][className])
                    {
                        str.AppendLine(String.Format("            obj.{1} = translation.GetText(language, \"{0}\", \"{1}\");",
                                                     className, item.Key));
                    }
                }
                str.AppendLine("        }");
            }
            return(str);
        }
Пример #11
0
        /// <summary>
        /// Create string builder from the specified translation.
        /// </summary>
        /// <param name="language">The language.</param>
        /// <param name="translation">The translation.</param>
        /// <returns></returns>
        public static StringBuilder ToStringBuilder(string language, Translation translation)
        {
            var str = new StringBuilder();

            str.AppendLine("; NLocalizer (C) Chris Prusik (KAP1 Ltd www.kap1.net), NLocalizer.codeplex.com");
            str.AppendLine(String.Format("; Language file: {0}, autogenerated {1}",
                                         language, DateTime.Now.ToShortDateString()));
            str.AppendLine("; $Id" + "$");
            str.AppendLine(";");
            str.AppendLine("; !(ClassName)StaticPropertyName = translated message");
            str.AppendLine("; (ClassName)DynamicPropertyName = translated message");
            str.AppendLine("; module FileName1.dll, FileName2.dll");
            str.AppendLine("; using Namespace1, Namespace2");
            str.AppendLine("; static StaticClassName1, StaticClassName2");
            str.AppendLine("; template file.tpl");
            str.AppendLine("; debug file.cs");
            str.AppendLine("; log file.log");
            str.AppendLine(";");

            string textLocales = "";

            foreach (KeyValuePair <string, string> localeItem in translation.Locales)
            {
                if (localeItem.Value == language)
                {
                    textLocales += localeItem.Key + ", ";
                }
            }
            if (textLocales != "")
            {
                str.AppendLine("locale " + textLocales.Remove(textLocales.Length - 2));
            }

            if (translation.CodeDlls.Count > 0)
            {
                string text = "";
                foreach (string moduleItem in translation.CodeDlls)
                {
                    text += moduleItem + ", ";
                }
                str.AppendLine("module " + text.Remove(text.Length - 2));
            }

            if (translation.CodeUsings.Count > 0)
            {
                string text = "";
                foreach (string usingItem in translation.CodeUsings)
                {
                    text += usingItem + ", ";
                }
                str.AppendLine("using " + text.Remove(text.Length - 2));
            }

            if (translation.StaticClasses.Count > 0)
            {
                string text = "";
                foreach (string staticItem in translation.StaticClasses)
                {
                    text += staticItem + ", ";
                }
                str.AppendLine("static " + text.Remove(text.Length - 2));
            }

            if (translation.Exists(language))
            {
                foreach (KeyValuePair <string, TranslationProperties> classItem in translation[language])
                {
                    foreach (KeyValuePair <string, TranslationProperty> propertyItem in classItem.Value)
                    {
                        string modifier = "";
                        if (propertyItem.Value.IsStatic)
                        {
                            modifier = "!";
                        }
                        str.AppendLine(String.Format("{0}({1}){2} = {3}",
                                                     modifier, classItem.Key, propertyItem.Key,
                                                     ReplaceSpecialChars(propertyItem.Value.Message)));
                    }
                }
            }
            return(str);
        }
Пример #12
0
 /// <summary>
 /// Write all the specified translation into files in the application directory.
 /// </summary>
 /// <param name="translation">The translation.</param>
 public static void Write(Translation translation)
 {
     Write(Path.GetDirectoryName(Application.ExecutablePath), translation);
 }
Пример #13
0
 /// <summary>
 /// Write the specified language from translation into the directory name.
 /// </summary>
 /// <param name="directoryName">Name of the directory.</param>
 /// <param name="language">The language.</param>
 /// <param name="translation">The translation.</param>
 public static void Write(string directoryName, string language, Translation translation)
 {
     File.WriteAllText(Path.Combine(directoryName, language + ".lang"),
                       ToStringBuilder(language, translation).ToString());
 }