RegexTranslation.cs 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. using System;
  2. using System.Text.RegularExpressions;
  3. namespace XUnity.AutoTranslator.Plugin.Core
  4. {
  5. class RegexTranslation
  6. {
  7. public RegexTranslation( string key, string value )
  8. {
  9. // remove r:
  10. if( key.StartsWith( "r:" ) )
  11. {
  12. key = key.Substring( 2, key.Length - 2 );
  13. }
  14. var startIdx = key.IndexOf( '"' ) + 1;
  15. if( startIdx == -1 )
  16. {
  17. // take entire string
  18. }
  19. else
  20. {
  21. var endIdx = key.LastIndexOf( '"', key.Length - 1 );
  22. if( endIdx != startIdx )
  23. {
  24. key = key.Substring( startIdx, endIdx - startIdx );
  25. }
  26. }
  27. // remove r:
  28. if( value.StartsWith( "r:" ) )
  29. {
  30. value = value.Substring( 2, value.Length - 2 );
  31. }
  32. startIdx = value.IndexOf( '"' ) + 1;
  33. if( startIdx == -1 )
  34. {
  35. // take entire string
  36. }
  37. else
  38. {
  39. var endIdx = value.LastIndexOf( '"', value.Length - 1 );
  40. if( endIdx != startIdx )
  41. {
  42. value = value.Substring( startIdx, endIdx - startIdx );
  43. }
  44. }
  45. CompiledRegex = new Regex( key );
  46. Original = key;
  47. Translation = value;
  48. }
  49. public Regex CompiledRegex { get; set; }
  50. public string Original { get; set; }
  51. public string Translation { get; set; }
  52. }
  53. }