-
Notifications
You must be signed in to change notification settings - Fork 903
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
Reduce amount of strings we need to pull/push with Transifex #5795
Merged
Changes from 7 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
ab1a185
Pull from Local xtbs when possible
bbondy 4e0553d
Generate override files for strings we override
bbondy 5779f6c
Preserve file elements which point to xtb in override grds
bbondy 8ba80c0
Send up override files to Transifex
bbondy 0a6f323
If there are any modified parts or messages include strings
bbondy b3a42e1
No more need to check for missing Chromium strings
bbondy 31b1416
Recombine override files into xtb files in py
bbondy 4a6ceb8
Add generated override grd and grdp files
bbondy 862435f
Update XTB files
bbondy 4082c24
GRDP minor updates
bbondy e8dc47e
Merge pull request #5998 from brave/l10n-less-xtb-grd
bbondy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
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'(?<!authenticate )people', r'profiles'), | ||
(r'(Person)(?!\w)', r'Profile'), | ||
(r'(person)(?!\w)', r'profile'), | ||
(r'Bookmarks Bar\n', r'Bookmarks\n'), | ||
(r'Bookmarks bar\n', r'Bookmarks\n'), | ||
(r'bookmarks bar\n', r'bookmarks\n'), | ||
] | ||
|
||
|
||
# Fix up some strings after aggressive first round replacement. | ||
fixup_replacements = [ | ||
(r'Brave Cloud Print', r'Google Cloud Print'), | ||
(r'Brave Docs', r'Google Docs'), | ||
(r'Brave Drive', r'Google Drive'), | ||
(r'Brave OS', r'Chrome OS'), | ||
(r'Brave Safe Browsing', r'Google Safe Browsing'), | ||
(r'Safe Browsing \(protects you and your device from dangerous sites\)', | ||
r'Google Safe Browsing (protects you and your device from dangerous sites)'), | ||
(r'Sends URLs of some pages you visit to Brave', r'Sends URLs of some pages you visit to Google'), | ||
(r'Google Google', r'Google'), | ||
] | ||
|
||
|
||
# Replacements for text nodes and neither for inside descriptions nor comments | ||
main_text_only_replacements = [ | ||
(r'Copyright', u'Copyright \xa9'), | ||
] | ||
|
||
|
||
def braveify_grd_text(text, is_main_text, branding_replacements_only): | ||
"""Replaces text string to Brave wording""" | ||
for (pattern, to) in branding_replacements: | ||
text = re.sub(pattern, to, text) | ||
if not branding_replacements_only: | ||
for (pattern, to) in default_replacements: | ||
text = re.sub(pattern, to, text) | ||
for (pattern, to) in fixup_replacements: | ||
text = re.sub(pattern, to, text) | ||
if is_main_text: | ||
for (pattern, to) in main_text_only_replacements: | ||
text = re.sub(pattern, to, text) | ||
return text | ||
|
||
|
||
def generate_braveified_node(elem, is_comment, branding_replacements_only): | ||
"""Replaces a node and attributes to Brave wording""" | ||
if elem.text: | ||
elem.text = braveify_grd_text(elem.text, not is_comment, branding_replacements_only) | ||
|
||
if elem.tail: | ||
elem.tail = braveify_grd_text(elem.tail, not is_comment, branding_replacements_only) | ||
|
||
if 'desc' in elem.keys(): | ||
elem.attrib['desc'] = braveify_grd_text(elem.attrib['desc'], False, branding_replacements_only) | ||
if 'meaning' in elem.keys(): | ||
elem.attrib['meaning'] = braveify_grd_text(elem.attrib['meaning'], False, branding_replacements_only) | ||
for child in elem: | ||
generate_braveified_node(child, is_comment, branding_replacements_only) | ||
|
||
|
||
def format_xml_style(xml_content): | ||
"""Formats an xml file according to how Chromium GRDs are formatted""" | ||
xml_content = re.sub(r'\s+desc="', r' desc="', xml_content) | ||
xml_content = xml_content.replace('/>', ' />') | ||
xml_content = xml_content.replace(r'<?xml version="1.0" encoding="UTF-8"?>', | ||
r'<?xml version=\'1.0\' encoding=\'UTF-8\'?>') | ||
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 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are these two lines in conflict with each other? Or maybe I'm not understanding how this works...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just case sensitive replacement. Bar vs bar. Could be done with a regex probably too
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, missed that!