Beispiel #1
0
    protected void Save_Click(object sender, System.EventArgs e)
    {
        if (!User.Identity.IsAuthenticated)
        {
            showError("You are not logged in. Please login!", "/Account/Login", "/translate");
        }
        else
        {
            ProjectHelper.ProjectInfo Project = (ProjectHelper.ProjectInfo)Session["CurrentlyChosenProject"];
            string Language         = User.Identity.getUserLanguage(Session);
            string ProjectDirectory = ConfigurationManager.AppSettings["ProjectDirectory"].ToString() + Project.Folder + "\\";
            string SourceLang       = User.Identity.GetSourceLanguage();

            if (Project == null || Language == null)
            {
                showError("Session expired. Could not read Project or Language. Please login!", "/Account/Login", "/translate");
                // TODO: Save Values here for relog (like HttpCookie myCookie = new HttpCookie("savedValues");)
            }

            int Updates = 0;

            XmlDocument SourceFile;

            if (Directory.Exists(Path.Combine(ProjectDirectory, SourceLang)))
            {
                SourceFile = XMLFile.GetXMLDocument(Path.Combine(ProjectDirectory, SourceLang, Convert.ToString(Session["SelectedFilename"]) + "." + SourceLang + ".resx"));
            }
            else
            {
                SourceFile = XMLFile.GetXMLDocument(ProjectDirectory + Convert.ToString(Session["SelectedFilename"]) + ".resx");
            }

            string TargetFilename       = ProjectDirectory + Language + "\\" + Convert.ToString(Session["SelectedFilename"]) + "." + Language + ".resx";
            string TargetFileNameForGen = ProjectDirectory + Language + "\\Download" + "\\" + Convert.ToString(Session["SelectedFilename"]) + "." + Language + ".resx";

            // if download directory does not exist, create it
            if (!Directory.Exists(ProjectDirectory + Language + "\\Download"))
            {
                Directory.CreateDirectory(ProjectDirectory + Language + "\\Download");
            }

            XmlDocument TranslatedFile = XMLFile.GetXMLDocument(TargetFilename);
            string      Filename       = Convert.ToString(Session["SelectedFilename"]);

            foreach (RepeaterItem Item in TextElements.Items)
            {
                Label   LB          = Item.FindControl("Element") as Label;
                TextBox TB          = Item.FindControl("TranslatedText") as TextBox;
                string  TComment    = (Item.FindControl("TranslateComment") as TextBox).Text;
                string  ElementName = Utils.HTMLDecode(LB.Text);

                XmlNode Node = TranslatedFile.SelectSingleNode("/root/data[@name=\"" + ElementName + "\"]/value");

                // Node does not exist in translation file, so add it
                if (Node == null)
                {
                    Node = SourceFile.SelectSingleNode("/root/data[@name=\"" + ElementName + "\"]");
                    XmlNode rootnode   = TranslatedFile.SelectSingleNode("/root");
                    XmlNode CopiedNode = TranslatedFile.ImportNode(Node, true);
                    CopiedNode.SelectSingleNode("value").InnerText = TB.Text;

                    rootnode.AppendChild(CopiedNode);
                    Updates += 1;
                }
                else
                {
                    string CurrentValue = Node.InnerText;
                    string NewValue     = TB.Text;

                    // if value changed..
                    if (CurrentValue != NewValue)
                    {
                        Node.InnerText = NewValue;
                        Updates       += 1;
                    }
                }


                XmlNode CommentNode = TranslatedFile.SelectSingleNode("/root/data[@name=\"" + ElementName + "\"]/comment");
                // Node does not exist in translation file, so add it
                if (CommentNode == null)
                {
                    CommentNode = TranslatedFile.SelectSingleNode("/root/data[@name=\"" + ElementName + "\"]");
                    XmlNode rootnode = TranslatedFile.SelectSingleNode("/root");

                    //Create a new comment node.
                    XmlElement elem = TranslatedFile.CreateElement("comment");
                    elem.InnerText = TComment;
                    CommentNode.AppendChild(elem);

                    rootnode.AppendChild(CommentNode);
                    Updates += 1;
                }
                else
                {
                    string CurrentValue = CommentNode.InnerText;

                    // if value changed..
                    if (CurrentValue != TComment)
                    {
                        Updates += 1;
                        CommentNode.InnerText = TComment;
                    }
                }
            }

            int alreadyTranslated = Convert.ToInt32(User.Identity.GetTranslatedStrings());
            int updateCount       = Updates;

            User.Identity.SetTranslatedStrings(alreadyTranslated + updateCount);

            // No updates made. No need to save file, but recalculate the percentage in case the file changed externally
            if (Updates == 0)
            {
                XMLFile.ComputePercentage(Project, Language, Convert.ToString(Session["SelectedFilename"]), SourceLang);
            }
            else
            {
                TranslatedFile.Save(TargetFilename);

                Utils.CreateBackup(TranslatedFile, TargetFilename);

                XMLFile.ComputePercentage(Project, Language, Convert.ToString(Session["SelectedFilename"]), SourceLang);

                //Session["GlobalMessage"] = "File '" + TargetFilename.Split("\\".ToCharArray())[TargetFilename.Split("\\".ToCharArray()).GetUpperBound(0)] + "' saved sucessfully!";

                TranslatedFile.Save(TargetFileNameForGen);

                if (ProjectHelper.FTPUploadEnabled(Project))
                {
                    try
                    {
                        ProjectHelper.FTPUpload(Project, TargetFilename, Language);
                    }
                    catch (Exception ex)
                    {
                        showError("Error: " + ex.Message);
                    }
                }

                Response.Redirect("Translate.aspx");
                Session.Remove("SelectedFilename");
            }
        }
    }