Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to wrap the "msgstr" with the specified width? #139

Closed
hwhsu1231 opened this issue May 19, 2023 · 2 comments
Closed

How to wrap the "msgstr" with the specified width? #139

hwhsu1231 opened this issue May 19, 2023 · 2 comments

Comments

@hwhsu1231
Copy link

Problem Description

Recently, I tried to write a Python script to merge a new version new.po file into an old version old.po file. That is, if there exists an existing msgid in the old.po exatly the same with the corresponding msgid in the new.po, then it should copy the translated msgstr of the entry from new.po into a old_updated.po directly. And in fact, I already implemented a simple merge.py.

Click to expand
import argparse
import polib
import os

# Add a parser
parser = argparse.ArgumentParser(description='Sync PO files.')

# Add arguments
parser.add_argument('old_po', type=str, help='Path to the old PO file')
parser.add_argument('new_po', type=str, help='Path to the new PO file')

# Parse arguments
args = parser.parse_args()

print("Old PO file path:", args.old_po)
print("New PO file path:", args.new_po)

po_old = polib.pofile(args.old_po)
po_new = polib.pofile(args.new_po)

# Create a dictionary of the new translations
new_translations = {entry.msgid: entry.msgstr for entry in po_new}

# Update the old translations
for entry in po_old:
    if entry.msgid in new_translations:
        entry.msgstr = new_translations[entry.msgid]        

# Split the filename and extension of old_po
old_po_name, old_po_ext = os.path.splitext(args.old_po)

# Create a new file path
old_po_updated = old_po_name + "_updated" + old_po_ext

# Save the old PO file with the updated translations
po_old.save(old_po_updated)

However, there is another problem bothering me. That is, the updated old_updated.po doesn't have the same "wrap at" format for the copied msgstr. Take the following results for reference:

  • The following is the format of an entry in the new.po, which is wrapped at 79 location by Poedit:

    #: ../../index.rst:6
    msgid ""
    "CMake is a tool to manage building of source code.  Originally, CMake was "
    "designed as a generator for various dialects of ``Makefile``, today CMake "
    "generates modern buildsystems such as ``Ninja`` as well as project files for "
    "IDEs such as Visual Studio and Xcode."
    msgstr ""
    "CMake 是一款用來管理來源碼建置的工具。原本,CMake 是被設計做為不同 dialect "
    "的 ``Makefile`` 的產生器。如今,CMake 可以產生像是 ``Ninja`` 這樣的現代建置系"
    "統,以及像是 Visual Studio 和 Xcode 這樣的 IDE 專案檔。"
  • The following is the format of the corresponding entry in the old.po, which is empty in msgstr:

    #: ../../../index.rst:6
    msgid ""
    "CMake is a tool to manage building of source code.  Originally, CMake was "
    "designed as a generator for various dialects of ``Makefile``, today CMake "
    "generates modern buildsystems such as ``Ninja`` as well as project files for "
    "IDEs such as Visual Studio and Xcode."
    msgstr ""
  • The following is the format of the corresponding entry in the old_updated.po after runnnig python merge.py old.po new.po:

    #: ../../../index.rst:6
    msgid ""
    "CMake is a tool to manage building of source code.  Originally, CMake was "
    "designed as a generator for various dialects of ``Makefile``, today CMake "
    "generates modern buildsystems such as ``Ninja`` as well as project files for"
    " IDEs such as Visual Studio and Xcode."
    msgstr ""
    "CMake 是一款用來管理來源碼建置的工具。原本,CMake 是被設計做為不同 dialect 的 ``Makefile`` 的產生器。如今,CMake"
    " 可以產生像是 ``Ninja`` 這樣的現代建置系統,以及像是 Visual Studio 和 Xcode 這樣的 IDE 專案檔。"

As we can see, the old_updated.po file doesn't have the same "wrap at" format as the new.po file.

How should I modify this merge.py Python script in order to do the same format provided by Poedit?

cc: @izimobil

Demonstration

merge.zip

@izimobil
Copy link
Owner

Duplicate of #96
See this issue for possible workarounds (especially the msgcat trick)

@hwhsu1231
Copy link
Author

@izimobil

The msgcat workaround works! Thanks!

msgcat old_updated.po -w79 -o old_updated_2.po

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants