from lxml import etree import os import re # Strings we want to replace but that we also replace automatically for XTB files branding_replacements = [ (r'The Chromium Authors. All rights reserved.', r'The Brave Authors. All rights reserved.'), (r'Google LLC. All rights reserved.', r'The Brave Authors. All rights reserved.'), (r'The Chromium Authors', r'Brave Software Inc'), (r'Google Chrome', r'Brave'), (r'(Google)(?! Play)', r'Brave'), (r'Chromium', r'Brave'), (r'Chrome', r'Brave'), ] # Strings we want to replace but that we need to use Transifex for # to translate the XTB files default_replacements = [ (r'Brave Web Store', r'Web Store'), (r'Automatically send usage statistics and crash reports to Brave', r'Automatically send crash reports to Brave'), (r'Automatically sends usage statistics and crash reports to Brave', r'Automatically sends crash reports to Brave'), (r'You\'re incognito', r'This is a private window'), (r'an incognito', r'a private'), (r'an Incognito', r'a Private'), (r'incognito', r'private'), (r'Incognito', r'Private'), (r'inco&gnito', r'&private'), (r'Inco&gnito', r'&Private'), (r'People', r'Profiles'), # 'people' but only in the context of profiles, not humans. (r'(?', ' />') xml_content = xml_content.replace(r'', r'') return xml_content def write_xml_file_from_tree(string_path, xml_tree): """Writes out an xml tree to a file with Chromium GRD formatting replacements""" transformed_content = etree.tostring(xml_tree, pretty_print=True, xml_declaration=True, encoding='UTF-8') transformed_content = format_xml_style(transformed_content) with open(string_path, mode='w') as f: f.write(transformed_content) def update_braveified_grd_tree_override(source_xml_tree, branding_replacements_only): """Takes in a grd(p) tree and replaces all messages and comments with Brave wording""" for elem in source_xml_tree.xpath('//message'): generate_braveified_node(elem, False, branding_replacements_only) for elem in source_xml_tree.xpath('//comment()'): generate_braveified_node(elem, True, branding_replacements_only) def write_braveified_grd_override(source_string_path): """Takes in a grd file and replaces all messages and comments with Brave wording""" source_xml_tree = etree.parse(source_string_path) update_braveified_grd_tree_override(source_xml_tree, False) write_xml_file_from_tree(source_string_path, source_xml_tree) def get_override_file_path(source_string_path): """Obtain src/brave source string override path for local grd strings with replacements""" filename = os.path.basename(source_string_path) (basename, ext) = filename.split('.') if ext == 'xtb': # _override goes after the string name but before the _[locale].xtb part parts = basename.split('_') parts.insert(-1, 'override') override_string_path = os.path.join(os.path.dirname(source_string_path), '.'.join(('_'.join(parts), ext))) else: override_string_path = os.path.join(os.path.dirname(source_string_path), '.'.join((basename + '_override', ext))) return override_string_path