mirror of
https://github.com/LawnchairLauncher/lawnchair.git
synced 2026-02-27 15:26:58 +00:00
Import smartspace translations
This commit is contained in:
59
tools/translations_importer/apkutils.py
Normal file
59
tools/translations_importer/apkutils.py
Normal file
@@ -0,0 +1,59 @@
|
||||
from bs4 import BeautifulSoup
|
||||
import os
|
||||
import subprocess
|
||||
import re
|
||||
|
||||
string_def_pattern = '^ resource 0x[0123456789abcdef]{8} string\/(.+)$'
|
||||
locale_def_pattern = '^ \\(((?:[a-z]|[A-Z]|-|\\+)*)\\)'
|
||||
|
||||
def find_aapt2():
|
||||
android_home = os.environ['ANDROID_HOME']
|
||||
build_tools = f'{android_home}/build-tools'
|
||||
latest = sorted(os.listdir(build_tools))[-1]
|
||||
if latest is None:
|
||||
return
|
||||
aapt2 = f'{build_tools}/{latest}/aapt2'
|
||||
return aapt2
|
||||
|
||||
aapt2_path = find_aapt2()
|
||||
|
||||
def execute_aapt2(args):
|
||||
output = subprocess.run([aapt2_path] + args, stdout=subprocess.PIPE, text=True)
|
||||
return output.stdout.split('\n')
|
||||
|
||||
def extract_strings(apk_path):
|
||||
lines = execute_aapt2(['dump', 'resources', apk_path])
|
||||
|
||||
all_map = {}
|
||||
|
||||
current_string = None
|
||||
current_map = {}
|
||||
for line in lines:
|
||||
if not line.startswith(' ('):
|
||||
if current_string is not None:
|
||||
if len(current_map) > 0:
|
||||
all_map[current_string] = current_map
|
||||
current_string = None
|
||||
current_map = {}
|
||||
if line.startswith(' resource'):
|
||||
matches = re.findall(string_def_pattern, line)
|
||||
if len(matches) == 0:
|
||||
continue
|
||||
string_name = matches[0]
|
||||
current_string = string_name
|
||||
current_map = {}
|
||||
continue
|
||||
if current_string is not None:
|
||||
locale = re.findall(locale_def_pattern, line)[0]
|
||||
line = line[len(locale)+9:]
|
||||
if not line.startswith('"'):
|
||||
continue
|
||||
string = line[1:-1]
|
||||
current_map[locale] = string
|
||||
|
||||
if current_string is not None:
|
||||
all_map[current_string] = current_map
|
||||
current_string = None
|
||||
current_map = {}
|
||||
|
||||
return all_map
|
||||
49
tools/translations_importer/main.py
Normal file
49
tools/translations_importer/main.py
Normal file
@@ -0,0 +1,49 @@
|
||||
from apkutils import extract_strings
|
||||
from bs4 import BeautifulSoup, NavigableString
|
||||
from pathlib import Path
|
||||
import shutil
|
||||
import os
|
||||
import argparse
|
||||
|
||||
my_parser = argparse.ArgumentParser(description='List the content of a folder')
|
||||
my_parser.add_argument('path', type=str, help='Path of the APK to import from')
|
||||
my_parser.add_argument('string_name', type=str, help='Name of the string to import')
|
||||
|
||||
args = my_parser.parse_args()
|
||||
|
||||
apk_path = args.path
|
||||
string_name = args.string_name
|
||||
strings = extract_strings(apk_path)
|
||||
|
||||
p = Path(__file__)
|
||||
project_root = p.parent.parent.parent
|
||||
res_root = project_root / 'lawnchair' / 'res'
|
||||
|
||||
def add_to_xml(locale, string_name, string):
|
||||
folder_name = 'values' if locale == '' else f'values-{locale}'
|
||||
print(f'{folder_name}: {string}')
|
||||
file_path = res_root / folder_name / 'strings.xml'
|
||||
if not file_path.exists():
|
||||
os.makedirs(file_path.parent, exist_ok=True)
|
||||
shutil.copy(p.parent / 'template.xml', file_path)
|
||||
with open(file_path, 'r') as f:
|
||||
data = f.read()
|
||||
bs = BeautifulSoup(data, "xml")
|
||||
existing = bs.find('string', {'name': string_name})
|
||||
if existing is not None:
|
||||
return
|
||||
new_tag = bs.new_tag('string')
|
||||
new_tag['name'] = string_name
|
||||
new_tag.insert(0, NavigableString(string))
|
||||
tag_string = str(new_tag)
|
||||
|
||||
lines = data.split('\n')
|
||||
insert_at = lines.index('</resources>')
|
||||
lines.insert(insert_at, f' {tag_string}')
|
||||
result = '\n'.join(lines)
|
||||
with open(file_path, 'w') as f:
|
||||
f.write(result)
|
||||
|
||||
locales = strings[string_name]
|
||||
for locale, string in locales.items():
|
||||
add_to_xml(locale, string_name, string)
|
||||
18
tools/translations_importer/template.xml
Normal file
18
tools/translations_importer/template.xml
Normal file
@@ -0,0 +1,18 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<!--
|
||||
~ Copyright 2021, Lawnchair
|
||||
~
|
||||
~ Licensed under the Apache License, Version 2.0 (the "License");
|
||||
~ you may not use this file except in compliance with the License.
|
||||
~ You may obtain a copy of the License at
|
||||
~
|
||||
~ http://www.apache.org/licenses/LICENSE-2.0
|
||||
~
|
||||
~ Unless required by applicable law or agreed to in writing, software
|
||||
~ distributed under the License is distributed on an "AS IS" BASIS,
|
||||
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
~ See the License for the specific language governing permissions and
|
||||
~ limitations under the License.
|
||||
-->
|
||||
<resources xmlns:tools="http://schemas.android.com/tools" xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
|
||||
</resources>
|
||||
Reference in New Issue
Block a user