Efficiently translation of po files using LLMs

I need to share my thoughts about efficient translation of po files using LLMs…
Maybe other guys did have the same idea and did go further?

I am looking for a way to translate po files based on an existing translated one to help disambiguate translations.
For example, if you provide a po file with

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>msgid "Bank"
msgstr ""
</code>
<code>msgid "Bank" msgstr "" </code>
msgid "Bank"
msgstr ""

No tool, no matter how intelligent, is ever going to know if it’s talking about a financial company or the banks of a river.
But if you provide a French translation:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>msgid: "Bank"
msgstr: "Banque"
</code>
<code>msgid: "Bank" msgstr: "Banque" </code>
msgid: "Bank"
msgstr: "Banque"

then any translation capable LLM should be able to translate that po file entry into Spanish or German or whatever language it knows.

Did anybody ever think about this?

Actually, with the help of Claude and ChatGPT (free versions 😉 I tried this idea myself and ended up with this pretty simple piece of Python code that:

  1. loads a LLM specialized in translation (facebook/mbart-large-50-many-to-many-mmt),
  2. reads the po file entry by entry
  3. tries to provide a translation of the English msgid using the French translation as context:
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>import polib
from transformers import pipeline
def translate_po_file(input_file, output_file):
# Load multilingual translation template
translator = pipeline("translation", model="facebook/mbart-large-50-many-to-many-mmt")
# Load input .po file
po = polib.pofile(input_file)
# Browse each entry and translate
for entry in po:
if entry.msgid and not entry.fuzzy:
# Preparing the context and the text to be translated
context = entry.msgstr if entry.msgstr else entry.msgid
text_to_translate = entry.msgid
# Building the prompt
prompt = f"Translate to Spanish. Context: {context}nText: {text_to_translate}"
# Translate into Spanish
translation = translator(prompt, src_lang="en_XX", tgt_lang="es_XX")[0]['translation_text']
# Extract the translated part (after "Text: ")
translation = translation.split("Text: ")[-1].strip()
# Update translation
entry.msgstr = translation
# Save the new .po file
po.save(output_file)
# Example of use
input_file = "input.po"
output_file = "output.es.po"
translate_po_file(input_file, output_file)
</code>
<code>import polib from transformers import pipeline def translate_po_file(input_file, output_file): # Load multilingual translation template translator = pipeline("translation", model="facebook/mbart-large-50-many-to-many-mmt") # Load input .po file po = polib.pofile(input_file) # Browse each entry and translate for entry in po: if entry.msgid and not entry.fuzzy: # Preparing the context and the text to be translated context = entry.msgstr if entry.msgstr else entry.msgid text_to_translate = entry.msgid # Building the prompt prompt = f"Translate to Spanish. Context: {context}nText: {text_to_translate}" # Translate into Spanish translation = translator(prompt, src_lang="en_XX", tgt_lang="es_XX")[0]['translation_text'] # Extract the translated part (after "Text: ") translation = translation.split("Text: ")[-1].strip() # Update translation entry.msgstr = translation # Save the new .po file po.save(output_file) # Example of use input_file = "input.po" output_file = "output.es.po" translate_po_file(input_file, output_file) </code>
import polib
from transformers import pipeline

def translate_po_file(input_file, output_file):
  # Load multilingual translation template
  translator = pipeline("translation", model="facebook/mbart-large-50-many-to-many-mmt")
  # Load input .po file
  po = polib.pofile(input_file)
  # Browse each entry and translate
  for entry in po:
    if entry.msgid and not entry.fuzzy:
      # Preparing the context and the text to be translated
      context = entry.msgstr if entry.msgstr else entry.msgid
      text_to_translate = entry.msgid
      # Building the prompt
      prompt = f"Translate to Spanish. Context: {context}nText: {text_to_translate}"
      # Translate into Spanish
      translation = translator(prompt, src_lang="en_XX", tgt_lang="es_XX")[0]['translation_text']
      # Extract the translated part (after "Text: ")
      translation = translation.split("Text: ")[-1].strip()
      # Update translation
      entry.msgstr = translation
      # Save the new .po file
      po.save(output_file)

# Example of use
input_file = "input.po"
output_file = "output.es.po"
translate_po_file(input_file, output_file)

You’ll need to `pip install’ several libraries before running this code (I’ve done several tests so I’m not sure they’re all still needed).

polib
transformers
torch
sentencepiece
sacremoses
protobuf

Unfortunately, this does not work very well.
I think I have to

  1. deal with the placeholders included in the po files (e.g. {some_variable} or %s or %(some_variables)s …)
  2. and probably provide a much better prompt to explain to the model how to use the context for translation…

I had a look on github, where there are a lot of “gpt for po” projects, but I didn’t find any that use an already done translation as disambiguation context, although I think this is absolutely key for po, where sentences are very short (even just one word) and thus don’t provide enough context for the translator to work properly…

Any feedback welcome…

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa

Efficiently translation of po files using LLMs

I need to share my thoughts about efficient translation of po files using LLMs…
Maybe other guys did have the same idea and did go further?

I am looking for a way to translate po files based on an existing translated one to help disambiguate translations.
For example, if you provide a po file with

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>msgid "Bank"
msgstr ""
</code>
<code>msgid "Bank" msgstr "" </code>
msgid "Bank"
msgstr ""

No tool, no matter how intelligent, is ever going to know if it’s talking about a financial company or the banks of a river.
But if you provide a French translation:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>msgid: "Bank"
msgstr: "Banque"
</code>
<code>msgid: "Bank" msgstr: "Banque" </code>
msgid: "Bank"
msgstr: "Banque"

then any translation capable LLM should be able to translate that po file entry into Spanish or German or whatever language it knows.

Did anybody ever think about this?

Actually, with the help of Claude and ChatGPT (free versions 😉 I tried this idea myself and ended up with this pretty simple piece of Python code that:

  1. loads a LLM specialized in translation (facebook/mbart-large-50-many-to-many-mmt),
  2. reads the po file entry by entry
  3. tries to provide a translation of the English msgid using the French translation as context:
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>import polib
from transformers import pipeline
def translate_po_file(input_file, output_file):
# Load multilingual translation template
translator = pipeline("translation", model="facebook/mbart-large-50-many-to-many-mmt")
# Load input .po file
po = polib.pofile(input_file)
# Browse each entry and translate
for entry in po:
if entry.msgid and not entry.fuzzy:
# Preparing the context and the text to be translated
context = entry.msgstr if entry.msgstr else entry.msgid
text_to_translate = entry.msgid
# Building the prompt
prompt = f"Translate to Spanish. Context: {context}nText: {text_to_translate}"
# Translate into Spanish
translation = translator(prompt, src_lang="en_XX", tgt_lang="es_XX")[0]['translation_text']
# Extract the translated part (after "Text: ")
translation = translation.split("Text: ")[-1].strip()
# Update translation
entry.msgstr = translation
# Save the new .po file
po.save(output_file)
# Example of use
input_file = "input.po"
output_file = "output.es.po"
translate_po_file(input_file, output_file)
</code>
<code>import polib from transformers import pipeline def translate_po_file(input_file, output_file): # Load multilingual translation template translator = pipeline("translation", model="facebook/mbart-large-50-many-to-many-mmt") # Load input .po file po = polib.pofile(input_file) # Browse each entry and translate for entry in po: if entry.msgid and not entry.fuzzy: # Preparing the context and the text to be translated context = entry.msgstr if entry.msgstr else entry.msgid text_to_translate = entry.msgid # Building the prompt prompt = f"Translate to Spanish. Context: {context}nText: {text_to_translate}" # Translate into Spanish translation = translator(prompt, src_lang="en_XX", tgt_lang="es_XX")[0]['translation_text'] # Extract the translated part (after "Text: ") translation = translation.split("Text: ")[-1].strip() # Update translation entry.msgstr = translation # Save the new .po file po.save(output_file) # Example of use input_file = "input.po" output_file = "output.es.po" translate_po_file(input_file, output_file) </code>
import polib
from transformers import pipeline

def translate_po_file(input_file, output_file):
  # Load multilingual translation template
  translator = pipeline("translation", model="facebook/mbart-large-50-many-to-many-mmt")
  # Load input .po file
  po = polib.pofile(input_file)
  # Browse each entry and translate
  for entry in po:
    if entry.msgid and not entry.fuzzy:
      # Preparing the context and the text to be translated
      context = entry.msgstr if entry.msgstr else entry.msgid
      text_to_translate = entry.msgid
      # Building the prompt
      prompt = f"Translate to Spanish. Context: {context}nText: {text_to_translate}"
      # Translate into Spanish
      translation = translator(prompt, src_lang="en_XX", tgt_lang="es_XX")[0]['translation_text']
      # Extract the translated part (after "Text: ")
      translation = translation.split("Text: ")[-1].strip()
      # Update translation
      entry.msgstr = translation
      # Save the new .po file
      po.save(output_file)

# Example of use
input_file = "input.po"
output_file = "output.es.po"
translate_po_file(input_file, output_file)

You’ll need to `pip install’ several libraries before running this code (I’ve done several tests so I’m not sure they’re all still needed).

polib
transformers
torch
sentencepiece
sacremoses
protobuf

Unfortunately, this does not work very well.
I think I have to

  1. deal with the placeholders included in the po files (e.g. {some_variable} or %s or %(some_variables)s …)
  2. and probably provide a much better prompt to explain to the model how to use the context for translation…

I had a look on github, where there are a lot of “gpt for po” projects, but I didn’t find any that use an already done translation as disambiguation context, although I think this is absolutely key for po, where sentences are very short (even just one word) and thus don’t provide enough context for the translator to work properly…

Any feedback welcome…

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật