Advertisement
Eselter

CloseCaption to xliff

Sep 20th, 2020
1,516
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.22 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text.RegularExpressions;
  5. using System.Xml.Linq;
  6.  
  7. namespace CloseCaption_to_xliff
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             string pattern = @"\s*""([A-Za-z0-9\.\/' _-]+)""\s+""(.*?)""\s*";
  14.  
  15.             string[] input = System.IO.File.ReadAllLines(@"J:\Inny\Desktop\Black Mesa PL WIP\PL latest\bms_spolszczenie\resource\bmsmp_english.txt");
  16.  
  17.             Dictionary<string, string> polishDict = new Dictionary<string, string>();
  18.  
  19.             foreach (string line in input)
  20.             {
  21.                 Match match = Regex.Match(line, pattern);
  22.                 if (match.Success)
  23.                 {
  24.                     try
  25.                     {
  26.                         polishDict.Add(match.Groups[1].Value, match.Groups[2].Value);
  27.                     }
  28.                     catch (Exception)
  29.                     {
  30.                         Console.WriteLine(match.Groups[1].Value);
  31.                         throw;
  32.                     }
  33.                 }
  34.             }
  35.  
  36.  
  37.             XDocument doc = XDocument.Load("C:\\Users\\Eselter\\Downloads\\Black Mesa.xliff");
  38.             XNamespace df = doc.Root.Name.Namespace;
  39.             int count = 0;
  40.             foreach (XElement transUnitNode in doc.Descendants(df + "trans-unit"))
  41.             {
  42.                 try
  43.                 {
  44.                     XElement sourceNode = transUnitNode.Element(df + "note");
  45.                     if (polishDict.ContainsKey(sourceNode.Value.Split('\n').Last().Trim()))
  46.                     {
  47.                         count++;
  48.                         transUnitNode.Element(df + "target").Value = polishDict[sourceNode.Value.Split('\n').Last().Trim()];
  49.                         transUnitNode.Element(df + "target").Attribute("state").Value = "translated";
  50.                     }
  51.                 }
  52.                 catch (Exception)
  53.                 {
  54.                     Console.WriteLine(transUnitNode.Attribute("id").Value);
  55.                 }
  56.             }
  57.  
  58.             doc.Save("C:\\Users\\Eselter\\Downloads\\Black Mesa.xliff");
  59.  
  60.             Console.WriteLine(count);
  61.             Console.ReadKey();
  62.         }
  63.     }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement