salarcode / AutoResxTranslator

A tool to automatically translate Resx files to any language using Google Translator. No API key is required for the translator. This tool also provides a text translator out of the box.
92 stars 37 forks source link

Tranlate only New added Key (not issue but Couldnt create pull request) #14

Closed webspiderteam closed 2 years ago

webspiderteam commented 2 years ago

Hello. First of all thanks for that beautiful app. Its life saver but I think it need this little improvement. Most programmer adds new features in their app by time and adds new keys to their resource files. But Google Translate is not perfect yet and translations can be wrong. So Programmers Fix their wrong translation by time. but when they add new keys their corrected Translations can gone if they translate them with this app and they have to fix all again one by one. So I made a little change to fix this.

In frmMain

I added a checkbox named "checkBoxTranslateOnlyNew"

image

in frmMain.cs ResxProgressCallback action was

        private delegate void ResxProgressCallback(int max, int pos, string status);

        async void TranslateResxFilesAsync(
            string sourceResx,
            string sourceLng,
            TranslationOptions translationOptions,
            List<string> desLanguages, string destDir,
            ResxProgressCallback progress,
            bool translateFromKey)
        {
            int max = 0;
            int pos = 0;
            int trycount = 0;
            string status = "";
            bool hasErrors = false;

            var sourceResxFilename = ReadLanguageFilename(sourceResx);
            var errorLogFilename = sourceResxFilename + ".errors.log";
            var errorLogFile = Path.Combine(destDir, errorLogFilename);

            foreach (var destLng in desLanguages)
            {
                var destFile = Path.Combine(destDir, sourceResxFilename + "." + destLng + ".resx");
                var doc = new XmlDocument();
                doc.Load(sourceResx);
                var dataList = ResxTranslator.ReadResxData(doc);
                max = dataList.Count;

                pos = 0;
                status = "Translating language: " + destLng;
                progress.BeginInvoke(max, pos, status, null, null);

                try
                {

                    foreach (var node in dataList)
                    {
                        status = "Translating language: " + destLng;
                        pos += 1;
                        progress.BeginInvoke(max, pos, status, null, null);

                        var valueNode = ResxTranslator.GetDataValueNode(node);
                        if (valueNode == null) continue;

                        var orgText = translateFromKey ? ResxTranslator.GetDataKeyName(node) : valueNode.InnerText;

and changed code as

        private delegate void ResxProgressCallback(int max, int pos, string status);

        async void TranslateResxFilesAsync(
            string sourceResx,
            string sourceLng,
            TranslationOptions translationOptions,
            List<string> desLanguages, string destDir,
            ResxProgressCallback progress,
            bool translateFromKey)
        {
            int max = 0;
            int pos = 0;
            int trycount = 0;
            string status = "";
            bool hasErrors = false;

            var sourceResxFilename = ReadLanguageFilename(sourceResx);
            var errorLogFilename = sourceResxFilename + ".errors.log";
            var errorLogFile = Path.Combine(destDir, errorLogFilename);

            foreach (var destLng in desLanguages)
            {
                var destFile = Path.Combine(destDir, sourceResxFilename + "." + destLng + ".resx");
                var doc = new XmlDocument();
                doc.Load(sourceResx);
                var dataList = ResxTranslator.ReadResxData(doc);
                max = dataList.Count;
                                // From Here ------>
                List<XmlNode> targetDataList = new List<XmlNode>();
                bool FileExist= File.Exists(destFile);
                if (checkBoxTranslateOnlyNew.Checked && FileExist)
                {
                    var Targetdoc = new XmlDocument();
                    Targetdoc.Load(destFile);

                    targetDataList = ResxTranslator.ReadResxData(Targetdoc);
                }
                                // <--- to Here
                pos = 0;
                status = "Translating language: " + destLng;
                progress.BeginInvoke(max, pos, status, null, null);

                try
                {
                                         // From here ---->
                    int indexCorrection = 0;
                    foreach (var (node, index) in dataList.Select((n, i) => (n, i)))
                    {
                        status = "Translating language: " + destLng;
                        pos += 1;
                        progress.BeginInvoke(max, pos, status, null, null);

                        var valueNode = ResxTranslator.GetDataValueNode(node);
                        var orgText = translateFromKey ? ResxTranslator.GetDataKeyName(node) : valueNode.InnerText;
                        if (checkBoxTranslateOnlyNew.Checked && FileExist)
                        {
                            int Destindex = index - indexCorrection;
                            if (ResxTranslator.GetDataKeyName(targetDataList.ElementAt(Destindex)) == ResxTranslator.GetDataKeyName(node))
                            {
                                valueNode.InnerText = ResxTranslator.GetDataValueNode(targetDataList.ElementAt(Destindex)).InnerText;
                                indexCorrection++;
                                continue;
                            }

                        }

                        if (valueNode == null) continue;

                        if (string.IsNullOrWhiteSpace(orgText))
                            continue;
                                                /// <------ to Here..

                        if (translationOptions.ServiceType == ServiceTypeEnum.Google)

I hope this would help other people too.

webspiderteam commented 2 years ago

and one more think if target dir has resx files we can check wich languages available before

for this I added

        private void btnSelectResxSource_Click(object sender, EventArgs e)
        {
            var dlg = new OpenFileDialog();
            dlg.Filter = "ResourceX File|*.resx";
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                txtSourceResx.Text = dlg.FileName;
                if (txtOutputDir.Text.Length == 0)
                {
                    txtOutputDir.Text = Path.GetDirectoryName(txtSourceResx.Text);
                }
                                /// From Here ---->
                string[] filePaths = Directory.GetFiles(Path.GetDirectoryName(txtSourceResx.Text), "*.resx");
                foreach (int i in lstResxLanguages.CheckedIndices) // here clears checked items 
                    lstResxLanguages.Items[i].Checked=false;  // this can be disabled some users may not like 
                foreach (var file in filePaths)
                {
                    var haslng = ReadLanguageName(file);
                    if (haslng != "")
                    {
                        var haskey = _languages.FirstOrDefault(x => string.Compare(x.Key, haslng, StringComparison.InvariantCultureIgnoreCase) == 0);
                        lstResxLanguages.Items[lstResxLanguages.Items.IndexOfKey(haskey.Key)].Checked = true;
                    }
                }
                               // <---- to Here
                var lng = ReadLanguageName(txtSourceResx.Text);
                var key = _languages.FirstOrDefault(x => string.Compare(x.Key, lng, StringComparison.InvariantCultureIgnoreCase) == 0);
                if (key.Key != null)
                    cmbSourceResxLng.SelectedItem = key;
                else
salarcode commented 2 years ago

Thank you it is merged now