Skip to content

Instantly share code, notes, and snippets.

@planetis-m
Created September 10, 2024 05:28
Show Gist options
  • Save planetis-m/df20e575367b305086146be3fe0b4f6c to your computer and use it in GitHub Desktop.
Save planetis-m/df20e575367b305086146be3fe0b4f6c to your computer and use it in GitHub Desktop.
import re
def process_line(line):
if not line.startswith('+msgstr'):
return line
# Extract the content within quotes
content = re.search(r'\+msgstr "(.*)"', line)
if not content:
return line
content = content.group(1)
if not content or content.isspace():
return line
words = content.split()
def is_unicode_word(word):
# Skip '&' at the beginning if present
word = word[1:] if word.startswith('&') else word
return word and any(ord(char) > 127 for char in word)
def process_word(word, is_first_word):
if word.startswith('&'):
return '&' + process_word(word[1:], is_first_word)
elif not is_first_word and is_unicode_word(word) and len(word) > 1 and word[0].isupper():
return word[0].lower() + word[1:]
else:
return word
# Process words, keeping the first word as is
processed_words = [process_word(words[0], True)] + [process_word(word, False) for word in words[1:]]
return f'+msgstr "{" ".join(processed_words)}"\n'
def process_file(input_file, output_file):
with open(input_file, 'r', encoding='utf-8') as infile, open(output_file, 'w', encoding='utf-8') as outfile:
for line in infile:
processed_line = process_line(line)
if processed_line != line:
print(line, processed_line, sep='', end='')
outfile.write(processed_line)
# Example usage
input_file = 'new.patch'
output_file = 'output.patch'
process_file(input_file, output_file)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment