Пример #1
0
        public static string TR(string name)
        {
            try
            {
                if (Provider.Configuration.DefaultLang == Provider.CurrentLanguage.Id || string.IsNullOrWhiteSpace(name))
                    return name;

                Dictionary<string, int> sr = null;
                Dictionary<string, string> srl = null;

                cacheResources();

                sr = (Dictionary<string, int>)HttpContext.Current.Cache["StaticResource"];
                srl = (Dictionary<string, string>)HttpContext.Current.Cache["StaticResourceLang"];

                if (!sr.ContainsKey(name))
                {
                    StaticResource newSR = new StaticResource { Name = name };
                    newSR.Save();
                    sr[name] = newSR.Id;
                    //HttpContext.Current.Cache["StaticResource"] = sr;

                    try
                    {
                        var defLang = Provider.Database.Read<Lang>(Provider.Configuration.DefaultLang);
                        foreach (Lang l in Provider.Database.ReadList<Lang>("select * from Lang where Id<>{0}", Provider.Configuration.DefaultLang))
                        {
                            string url = "http://translate.google.com/translate_a/t?client=t&sl=" + defLang.Code.Split('-')[0] + "&tl=" + l.Code.Split('-')[0] + "&hl=en&sc=2&ie=UTF-8&oe=UTF-8&oc=1&otf=1&ssel=4&tsel=0&q=" + Provider.Server.UrlEncode(name);
                            Encoding resolvedEncoding = Encoding.UTF8;
                            string translate = url.DownloadPage(ref resolvedEncoding).SplitWithTrim('"')[1];
                            if (char.IsUpper(name[0]) && char.IsLower(translate[0])) translate = translate.CapitalizeFirstLetterInvariant();
                            StaticResourceLang newSRL = new StaticResourceLang
                            {
                                LangId = l.Id,
                                StaticResourceId = newSR.Id,
                                Translation = translate
                            };
                            newSRL.Save();
                            srl[newSR.Id + "_" + newSRL.LangId] = newSRL.Translation;
                            //HttpContext.Current.Cache["StaticResourceLang"] = srl;

                            if (Provider.CurrentLanguage.Id == l.Id)
                                name = newSRL.Translation;
                        }
                    }
                    catch { }

                    return name;
                }

                int srItem = sr[name];

                if (!srl.ContainsKey(srItem + "_" + Provider.CurrentLanguage.Id))
                {
                    var defLang = Provider.Database.Read<Lang>(Provider.Configuration.DefaultLang);
                    Lang l = Provider.CurrentLanguage;
                    string translate = "";
                    try
                    {
                        string url = "http://translate.google.com/translate_a/t?client=t&sl=" + defLang.Code.Split('-')[0] + "&tl=" + l.Code.Split('-')[0] + "&hl=en&sc=2&ie=UTF-8&oe=UTF-8&oc=1&otf=1&ssel=4&tsel=0&q=" + Provider.Server.UrlEncode(name);
                        Encoding resolvedEncoding = Encoding.UTF8;
                        translate = url.DownloadPage(ref resolvedEncoding).SplitWithTrim('"')[1];
                        if (char.IsUpper(name[0]) && char.IsLower(translate[0])) translate = translate.CapitalizeFirstLetterInvariant();
                    }
                    catch
                    {
                        translate = name;
                    }

                    StaticResourceLang newSRL = new StaticResourceLang
                    {
                        LangId = l.Id,
                        StaticResourceId = srItem,
                        Translation = translate
                    };
                    newSRL.Save();
                    srl[srItem + "_" + newSRL.LangId] = newSRL.Translation;
                    name = newSRL.Translation;

                    HttpContext.Current.Cache["StaticResourceLang"] = srl;

                    return name;

                }
                else
                    return srl[srItem + "_" + Provider.CurrentLanguage.Id];
            }
            catch (Exception ex)
            {
                return name + "<font color=red>!!!</font>";
            }
        }
Пример #2
0
        private void importLocalization()
        {
            string xmlData = context.Request["xmlData"];

            XmlDocument doc = new XmlDocument();
            doc.LoadXml(xmlData);

            Provider.Database.Execute(() =>
                {
                    HttpContext.Current.Cache.Remove("StaticResource");
                    HttpContext.Current.Cache.Remove("StaticResourceLang");

                    Provider.Database.Read<StaticResource>(1);
                    Provider.Database.Read<StaticResourceLang>(1);

                    Provider.Database.ExecuteNonQuery("truncate table StaticResourceLang;");
                    Provider.Database.ExecuteNonQuery("truncate table StaticResource;");

                    string[] defLangArr = Lang.GetLangFullCodeAndName(doc.FirstChild.Attributes["defaultLang"].Value);
                    Lang def = Provider.Database.Read<Lang>("Code={0}", defLangArr[0]);
                    if (def == null)
                    {
                        def = new Lang { Name = defLangArr[1], Code = defLangArr[0] };
                        def.Save();
                    }

                    Dictionary<string, Lang> langs = new Dictionary<string, Lang>();
                    foreach (XmlNode node in doc.FirstChild.FirstChild.ChildNodes)
                    {
                        string langCode = node.Attributes["name"].Value;
                        string[] langArr = Lang.GetLangFullCodeAndName(langCode);
                        langs[langCode] = Provider.Database.Read<Lang>("Code={0}", langArr[0]);
                        if (langs[langCode] == null)
                        {
                            langs[langCode] = new Lang { Name = langArr[1], Code = langArr[0] };
                            langs[langCode].Save();
                        }
                    }

                    foreach (XmlNode entry in doc.SelectNodes("/localization/entry"))
                    {
                        var sr = new StaticResource { Name = entry.Attributes["phrase"].Value };
                        sr.Save();

                        foreach (XmlNode lang in entry.ChildNodes)
                        {
                            try
                            {
                                var srl = new StaticResourceLang { Translation = lang.InnerText, StaticResourceId = sr.Id, LangId = langs[lang.Attributes["name"].Value].Id };
                                srl.Save();
                            }
                            catch { }
                        }

                    }
                });

            context.Response.Write("OK");
        }