Make use of RegEx in Translating

  1. Home
  2. Translator++
  3. Unity Engine
  4. Make use of RegEx in Translating

Make use of RegEx in Translating

As you know, the xUnity.AutoTranslator method is on the go, but sometimes the displayed text can contain variables that cannot be individually explored. For example, damage output, download, and game loading information, status updates, shopping confirmation, and many more. That’s why you need to utilize regular expressions.

Regular expressions, commonly abbreviated as regex, are a powerful tool used in computer programming and text processing to match and manipulate strings of text based on patterns. A regex is a sequence of characters that defines a search pattern and is used to match and manipulate text in a flexible and efficient manner.

For example, you can see that during loading, there is continuous translation like:

データダウンロード中 0%=Downloading data 0%
データダウンロード中 10%=Downloading data 10%
データダウンロード中 20%=Downloading data 20%
データダウンロード中 30%=Downloading data 30%
データダウンロード中 40%=Downloading data 40%
データダウンロード中 50%=Downloading data 50%

You can replace all of them with just one line using:

r:"^データダウンロード中 (\d+)%$"=Downloading data $1%

to cover all the loading from 0 to 100.

Regexes can be applied to translations in two different ways. The following two sections describe these two ways:
regular regex
r:"text is written here"=translation
splitter regex
sr:"text is written here"=translation

1. Example of using Regular Regex.

自分の物理攻撃力を6234アップ=Boosts own physical attack by 6234.

where 6234 can change throughout the gameplay for that, you can write

r:"^自分の物理攻撃力を(\d+)アップ$"=Boosts own physical attack by $1. 

where ([0-9]+) or (\d+) represents any number.

another example:

Corneliaがクランに加入しました。=Cornelia has joined the clan. 
コーネリアがクランに加入しました。=コーネリア has joined the clan. 
Dreamsaviorがクランに加入しました。=Dreamsavior has joined the clan. 
ネムがクランに加入しました。=ネム has joined the clan.

You know that Cornelia, コーネリア, Dreamsavior, ネム are IGNs that should not be translated.

You can write them in regex like this

r:"^(.{2,10})がクランに加入しました。$"=$1 has joined the clan.

Where (.{2,10}) represents the player’s name within the range of 2 to 10 characters.

2. Example of using Splitter Regex.

Sometimes games like to combine texts before displaying them on screen. This means that it can sometimes be hard to know what text to add to the translation file because it appears in a number of different ways.

This section explores a solution to this by applying a regex to split the text to be translated into individual pieces before trying to make lookups for the specified texts.

For example:

イベントストーリーが\n解放されました。$"=Event Story\nhas been Unlocked. 
メインストーリーが\n解放されました。$"=Main Story\nhas been Unlocked. 
アリーナが\n解放されました。$"=Arena\nhas been Unlocked.

You can see the pattern, where you already have a list of translations for:

イベントストーリー=Event Story 
メインストーリー=Main Story 

and so on.

Then you can write the regex like this:

sr:"^(.+)が\n解放されました。$"=$1\nhas been Unlocked.

where (.+) searches for translations again in your translation folder.


Please note that splitter regex can result in recursion, which is why you should write it as specific as possible. You may also need to set a limit on how much recursion occurs.

Limiting Recursion config


For more information:


You can try and learn about the regular expression at this site:

Tags , , , , , , , ,
Was this article helpful to you? No Yes

How can we help?