ChatGPT + Powershell = Localization Superpowers!

DALL·E - Digital art: AI generating Powershell scriptsLocalization engineers are the miracle workers behind the scenes of localization workflows, and without them many of the projects we see couldn’t happen.  The skillsets they possess go far beyond the sort of things that most translators know how to do, and often require the ability to code.  I’ve already written a little about these sorts of things in the last three or four articles I published this month, mainly because the use of AI (tools like ChatGPT for example) is opening up the possibility for the rest of us mere mortals to benefit from the sort of things they do.  Today I’m extending on another such skill that I have introduced only once before back in 2013, a decade ago!  It is a very technical, and yet powerful thing to be able to tap into, so now with the help of ChatGPT I’m going to do it again!

Filetype support…

The filetype support in Trados Studio is pretty extensive.  But sometimes what you actually need to improve the overall experience based on information in a file just isn’t there.  So what do you do… if you’re lucky you’ll have a localization engineer to help, and if not you’ll ask in the forums… probably.  But this sort of technical skillset is probably nothing you’ll get a lot of help with because it’s normally quite bespoke and expensive to provide.  So what sort of thing am I talking about?  Let’s take a simple sort of example to get the idea.

Let’s say you received an IOS .strings file like this for translation:

The first thing you’d do is try and open it in Trados Studio, or whatever CAT tool you own (some CAT tools may support this file already… keep in mind I’m just using this as an example for a skillset you’re gonna like!).  If your tool doesn’t support it you may see something like this:

But… unperturbed you know there’s another way to handle this using regular expressions.  So you create your filetype, make sure it can be used for a .strings file:

And now you see this when you open the file:

All simple stuff and then you realise you can’t see the comments, and you might need these to give yourself some context while translating.  Now with Trados Studio you are lucky as the preview will show the contents of the file:

But if you didn’t have this, or if the file was actually more complex so the preview was less helpful due to the amount of unnecessary clutter in the file, then you really want something better.  For example, if the file was in Excel then you could use the html preview or the DSI Viewer to show you exactly what you needed by handling the file with the Bilingual Excel filetype.

If you’re lucky your localization engineer will have done this already for you, but if not you now have a little help from our AI friend, ChatGPT.  First of all I’m not going to say this was straightforward, because it wasn’t.  I have mentioned before that getting the prompt correct is the most important part. So I started with this, just to give you some idea:

Please create an Excel file using a Powershell script containing a sheet with three columns: “Comment”, “Key” and “Value”. The file should contain a list of localization strings in the format of:

/* Comment */
“Key” = “Value”;
A blank row follows each key/value pair.

The localization strings come from a .strings resource file that you prompt the user to provide.

The Excel sheet should contain one row per localization string, with the Comment column containing the comment, with the “Key” column containing the string key (without quotes), and the “Value” column containing the localized value (also without quotes). The Excel sheet should have no blank rows.

I had to make corrections five or six times (not straight forward but not exactly difficult!) and ChatGPT told me later I should have asked this:

Write a PowerShell script that can read a .strings resource file and create an Excel spreadsheet with the data from the file. The script should:

  1. Prompt the user to enter the path to the .strings resource file.
  2. Import the content of the .strings resource file.
  3. Create a new Excel spreadsheet with three columns: “Comment”, “Key”, and “Value”.
  4. Add the content of the .strings resource file to the spreadsheet, with comments in the “Comment” column and key-value pairs in the “Key” and “Value” columns.
  5. Save the spreadsheet to the same directory as the .strings resource file with the filename “Localization-Strings.xlsx”.
  6. Display a message to the user indicating whether the operation was successful or if any errors occurred.
  7. Avoid using the EPPlus assembly.

That last bit was there because at one point it actually tried to introduce an additional Powershell module called EPPlus that I didn’t want.  If you’re familiar with ChatGPT or you’ve read my other articles you can probably imagine the discussion to get it right!

However, after around 15 or 20 minutes I think I got there and the end result was this script:

# Prompt user for path to .strings resource file
$stringsFilePath = Read-Host “Enter the path to the .strings resource file”

# Import the content of the .strings resource file
$stringsContent = Get-Content $stringsFilePath

# Create new Excel spreadsheet
$excel = New-Object -ComObject Excel.Application
$workbook = $excel.Workbooks.Add()
$worksheet = $workbook.Worksheets.Item(1)
$worksheet.Name = “Localization-Strings”

# Add column headers to worksheet
$worksheet.Cells.Item(1,1) = “Comment”
$worksheet.Cells.Item(1,2) = “Key”
$worksheet.Cells.Item(1,3) = “Value”

# Parse the .strings content and add to worksheet
$row = 2
foreach ($line in $stringsContent) {
if ($line -match ‘^/\*’) {
# This line is a comment
$comment = $line.TrimStart(‘/* ‘).TrimEnd(‘ */’)
}
elseif ($line -match ‘^”(.+)”\s*=\s*”(.+)”\s*;$’) {
# This line is a key-value pair
$key = $Matches[1]
$value = $Matches[2]
$worksheet.Cells.Item($row,1) = $comment
$worksheet.Cells.Item($row,2) = $key
$worksheet.Cells.Item($row,3) = $value
$row++
}
}

# Save the spreadsheet
$excel.DisplayAlerts = $false
$workbook.SaveAs(($stringsFilePath -replace ‘\.strings$’, ‘-Strings.xlsx’))
$workbook.Close()
$excel.Quit()

# Display success message
Write-Host “Localization strings have been exported to an Excel spreadsheet.”

This successfully converts my strings file into Excel like this:

So now I could set up the Bilingual Excel filetype in Trados Studio like this:

Where…

  1. I set the source and target column to be the same
  2. the comments from the .strings file are added as context
  3. the key is added as a comment

I could have switched 2. and 3. around… just preferences… but now when I translate I have this using the out of the box preview in Trados Studio 2022:

Or I could use the DSI viewer from the appstore like this:

The DSI viewer gets you more screen estate while translating, the html preview is more pleasing to the eye… all a matter of preference.

So… that’s it!  Pretty cool, and another fantastic way to use AI to help you get more by giving you localization super powers!!

What about the target file!

But wait… what about the target file!!!  Well you could, add the .strings file to the same project you just translated using the Regex Delimited Filetype you tested at the start; then just pre-translate from your translation memory and save the target.  Job done!

But you could also just ask ChatGPT to create a script to put it back again!!  After we… “we”… my language talking to a computer is starting to worry me!  After the AI gave me the script I said thanks:

It asked for more and I obliged.  Sounds easy but this time there was a lot more to’ing and fro’ing.  ChatGPT tried to get me to install all kinds of add-ons which I didn’t want, and it persisted for a while to ignore what I was asking, but in the end I got there too.  I think it took me around 45 minutes to get this right… a lot longer but nothing compared to the time it would have taken me without it.  I’m sure a well versed localization engineer would have done this faster, but to be fair localization engineers probably have a lot of scripts up their sleeves for any occasion after being in the business for a while.  I can imagine this tool being very helpful for the first time, or for tackling something new, even for them.

So here’s the return script:

# Prompt user to select the input file
$inputFile = Read-Host “Enter the path to the Excel file”

# Create a new string builder
$stringsBuilder = New-Object System.Text.StringBuilder

# Import data from the Excel file
$excel = New-Object -ComObject Excel.Application
$workbook = $excel.Workbooks.Open($inputFile)
$worksheet = $workbook.Sheets.Item(1)
$range = $worksheet.UsedRange
$rows = $range.Rows.Count
$cols = $range.Columns.Count

# Iterate over the rows and columns of the Excel data
for ($i = 2; $i -le $rows; $i++) {
$comment = $worksheet.Cells.Item($i, 1).Text
$key = $worksheet.Cells.Item($i, 2).Text
$value = $worksheet.Cells.Item($i, 3).Text
if ($comment -ne “”) {
$stringsBuilder.AppendLine(“/* $comment */”)
}
$stringsBuilder.AppendLine(“`”$key`” = `”$value`”;”)
if ($i -ne $rows) {
$nextComment = $worksheet.Cells.Item($i + 1, 1).Text
if ($comment -ne “” -and $nextComment -ne “”) {
$stringsBuilder.AppendLine()
}
}
}

# Save the output to a file
$outputFile = [System.IO.Path]::ChangeExtension($inputFile, “.strings”)
[System.IO.File]::WriteAllText($outputFile, $stringsBuilder.ToString())

# Close the workbook and Excel
$workbook.Close($false)
$excel.Quit()
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($worksheet) | Out-Null
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($workbook) | Out-Null
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($excel) | Out-Null

# Display a message to the user
Write-Host “Strings file saved to $outputFile.”

I’m not really sure what the best prompts should be to solve a problem like this.  I think at the end of the day you just treat it like a tool.  Ask your question, test the output, explain what’s wrong and try again… and again until you get it right.  So really no different to what you’d do yourself if you were writing… the only difference being I bet you couldn’t write it!

But how do you use it?

I thought that rather than write this all down with even more explanations it would simply be easier to show you.  So here’s how I used the scripts to achieve my goal.

I think there are many applications of this, so please do keep in mind I was just showing you an example of how it could be used.  If you have a tool that already does it then that’s great… try and think about the files you get which are not so easy to work with!  How could you use an AI solution like ChatGPT to help you tackle the problem in ways you probably never even considered before!!

And of course if you want to try this yourself using Powershell, here’s a zip of the .strings file I used and also the two scripts “I” created.

2 thoughts on “ChatGPT + Powershell = Localization Superpowers!

  1. As usual, a very cool article from a cool technology guy!

    The takeway from your article, at least from my standpoint, is that this is really interesting because it opens the door to people who are not quite developers, but have a logical and analytical mind, to write code that could actually work, even if they don’t know much about the intricacies of coding syntax.

    Of course, that also opens the door to a lot of potential problems such as code validity and code maintainability, so I wouldn’t recommend this for a long-term official project that needs to be well architected, but for a one-off quick script that you need to make your job easier (which is a lot of what localization folks deal with), it can be a good approach.

    1. Thanks Michel, and I would agree with you… almost. I think a long-term official project should indeed be well architected, and it needs experienced developers to pull it together. But this technology lends itself really well as a tool for professional developers to not only write mundane code for them, but also to help with solutions to particular problems along the way. When I started looking at GPT as a solution for this stuff I definitely came at it from the perspective you mention here; but since then I’m seeing examples of professional developers using it as a significant productivity tool. So whilst I definitely agree with your takeaway I think there is scope for more in the right hands and we are certainly going to see more and more in the very near future.

Leave a Reply