Skip to content

Instantly share code, notes, and snippets.

@flodolo
Created October 19, 2022 12:18
Show Gist options
  • Save flodolo/a75b8a1953bf7d8c2e6ff1c1a87626b1 to your computer and use it in GitHub Desktop.
Save flodolo/a75b8a1953bf7d8c2e6ff1c1a87626b1 to your computer and use it in GitHub Desktop.
Import translations for Firefox Translations notification bar
#! /usr/bin/env python3
import argparse
from copy import deepcopy
import json
import os
from urllib.request import urlopen
def main():
parser = argparse.ArgumentParser()
parser.add_argument(
"--path",
required=True,
dest="base_folder",
help="Path to folder including subfolders for all locales",
)
args = parser.parse_args()
string_list = [
{
"id": "translationBarPageIsIn",
"url": "https://transvision.flod.org/api/v1/entity/gecko_strings/?id=browser/chrome/browser/translation.dtd:translation.thisPageIsIn.label&repo=gecko_strings",
},
{
"id": "translationBarTranslateButton",
"url": "https://transvision.flod.org/api/v1/entity/gecko_strings/?id=browser/chrome/browser/translation.dtd:translation.translate.button&repo=gecko_strings",
},
{
"id": "translationBarOptionsButton",
"url": "https://transvision.flod.org/api/v1/entity/gecko_strings/?id=browser/chrome/browser/translation.dtd:translation.options.menu&repo=gecko_strings",
},
{
"id": "translationBarNeverThisSiteLabel",
"url": "https://transvision.flod.org/api/v1/entity/gecko_strings/?id=browser/chrome/browser/translation.dtd:translation.options.neverForSite.label&repo=gecko_strings",
},
{
"id": "translationBarNeverThisSiteAccesskey",
"url": "https://transvision.flod.org/api/v1/entity/gecko_strings/?id=browser/chrome/browser/translation.dtd:translation.options.neverForSite.accesskey&repo=gecko_strings",
},
{
"id": "optionsMenuLabel",
"url": "https://transvision.flod.org/api/v1/entity/gecko_strings/?id=browser/chrome/browser/translation.dtd:translation.options.preferences.label&repo=gecko_strings",
},
{
"id": "optionsMenuAccesskey",
"url": "https://transvision.flod.org/api/v1/entity/gecko_strings/?id=browser/chrome/browser/translation.dtd:translation.options.preferences.accesskey&repo=gecko_strings",
},
{
"id": "closeNotification",
"url": "https://transvision.flod.org/api/v1/entity/gecko_strings/?id=toolkit/chrome/global/notification.dtd:closeNotification.tooltip&repo=gecko_strings",
},
{
"id": "neverForLanguageLabel",
"url": "https://transvision.flod.org/api/v1/entity/gecko_strings/?id=browser/chrome/browser/translation.properties:translation.options.neverForLanguage.label&repo=gecko_strings",
},
{
"id": "neverForLanguageAccesskey",
"url": "https://transvision.flod.org/api/v1/entity/gecko_strings/?id=browser/chrome/browser/translation.properties:translation.options.neverForLanguage.accesskey&repo=gecko_strings",
},
]
json_template = {
"translationBarPageIsIn": {
"message": "This page is in",
"description": "This message is followed by a dropdown list of language names. If this doesn't work for your locale, you can translate it as ”Language of the page:”"
},
"translationBarTranslateButton": {
"message": "Translate",
"description": "Label for the button to start the translation"
},
"translationBarOptionsButton": {
"message": "Options",
"description": "Label for an Options button that will display a dropdown menu"
},
"translationBarNeverThisSiteLabel": {
"message": "Never translate this site",
"description": "Label for a checkbox to never translate the current website"
},
"translationBarNeverThisSiteAccesskey": {
"message": "e",
"description": "Accesskey for a checkbox to never translate the current website"
},
"optionsMenuLabel": {
"message": "Translation preferences",
"description": "Label for options menu item in the Options dropdown"
},
"optionsMenuAccesskey": {
"message": "T",
"description": "Accesskey for options menu item in the Options dropdown"
},
"closeNotification": {
"message": "Close this message",
"description": "Close notification tooltip"
},
"neverForLanguageLabel": {
"message": "Never translate $LANGUAGE$",
"description": "Label for options menu item in the Options dropdown. Language is replaced by a language name.",
"placeholders": {
"language": {
"content": "$1",
"example": "Italian"
}
}
},
"neverForLanguageAccesskey": {
"message": "N",
"description": "Accesskey for options menu item in the Options dropdown."
}
}
base_folder = args.base_folder
locales = [
d
for d in os.listdir(args.base_folder)
if os.path.isdir(os.path.join(base_folder, d)) and not d.startswith(".") and d != "en_US"
]
locales.sort()
# Get translations from Transvision
translations = {}
for e in string_list:
try:
response = urlopen(e["url"])
json_data = json.load(response)
translations[e["id"]] = json_data
except Exception as e:
print(e)
for loc in locales:
print(f"Writing {loc}")
with open(os.path.join(base_folder, loc, "messages.json"), "r") as f:
existing_strings = json.load(f)
# Inject translations in a copy of json_template
json_template_copy = deepcopy(json_template)
for id in json_template_copy:
moz_locale = loc.replace("_", "-")
if id in translations and moz_locale in translations[id]:
json_template_copy[id]["message"] = translations[id][moz_locale]
else:
# Remove if translation is not available
json_template_copy.remove(id)
new_content = existing_strings | json_template_copy
with open(os.path.join(base_folder, loc, "messages.json"), "w") as f:
json.dump(new_content, f, indent=2, ensure_ascii=False)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment