Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Xml.Linq;
- namespace ConsoleApp1
- {
- class Program
- {
- static void Main(string[] args)
- {
- string path = @"C:\Users\lenovo\Source\Repos\subtitleedit\src\Languages";
- UpdateLanguage(path);
- Console.WriteLine("DONE");
- Console.ReadLine();
- }
- public static void UpdateLanguage(string path)
- {
- string[] files = Directory.GetFiles(path, "*.xml");
- const string oldFormat = "{1}%";
- const string newFormat = "{1:0.00}%";
- foreach (string xmlFile in files)
- {
- XDocument xdoc = null;
- bool changedHappenned = false;
- try
- {
- xdoc = XDocument.Load(xmlFile);
- // element name: XNumberOfDifferenceAndPercentChanged
- // element value: "Number of differences: {0} ({1:0.00}% of words changed)"
- // TODO: change all {1:0} to {1:0.00}
- XElement rootElement = xdoc.Root;
- XElement compareSubtitleElement = rootElement.Element("CompareSubtitles");
- XElement xElement1 = compareSubtitleElement.Element("XNumberOfDifferenceAndPercentChanged");
- XElement xElement2 = compareSubtitleElement.Element("XNumberOfDifferenceAndPercentLettersChanged");
- // note: check for nullability because some language files may not have these elements
- // Console.WriteLine(xElement1?.Value);
- // Console.WriteLine(xElement2?.Value);
- // chane format
- if (xElement1 != null)
- {
- xElement1.Value = xElement1.Value.Replace(oldFormat, newFormat);
- changedHappenned = true;
- }
- if (xElement1 != null)
- {
- xElement2.Value = xElement2.Value.Replace(oldFormat, newFormat);
- changedHappenned = true;
- }
- }
- catch (Exception ex)
- {
- Console.WriteLine(ex.Message);
- }
- finally
- {
- if (changedHappenned)
- xdoc.Save(xmlFile);
- // *dispose xdoc
- xdoc = null;
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement