Add Translations 2.0 migrate script (#34261)
parent
9f1bffe3be
commit
72cc656b7e
|
@ -1,9 +1,8 @@
|
|||
"""Find translation keys that are in Lokalise but no longer defined in source."""
|
||||
import json
|
||||
|
||||
from .const import INTEGRATIONS_DIR, PROJECT_ID
|
||||
from .lokalise import Lokalise
|
||||
from .util import get_lokalise_token
|
||||
from .const import INTEGRATIONS_DIR
|
||||
from .lokalise import get_api
|
||||
|
||||
|
||||
def find_extra(base, translations, path_prefix, missing_keys):
|
||||
|
@ -50,7 +49,7 @@ def run():
|
|||
print("No missing translations!")
|
||||
return
|
||||
|
||||
lokalise = Lokalise(PROJECT_ID, get_lokalise_token())
|
||||
lokalise = get_api()
|
||||
|
||||
to_delete = []
|
||||
|
||||
|
|
|
@ -1,6 +1,14 @@
|
|||
"""API for Lokalise."""
|
||||
import requests
|
||||
|
||||
from .const import PROJECT_ID
|
||||
from .util import get_lokalise_token
|
||||
|
||||
|
||||
def get_api() -> "Lokalise":
|
||||
"""Get Lokalise API."""
|
||||
return Lokalise(PROJECT_ID, get_lokalise_token())
|
||||
|
||||
|
||||
class Lokalise:
|
||||
"""Lokalise API."""
|
||||
|
@ -28,7 +36,7 @@ class Lokalise:
|
|||
return req.json()
|
||||
|
||||
def keys_list(self, params={}):
|
||||
"""Fetch key ID from a name.
|
||||
"""List keys.
|
||||
|
||||
https://app.lokalise.com/api2docs/curl/#transition-list-all-keys-get
|
||||
"""
|
||||
|
@ -40,3 +48,10 @@ class Lokalise:
|
|||
https://app.lokalise.com/api2docs/curl/#transition-delete-multiple-keys-delete
|
||||
"""
|
||||
return self.request("DELETE", "keys", {"keys": key_ids})
|
||||
|
||||
def keys_bulk_update(self, updates):
|
||||
"""Update multiple keys.
|
||||
|
||||
https://app.lokalise.com/api2docs/curl/#transition-bulk-update-put
|
||||
"""
|
||||
return self.request("PUT", "keys", {"keys": updates})
|
||||
|
|
|
@ -0,0 +1,50 @@
|
|||
"""Migrate things."""
|
||||
import json
|
||||
from pprint import pprint
|
||||
|
||||
from .const import INTEGRATIONS_DIR
|
||||
from .lokalise import get_api
|
||||
|
||||
MIGRATED = {}
|
||||
|
||||
|
||||
def run():
|
||||
"""Migrate translations."""
|
||||
to_migrate = {}
|
||||
|
||||
for integration in INTEGRATIONS_DIR.iterdir():
|
||||
strings_file = integration / "strings.json"
|
||||
if not strings_file.is_file():
|
||||
continue
|
||||
|
||||
if integration.name in MIGRATED:
|
||||
continue
|
||||
|
||||
strings = json.loads(strings_file.read_text())
|
||||
|
||||
if "title" in strings:
|
||||
from_key = f"component::{integration.name}::config::title"
|
||||
to_key = f"component::{integration.name}::title"
|
||||
to_migrate[from_key] = to_key
|
||||
|
||||
updates = []
|
||||
|
||||
lokalise = get_api()
|
||||
|
||||
print("Gathering IDs")
|
||||
|
||||
for from_key, to_key in to_migrate.items():
|
||||
key_data = lokalise.keys_list({"filter_keys": from_key})
|
||||
if len(key_data) != 1:
|
||||
print(
|
||||
f"Lookin up {from_key} key in Lokalise returns {len(key_data)} results, expected 1"
|
||||
)
|
||||
continue
|
||||
|
||||
updates.append({"key_id": key_data[0]["key_id"], "key_name": to_key})
|
||||
|
||||
pprint(updates)
|
||||
|
||||
print()
|
||||
print("Updating keys")
|
||||
pprint(lokalise.keys_bulk_update(updates).json())
|
|
@ -11,7 +11,9 @@ def get_base_arg_parser():
|
|||
"""Get a base argument parser."""
|
||||
parser = argparse.ArgumentParser(description="Home Assistant Translations")
|
||||
parser.add_argument(
|
||||
"action", type=str, choices=["download", "clean", "upload", "develop"]
|
||||
"action",
|
||||
type=str,
|
||||
choices=["download", "clean", "upload", "develop", "migrate"],
|
||||
)
|
||||
parser.add_argument("--debug", action="store_true", help="Enable log output")
|
||||
return parser
|
||||
|
|
Loading…
Reference in New Issue