Guest User

translate.py

a guest
Jul 8th, 2018
381
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 11.94 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. import io
  4. import re
  5. import os
  6.  
  7.  
  8. pathOrg = "./TEXTDUMP/EU.nds/" #You might need to update the ROM folder name
  9. pathTrans = "./Localize/de/msg/" #You might need to update the locale name
  10.  
  11. filesBIG = ["battle", "bgm", "cmes0", "cmes1", "cmes2", "cmes3", "cmes4", "cmes5", "comu0", "ev_title",
  12.     "ex_ending", "ex_illust", "ex_item", "ex_itemget", "ex_mon", "ex_monspot", "ex_montec",
  13.     "exms0", "exms1", "exms2", "exms3", "extra", "item", "item_mes", "item_mes2", "item_sub", "kmes0",
  14.     "kmes1", "kmes2", "map", "mesi0", "mesk0", "mesk1", "mesk2", "mesk3", "mesk4", "mess0",
  15.     "mest1", "mest2", "mest3", "mest4", "mest5", "mon_tec", "monster", "msg01", "msg02", "msg03", "msg04",
  16.     "player", "ques0", "tec_mes", "tech", "w_map", "zukan" ]
  17. filesSMALL = ["sfc_btl", "sfc_item", "sfc_mon", "sfc_tec", "small"]
  18.  
  19. if not os.path.exists(pathOrg + "/translated/BIG/"):
  20.     os.makedirs(pathOrg + "/translated/BIG/")
  21. if not os.path.exists(pathOrg + "/translated/SMALL/"):
  22.     os.makedirs(pathOrg + "/translated/SMALL/")
  23.  
  24. msgNameRE = re.compile('((MSG)|(FLD))([^,])*(,)')
  25. starRE = re.compile('([★])')
  26. newlineRE = re.compile('(\\\\)')
  27. tabRE = re.compile('(<S10>)')
  28. quotesRE = re.compile('([„“])')
  29. squotesRE = re.compile('(‚)(.)*(‛)')
  30. quoteRE = re.compile('(’)')
  31. dashRE = re.compile('(–)')
  32. divRE = re.compile('(<S1>)')
  33. heartRE = re.compile('(♡)')
  34.  
  35. msgwndRE = re.compile('(<CT>)')
  36. waitRE = re.compile('(<WAIT>)')
  37. endWaitRE = re.compile('(</WAIT>)')
  38. aclearRE = re.compile('(<AUTO_PAGE>)')
  39. acloseRE = re.compile('(<AUTO_END>)')
  40. clearRE = re.compile('(<PAGE>)')
  41.  
  42. sel1RE = re.compile('(<C1>)')
  43. endSel1RE = re.compile('(</C1>)')
  44. sel2RE = re.compile('(<C2>)')
  45. endSel2RE = re.compile('(</C2>)')
  46. sel3RE = re.compile('(<C3>)')
  47. endSel3RE = re.compile('(</C3>)')
  48. sel4RE = re.compile('(<C4>)')
  49. endSel4RE = re.compile('(</C4>)')
  50. countRE = re.compile('(<NUMBER>)')
  51.  
  52. cronoRE = re.compile('(<NAME_CRO>)')
  53. luccaRE = re.compile('(<NAME_LUC>)')
  54. marleRE = re.compile('(<NAME_MAR>)')
  55. frogRE = re.compile('(<NAME_FRO>)')
  56. roboRE = re.compile('(<NAME_ROB>)')
  57. aylaRE = re.compile('(<NAME_AYL>)')
  58. magusRE = re.compile('(<NAME_MAG>)')
  59. hero1RE = re.compile('(<NAME_PT1>)')
  60. hero2RE = re.compile('(<NAME_PT2>)')
  61. hero3RE = re.compile('(<NAME_PT3>)')
  62. nameRE = re.compile('(<NAME_CNO>)')
  63. itemRE = re.compile('(<NAME_ITM>)')
  64. magicRE = re.compile('(<NAME_TEC>)')
  65. enemyRE = re.compile('(<NAME_MON>)')
  66. epochRE = re.compile('(<NAME_SIL>)')
  67.  
  68. fireRE = re.compile('(<ICON_FIRE>)')
  69. waterRE = re.compile('(<ICON_Aqua>)|(<ICON_WATER>)')
  70. lightRE = re.compile('(<ICON_LIGHT>)')
  71. shadowRE = re.compile('(<ICON_SHADOW>)')
  72.  
  73.  
  74. messageStartRE = re.compile('(\[@ITEM )[\d]+( \(EN\) \]\n)')
  75. messageEndRE = re.compile('(\\\\0\n\n\[@ITEM )[\d]+( \(L2\) \]\n)')
  76.  
  77.  
  78. def processFile(filename, folder):
  79.     fileOrg = io.open(pathOrg + folder + filename + ".msg.txt", mode="r", encoding="utf8")
  80.     content = fileOrg.read()
  81.     fileTrans = io.open(pathTrans + filename + ".txt", mode="r", encoding="utf-8")
  82.     trans = fileTrans.readlines()
  83.  
  84.     for x in range(0, len(trans)):
  85.         trans[x] = trans[x].strip()
  86.         m = msgNameRE.search(trans[x])
  87.         while m is not None:
  88.             trans[x] = trans[x][m.end():]
  89.             m = msgNameRE.search(trans[x])
  90.        
  91.         m = starRE.search(trans[x])
  92.         while m is not None:
  93.             tmp = trans[x][:m.start()]
  94.             tmp += "{STAR}"
  95.             tmp += trans[x][m.end():]
  96.             trans[x] = tmp
  97.             m = starRE.search(trans[x])
  98.  
  99.         m = quotesRE.search(trans[x])
  100.         while m is not None:
  101.             tmp = trans[x][:m.start()]
  102.             tmp += trans[x][m.end():]
  103.             trans[x] = tmp
  104.             m = quotesRE.search(trans[x])
  105.         m = squotesRE.search(trans[x])
  106.         while m is not None:
  107.             tmp = trans[x][:m.start()]
  108.             tmp += trans[x][m.start() + 1:m.end() -1]
  109.             tmp += trans[x][m.end():]
  110.             trans[x] = tmp
  111.             m = squotesRE.search(trans[x])
  112.         m = quoteRE.search(trans[x])
  113.         while m is not None:
  114.             tmp = trans[x][:m.start()]
  115.             tmp += "\'"
  116.             tmp += trans[x][m.end():]
  117.             trans[x] = tmp
  118.             m = quoteRE.search(trans[x])
  119.         m = dashRE.search(trans[x])
  120.         while m is not None:
  121.             tmp = trans[x][:m.start()]
  122.             tmp += "-"
  123.             tmp += trans[x][m.end():]
  124.             trans[x] = tmp
  125.             m = dashRE.search(trans[x])
  126.         m = divRE.search(trans[x])
  127.         while m is not None:
  128.             tmp = trans[x][:m.start()]
  129.             tmp += trans[x][m.end():]
  130.             trans[x] = tmp
  131.             m = divRE.search(trans[x])
  132.         m = heartRE.search(trans[x])
  133.         while m is not None:
  134.             tmp = trans[x][:m.start()]
  135.             tmp += "♥"
  136.             tmp += trans[x][m.end():]
  137.             trans[x] = tmp
  138.             m = heartRE.search(trans[x])
  139.  
  140.         m = msgwndRE.search(trans[x])
  141.         while m is not None:
  142.             tmp = trans[x][:m.start()]
  143.             tmp += "{MSGWND}"
  144.             tmp += trans[x][m.end():]
  145.             trans[x] = tmp
  146.             m = msgwndRE.search(trans[x])
  147.  
  148.         m = waitRE.search(trans[x])
  149.         while m is not None:
  150.             tmp = trans[x][:m.start()]
  151.             tmp += "{WAIT}"
  152.             tmp += trans[x][m.end():]
  153.             trans[x] = tmp
  154.             m = waitRE.search(trans[x])
  155.  
  156.         m = endWaitRE.search(trans[x])
  157.         while m is not None:
  158.             tmp = trans[x][:m.start()]
  159.             tmp += "{/WAIT}"
  160.             tmp += trans[x][m.end():]
  161.             trans[x] = tmp
  162.             m = endWaitRE.search(trans[x])
  163.  
  164.         m = aclearRE.search(trans[x])
  165.         while m is not None:
  166.             tmp = trans[x][:m.start()]
  167.             tmp += "{CLEAR}"
  168.             tmp += trans[x][m.end():]
  169.             trans[x] = tmp
  170.             m = aclearRE.search(trans[x])
  171.  
  172.         m = acloseRE.search(trans[x])
  173.         while m is not None:
  174.             tmp = trans[x][:m.start()]
  175.             tmp += "{CLOSE}"
  176.             tmp += trans[x][m.end():]
  177.             trans[x] = tmp
  178.             m = acloseRE.search(trans[x])
  179.  
  180.         m = clearRE.search(trans[x])
  181.         while m is not None:
  182.             tmp = trans[x][:m.start()]
  183.             tmp += "{PRESS_CLEAR}"
  184.             tmp += trans[x][m.end():]
  185.             trans[x] = tmp
  186.             m = clearRE.search(trans[x])
  187.  
  188.         m = tabRE.search(trans[x])
  189.         while m is not None:
  190.             tmp = trans[x][:m.start()]
  191.             tmp += "    "
  192.             tmp += trans[x][m.end():]
  193.             trans[x] = tmp
  194.             m = tabRE.search(trans[x])
  195.  
  196.         m = sel1RE.search(trans[x])
  197.         while m is not None:
  198.             tmp = trans[x][:m.start()]
  199.             tmp += "{SEL1}"
  200.             tmp += trans[x][m.end():]
  201.             trans[x] = tmp
  202.             m = sel1RE.search(trans[x])
  203.         m = sel2RE.search(trans[x])
  204.         while m is not None:
  205.             tmp = trans[x][:m.start()]
  206.             tmp += "{SEL2}"
  207.             tmp += trans[x][m.end():]
  208.             trans[x] = tmp
  209.             m = sel2RE.search(trans[x])
  210.         m = sel3RE.search(trans[x])
  211.         while m is not None:
  212.             tmp = trans[x][:m.start()]
  213.             tmp += "{SEL3}"
  214.             tmp += trans[x][m.end():]
  215.             trans[x] = tmp
  216.             m = sel3RE.search(trans[x])
  217.         m = sel4RE.search(trans[x])
  218.         while m is not None:
  219.             tmp = trans[x][:m.start()]
  220.             tmp += "{SEL4}"
  221.             tmp += trans[x][m.end():]
  222.             trans[x] = tmp
  223.             m = sel4RE.search(trans[x])
  224.         m = endSel1RE.search(trans[x])
  225.         while m is not None:
  226.             tmp = trans[x][:m.start()]
  227.             tmp += "{/SEL1}"
  228.             tmp += trans[x][m.end():]
  229.             trans[x] = tmp
  230.             m = endSel1RE.search(trans[x])
  231.         m = endSel2RE.search(trans[x])
  232.         while m is not None:
  233.             tmp = trans[x][:m.start()]
  234.             tmp += "{/SEL2}"
  235.             tmp += trans[x][m.end():]
  236.             trans[x] = tmp
  237.             m = endSel2RE.search(trans[x])
  238.         m = endSel3RE.search(trans[x])
  239.         while m is not None:
  240.             tmp = trans[x][:m.start()]
  241.             tmp += "{/SEL3}"
  242.             tmp += trans[x][m.end():]
  243.             trans[x] = tmp
  244.             m = endSel3RE.search(trans[x])
  245.         m = endSel4RE.search(trans[x])
  246.         while m is not None:
  247.             tmp = trans[x][:m.start()]
  248.             tmp += "{/SEL4}"
  249.             tmp += trans[x][m.end():]
  250.             trans[x] = tmp
  251.             m = endSel4RE.search(trans[x])
  252.  
  253.         m = countRE.search(trans[x])
  254.         while m is not None:
  255.             tmp = trans[x][:m.start()]
  256.             tmp += "{COUNT}"
  257.             tmp += trans[x][m.end():]
  258.             trans[x] = tmp
  259.             m = countRE.search(trans[x])
  260.  
  261.  
  262.         m = cronoRE.search(trans[x])
  263.         while m is not None:
  264.             tmp = trans[x][:m.start()]
  265.             tmp += "{Crono}"
  266.             tmp += trans[x][m.end():]
  267.             trans[x] = tmp
  268.             m = cronoRE.search(trans[x])
  269.         m = luccaRE.search(trans[x])
  270.         while m is not None:
  271.             tmp = trans[x][:m.start()]
  272.             tmp += "{Lucca}"
  273.             tmp += trans[x][m.end():]
  274.             trans[x] = tmp
  275.             m = luccaRE.search(trans[x])
  276.         m = marleRE.search(trans[x])
  277.         while m is not None:
  278.             tmp = trans[x][:m.start()]
  279.             tmp += "{Marle}"
  280.             tmp += trans[x][m.end():]
  281.             trans[x] = tmp
  282.             m = marleRE.search(trans[x])
  283.         m = frogRE.search(trans[x])
  284.         while m is not None:
  285.             tmp = trans[x][:m.start()]
  286.             tmp += "{Frog}"
  287.             tmp += trans[x][m.end():]
  288.             trans[x] = tmp
  289.             m = frogRE.search(trans[x])
  290.         m = roboRE.search(trans[x])
  291.         while m is not None:
  292.             tmp = trans[x][:m.start()]
  293.             tmp += "{Robo}"
  294.             tmp += trans[x][m.end():]
  295.             trans[x] = tmp
  296.             m = roboRE.search(trans[x])
  297.         m = aylaRE.search(trans[x])
  298.         while m is not None:
  299.             tmp = trans[x][:m.start()]
  300.             tmp += "{Ayla}"
  301.             tmp += trans[x][m.end():]
  302.             trans[x] = tmp
  303.             m = aylaRE.search(trans[x])
  304.         m = magusRE.search(trans[x])
  305.         while m is not None:
  306.             tmp = trans[x][:m.start()]
  307.             tmp += "{Magus}"
  308.             tmp += trans[x][m.end():]
  309.             trans[x] = tmp
  310.             m = magusRE.search(trans[x])
  311.         m = epochRE.search(trans[x])
  312.         while m is not None:
  313.             tmp = trans[x][:m.start()]
  314.             tmp += "{Epoch}"
  315.             tmp += trans[x][m.end():]
  316.             trans[x] = tmp
  317.             m = epochRE.search(trans[x])
  318.         m = hero1RE.search(trans[x])
  319.         while m is not None:
  320.             tmp = trans[x][:m.start()]
  321.             tmp += "{HERO1}"
  322.             tmp += trans[x][m.end():]
  323.             trans[x] = tmp
  324.             m = hero1RE.search(trans[x])
  325.         m = hero2RE.search(trans[x])
  326.         while m is not None:
  327.             tmp = trans[x][:m.start()]
  328.             tmp += "{HERO2}"
  329.             tmp += trans[x][m.end():]
  330.             trans[x] = tmp
  331.             m = hero2RE.search(trans[x])
  332.         m = hero3RE.search(trans[x])
  333.         while m is not None:
  334.             tmp = trans[x][:m.start()]
  335.             tmp += "{HERO3}"
  336.             tmp += trans[x][m.end():]
  337.             trans[x] = tmp
  338.             m = hero3RE.search(trans[x])
  339.         m = nameRE.search(trans[x])
  340.         while m is not None:
  341.             tmp = trans[x][:m.start()]
  342.             tmp += "{NAME}"
  343.             tmp += trans[x][m.end():]
  344.             trans[x] = tmp
  345.             m = nameRE.search(trans[x])
  346.         m = itemRE.search(trans[x])
  347.         while m is not None:
  348.             tmp = trans[x][:m.start()]
  349.             tmp += "{ITEM}"
  350.             tmp += trans[x][m.end():]
  351.             trans[x] = tmp
  352.             m = itemRE.search(trans[x])
  353.         m = magicRE.search(trans[x])
  354.         while m is not None:
  355.             tmp = trans[x][:m.start()]
  356.             tmp += "{MAGIC}"
  357.             tmp += trans[x][m.end():]
  358.             trans[x] = tmp
  359.             m = magicRE.search(trans[x])
  360.         m = enemyRE.search(trans[x])
  361.         while m is not None:
  362.             tmp = trans[x][:m.start()]
  363.             tmp += "{ENEMY}"
  364.             tmp += trans[x][m.end():]
  365.             trans[x] = tmp
  366.             m = enemyRE.search(trans[x])
  367.  
  368.  
  369.         m = fireRE.search(trans[x])
  370.         while m is not None:
  371.             tmp = trans[x][:m.start()]
  372.             tmp += "{FIRE}"
  373.             tmp += trans[x][m.end():]
  374.             trans[x] = tmp
  375.             m = fireRE.search(trans[x])
  376.         m = waterRE.search(trans[x])
  377.         while m is not None:
  378.             tmp = trans[x][:m.start()]
  379.             tmp += "{WATER}"
  380.             tmp += trans[x][m.end():]
  381.             trans[x] = tmp
  382.             m = waterRE.search(trans[x])
  383.         m = lightRE.search(trans[x])
  384.         while m is not None:
  385.             tmp = trans[x][:m.start()]
  386.             tmp += "{LIGHT}"
  387.             tmp += trans[x][m.end():]
  388.             trans[x] = tmp
  389.             m = lightRE.search(trans[x])
  390.         m = shadowRE.search(trans[x])
  391.         while m is not None:
  392.             tmp = trans[x][:m.start()]
  393.             tmp += "{BOMB/SHADOW}"
  394.             tmp += trans[x][m.end():]
  395.             trans[x] = tmp
  396.             m = shadowRE.search(trans[x])
  397.  
  398.  
  399.  
  400.         m = newlineRE.search(trans[x])
  401.         while m is not None:
  402.             tmp = trans[x][:m.start()]
  403.             tmp += "\n"
  404.             tmp += trans[x][m.end():]
  405.             trans[x] = tmp
  406.             m = newlineRE.search(trans[x])
  407.  
  408.  
  409.     contentNew = ""
  410.     i = 0
  411.     first = True
  412.     endMatch = None
  413.     for match in messageStartRE.finditer(content):
  414.         if first:
  415.             contentNew += content[:match.end()]
  416.             first = False
  417.         else:
  418.             contentNew += content[oldMatch.end() + endMatch.start():match.end()]
  419.         oldMatch = match
  420.        
  421.         contentNew += trans[i]
  422.         i = i + 1
  423.         lastEnd = endMatch
  424.         endMatch = messageEndRE.search(content[match.end():])
  425.     contentNew += content[lastEnd.start():]
  426.  
  427.  
  428.     fileNew = io.open(pathOrg + "/translated/" + folder + filename + ".msg.txt", mode="w", encoding="utf8")
  429.     fileNew.write(contentNew)
  430.     return
  431.  
  432.  
  433.  
  434.  
  435. for file in filesBIG:
  436.     print(file)
  437.     processFile(  file, "/BIG/")
  438. for file in filesSMALL:
  439.     processFile(  file, "/SMALL/")
Advertisement
Add Comment
Please, Sign In to add comment