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

Implementation of msgcat and msgmerge utilities from GNU gettext #1161

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add options update, backup, and c_overwrite for a different compendiu…
…m handling logic

 * Implement `update` to update the source file instead of writing to the current output file

 * Implement `backup` to save a backup of the source file before making any updates

 * Implement `c_overwrite` to use a new mode of handling the compendium, where translations from the compendium overwrite messages in the output file
  • Loading branch information
soft-suroleb committed Dec 9, 2024
commit cb71c9370edd57f172262ae32a8c316f42a30351
56 changes: 31 additions & 25 deletions babel/messages/frontend.py
Original file line number Diff line number Diff line change
@@ -1005,9 +1005,10 @@ class MessageMerge(CommandMixin):
('input-files', None, ''),
('directory=', 'D', ''),
('compendium=', 'C', ''),
('c-overwrite', '', ''),
('update', 'U', ''),
('output-file=', 'o', ''),
('backup=', None, ''),
('backup', None, ''),
('suffix=', None, ''),
('multi-domain', 'm', ''),
('for-msgfmt', None, ''),
@@ -1055,6 +1056,8 @@ class MessageMerge(CommandMixin):
'no-wrap',
'sort-output',
'sort-by-file',
'c-overwrite',
'backup',
]

option_choices = {
@@ -1065,13 +1068,14 @@ def initialize_options(self):
self.input_files = None #
self.directory = None
self.compendium = None #~
self.update = None
self.c_overwrite = False #
self.update = None #
self.output_file = None #
self.backup = None
self.suffix = None
self.backup = False #
self.suffix = '~' #
self.multi_domain = None
self.for_msgfmt = None
self.no_fuzzy_matching = None
self.no_fuzzy_matching = None #
self.previous = None
self.properties_input = None
self.stringtable_input = None
@@ -1095,8 +1099,8 @@ def initialize_options(self):
def finalize_options(self):
if not self.input_files or len(self.input_files) != 2:
raise OptionError('must be two po files')
if not self.output_file:
raise OptionError('you must specify the output file')
if not self.output_file and not self.update:
raise OptionError('you must specify the output file or update existing')

if self.no_wrap and self.width:
raise OptionError("'--no-wrap' and '--width' are mutually exclusive")
@@ -1107,36 +1111,38 @@ def finalize_options(self):

def run(self):
def_file, ref_file = self.input_files
with open(def_file, 'r') as pofile:
def_catalog = read_po(pofile)

if self.update and self.backup:
shutil.copy(def_file, def_file + self.suffix)

with open(def_file, 'r') as pofile:
catalog = read_po(pofile)
with open(ref_file, 'r') as pofile:
ref_catalog = read_po(pofile)

ref_catalog.mime_headers = def_catalog.mime_headers
ref_catalog.header_comment = def_catalog.header_comment

for message in def_catalog:
if not message.id:
continue
if message.id in ref_catalog:
ref_catalog[message.id].string = message.string
else:
ref_catalog.obsolete[message.id] = message
catalog.update(
ref_catalog,
no_fuzzy_matching=self.no_fuzzy_matching
)

if self.compendium:
with open(self.compendium, 'r') as pofile:
compendium_catalog = read_po(pofile)

for message in compendium_catalog:
if message.id in ref_catalog and not ref_catalog[message.id].string:
ref_catalog[message.id].string = message.string
current = catalog[message.id]
if message.id in catalog and (not current.string or current.fuzzy or self.c_overwrite):
if self.c_overwrite and not current.fuzzy and current.string:
catalog.obsolete[message.id] = current.clone()

ref_catalog.fuzzy = False
with open(self.output_file, 'wb') as outfile:
current.string = message.string
current.flags = [flag for flag in current.flags if flag != 'fuzzy']
current.auto_comments.append(self.compendium)

output_path = def_file if self.update else self.output_file
with open(output_path, 'wb') as outfile:
write_po(
outfile,
ref_catalog,
catalog,
width=self.width,
sort_by_file=self.sort_by_file,
sort_output=self.sort_output,