RegEx to translate RenPy files

Hello,

I'm trying to translate files from a video game created with the Ren'Py Visual Novel Engine (a software game engine with a programming language similar to Python). These files end with a .rpy extension and can be opened in any text editor. I have tried creating a new file type and defining a document structure. It works fine for the RPY files with a simpler structure, but I also have some more complex files and I haven't been able to find RegEx that seem to work. Here is an example:

# game/script/ch1.rpy:34
translate french ch1_308a744b:

# "This is example sentence 1."
"This is example sentence 1."

# game/script/ch1.rpy:38
translate french ch1_baea3297:

# "This is example sentence 2."
"This is example sentence 2."

# game/script/ch1.rpy:160
translate french ch1_7835ba72:

# sato vill cldo opes norm "Some sentences do \"this\" when the author wants to use quotation marks displayed in game."
sato vill cldo opes norm "Some sentences do \"this\" when the author wants to use quotation marks displayed in game."

# game/script/ch1.rpy:161
translate french ch1_fcb8c042:

# taku clot surp weeh "And a last example." with vpunch
taku clot surp weeh "And a last example." with vpunch

Sentences that need to be translated are always between quotation marks. Each sentence is repeated two times, first as a comment (lines starting with #), which must stay in the source language, then as an actual string I need to translate. Sometimes the strings are preceded or followed by a little bit of code (the way the code looks is a bit random).

If I say I only want to translate what's between quotation marks it doesn't work because the sentences in the comments also appears (which means each string appears two times while only one of the two must be translated). It also doesn't work with the \" that are sometimes found within the quotation marks.

I am using Trados Studio 2022 SR1 with the latest update.

emoji
Parents
  •   

    I spent some time on this and I think the filetype may have a bug.  At least only I think this... I need to validate it through support as it won't be the first time they explain to me how to make it work.

    But in the meantime, if it helps, I found a workaround via Excel.

    First of all I convert the RenPy file to Excel with a powershell script like this (or copy paste into Excel... script may be useful if you have multiple files):

    # Import the required module
    # Install it first if not already installed: Install-Module -Name ImportExcel
    Import-Module ImportExcel
    
    # Initialize an empty array to store the lines
    $lines = @()
    
    # Read the content of the text file line by line
    Get-Content -Path 'c:\Users\pfilkin\Documents\SDL\TESTING\Apolline Descy\RenPy.rpy' | ForEach-Object {
        # Add each line to the array
        $lines += $_
    }
    
    # Create an Excel worksheet with the lines
    $lines | Export-Excel -Path 'c:\Users\pfilkin\Documents\SDL\TESTING\Apolline Descy\RenPy.rpy.xlsx' -WorksheetName 'Sheet1' -AutoSize
    
    Write-Host "Excel file has been created."

    Then I can easily do this:

    Screenshot showing the Excel embedded content rules and  successful preview

    Placeholder Rules are:

    ^#.+?$

    ^([^"]*)"

    "[^"]*$

    ^[^"]*$

    Tag Pair is:

    \\" and \\"

    So I can translate the file:

    Screenshot showing the translated text in the editor

    Save the target:

    Screenshot showing the translated file in Excel

    Then either copy this into a text file and rename as an *.rpy file.. or use an Excel macro to do this for me:

    Sub ExportToRenPy()
        Dim ws As Worksheet
        Dim LastRow As Long
        Dim i As Long
        Dim RenPyContent As String
        Dim FilePath As String
        
        ' Set the worksheet and find the last row with content in column A
        Set ws = ThisWorkbook.Sheets(1)
        LastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
        
        ' Loop through each cell in column A to concatenate into a single string
        For i = 1 To LastRow
            RenPyContent = RenPyContent & ws.Cells(i, 1).Value & vbCrLf
        Next i
        
        ' Set the file path for saving
        FilePath = "c:\Users\pfilkin\Documents\SDL\TESTING\Apolline Descy\t_RenPy.rpy"
        
        ' Create a text file and write the content
        Open FilePath For Output As #1
        Print #1, RenPyContent
        Close #1
        
        MsgBox "RenPy file has been created."
    End Sub

    Final file looking like this:

    # game/script/ch1.rpy:34
    translate french ch1_308a744b:
    
    # "This is example sentence 1."
    "Questa è la frase di esempio 1."
    
    # game/script/ch1.rpy:38
    translate french ch1_baea3297:
    
    # "This is example sentence 2."
    "Questa è la frase di esempio 2."
    
    # game/script/ch1.rpy:160
    translate french ch1_7835ba72:
    
    # sato vill cldo opes norm "Alcune frasi \"lo\" fanno quando l'autore vuole usare le virgolette visualizzate in gioco."
    sato vill cldo opes norm "Alcune frasi \"lo\" fanno quando l'autore vuole usare le virgolette visualizzate in gioco."
    
    # game/script/ch1.rpy:161
    translate french ch1_fcb8c042:
    
    # taku clot surp weeh "And a last example." with vpunch
    taku clot surp weeh "E un ultimo esempio." with vpunch

    Seems like a long workaround, but it's actually pretty quick and I put the code here so you could adapt if you have multiple files to deal with.

    In the meantime I will log the problem with the regex delimited filetype so the support team can take a look and let you now if it was a bug, or if I also need a lesson in how to make it work properly!

    Paul Filkin | RWS Group

    ________________________
    Design your own training!

    You've done the courses and still need to go a little further, or still not clear? 
    Tell us what you need in our Community Solutions Hub

    emoji
Reply
  •   

    I spent some time on this and I think the filetype may have a bug.  At least only I think this... I need to validate it through support as it won't be the first time they explain to me how to make it work.

    But in the meantime, if it helps, I found a workaround via Excel.

    First of all I convert the RenPy file to Excel with a powershell script like this (or copy paste into Excel... script may be useful if you have multiple files):

    # Import the required module
    # Install it first if not already installed: Install-Module -Name ImportExcel
    Import-Module ImportExcel
    
    # Initialize an empty array to store the lines
    $lines = @()
    
    # Read the content of the text file line by line
    Get-Content -Path 'c:\Users\pfilkin\Documents\SDL\TESTING\Apolline Descy\RenPy.rpy' | ForEach-Object {
        # Add each line to the array
        $lines += $_
    }
    
    # Create an Excel worksheet with the lines
    $lines | Export-Excel -Path 'c:\Users\pfilkin\Documents\SDL\TESTING\Apolline Descy\RenPy.rpy.xlsx' -WorksheetName 'Sheet1' -AutoSize
    
    Write-Host "Excel file has been created."

    Then I can easily do this:

    Screenshot showing the Excel embedded content rules and  successful preview

    Placeholder Rules are:

    ^#.+?$

    ^([^"]*)"

    "[^"]*$

    ^[^"]*$

    Tag Pair is:

    \\" and \\"

    So I can translate the file:

    Screenshot showing the translated text in the editor

    Save the target:

    Screenshot showing the translated file in Excel

    Then either copy this into a text file and rename as an *.rpy file.. or use an Excel macro to do this for me:

    Sub ExportToRenPy()
        Dim ws As Worksheet
        Dim LastRow As Long
        Dim i As Long
        Dim RenPyContent As String
        Dim FilePath As String
        
        ' Set the worksheet and find the last row with content in column A
        Set ws = ThisWorkbook.Sheets(1)
        LastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
        
        ' Loop through each cell in column A to concatenate into a single string
        For i = 1 To LastRow
            RenPyContent = RenPyContent & ws.Cells(i, 1).Value & vbCrLf
        Next i
        
        ' Set the file path for saving
        FilePath = "c:\Users\pfilkin\Documents\SDL\TESTING\Apolline Descy\t_RenPy.rpy"
        
        ' Create a text file and write the content
        Open FilePath For Output As #1
        Print #1, RenPyContent
        Close #1
        
        MsgBox "RenPy file has been created."
    End Sub

    Final file looking like this:

    # game/script/ch1.rpy:34
    translate french ch1_308a744b:
    
    # "This is example sentence 1."
    "Questa è la frase di esempio 1."
    
    # game/script/ch1.rpy:38
    translate french ch1_baea3297:
    
    # "This is example sentence 2."
    "Questa è la frase di esempio 2."
    
    # game/script/ch1.rpy:160
    translate french ch1_7835ba72:
    
    # sato vill cldo opes norm "Alcune frasi \"lo\" fanno quando l'autore vuole usare le virgolette visualizzate in gioco."
    sato vill cldo opes norm "Alcune frasi \"lo\" fanno quando l'autore vuole usare le virgolette visualizzate in gioco."
    
    # game/script/ch1.rpy:161
    translate french ch1_fcb8c042:
    
    # taku clot surp weeh "And a last example." with vpunch
    taku clot surp weeh "E un ultimo esempio." with vpunch

    Seems like a long workaround, but it's actually pretty quick and I put the code here so you could adapt if you have multiple files to deal with.

    In the meantime I will log the problem with the regex delimited filetype so the support team can take a look and let you now if it was a bug, or if I also need a lesson in how to make it work properly!

    Paul Filkin | RWS Group

    ________________________
    Design your own training!

    You've done the courses and still need to go a little further, or still not clear? 
    Tell us what you need in our Community Solutions Hub

    emoji
Children
No Data