Changeset - 36a36ebdf4bb
[Not reviewed]
tip stable
0 1 0
Mads Kiilerich - 3 years ago 2022-12-12 22:24:50
mads@kiilerich.com
Grafted from: 0c9c91ac3873
i18n: prevent msgmerge fuzzy matching - it is too random
1 file changed with 1 insertions and 1 deletions:
0 comments (0 inline, 0 general)
scripts/i18n_utils.py
Show inline comments
 
@@ -147,49 +147,49 @@ def _normalize_po(raw_content):
 
        if '\n#, fuzzy' in raw_chunk:  # might be like "#, fuzzy, python-format"
 
            continue  # drop crazy auto translation that is worse than useless
 
        # strip all comment lines from chunk
 
        chunk_lines = [
 
            line
 
            for line in raw_chunk.splitlines()
 
            if line
 
            and not line.startswith('#')
 
        ]
 
        if not chunk_lines:
 
            continue
 
        # check lines starting from first msgstr, skip chunk if no translation lines
 
        msgstr_i = [i for i, line in enumerate(chunk_lines) if line.startswith('msgstr')]
 
        if (
 
            chunk_lines[0].startswith('msgid') and
 
            msgstr_i and
 
            all(line.endswith(' ""') for line in chunk_lines[msgstr_i[0]:])
 
        ):  # skip translation chunks that doesn't have any actual translations
 
            continue
 
        chunks.append('\n'.join(chunk_lines) + '\n')
 
    return '\n'.join(chunks)
 

	
 
def _normalize_po_file(po_file, merge_pot_file=None, strip=False):
 
    if merge_pot_file:
 
        runcmd(['msgmerge', '--width=76', '--backup=none', '--previous',
 
        runcmd(['msgmerge', '--width=76', '--backup=none', '--previous', '--no-fuzzy-matching',
 
                '--update', po_file, '-q', merge_pot_file])
 
    if strip:
 
        po_tmp = po_file + '.tmp'
 
        with open(po_file, 'r') as src, open(po_tmp, 'w') as dest:
 
            raw_content = src.read()
 
            normalized_content = _normalize_po(raw_content)
 
            dest.write(normalized_content)
 
        os.rename(po_tmp, po_file)
 

	
 
def _normalized_diff(file1, file2, merge_pot_file=None, strip=False):
 
    # Create temporary copies of both files
 
    temp1 = tempfile.NamedTemporaryFile(prefix=os.path.basename(file1))
 
    temp2 = tempfile.NamedTemporaryFile(prefix=os.path.basename(file2))
 
    debug('normalized_diff: %s -> %s / %s -> %s' % (file1, temp1.name, file2, temp2.name))
 
    shutil.copyfile(file1, temp1.name)
 
    shutil.copyfile(file2, temp2.name)
 
    # Normalize them in place
 
    _normalize_po_file(temp1.name, merge_pot_file=merge_pot_file, strip=strip)
 
    _normalize_po_file(temp2.name, merge_pot_file=merge_pot_file, strip=strip)
 
    # Now compare
 
    try:
 
        runcmd(['diff', '-u', temp1.name, temp2.name])
 
    except subprocess.CalledProcessError as e:
 
        return e.returncode
0 comments (0 inline, 0 general)