Skip to content

Commit 4b6d00f

Browse files
committed
feat: append custom localization files in the 'build_contenttools' command
1 parent 955e1e8 commit 4b6d00f

1 file changed

Lines changed: 40 additions & 0 deletions

File tree

apps/aklub/management/commands/build_contenttools.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import json
12
import os
23
import pathlib
34
import re
@@ -58,6 +59,10 @@ def handle(self, *args, **options):
5859
pathlib.Path(self.styles_src_dir) / 'contenttools' / 'src' /
5960
'sandbox' / 'styles'
6061
)
62+
self.cttools_src_trans_dir = (
63+
pathlib.Path(self.js_src_dir) / 'contenttools' / 'src' /
64+
'translations'
65+
)
6166

6267
# Target dirs
6368
self.cttools_target_dir = (
@@ -80,11 +85,15 @@ def handle(self, *args, **options):
8085
self.cttools_target_sandbox__build_dir = (
8186
self.cttools_target_dir / 'sandbox'
8287
)
88+
self.cttools_target_trans_dir = (
89+
self.cttools_target_dir / 'translations'
90+
)
8391

8492
self.__copy_coffee_scripts()
8593
self.__copy_sass_styles()
8694
self.__copy_fonts()
8795
self.__copy_gruntfile()
96+
self.__copy_localizations()
8897

8998
os.chdir(self.cttools_target_dir)
9099

@@ -233,6 +242,37 @@ def __copy_gruntfile(self):
233242
)
234243
shutil.copy(grunt_file, self.cttools_target_dir / grunt_file.name)
235244

245+
def __copy_localizations(self):
246+
"""Copy localization json files"""
247+
248+
trans_src_files = self.cttools_src_trans_dir.glob('*.json')
249+
250+
for trans_src_file in trans_src_files:
251+
252+
trans_target_file = (self.cttools_target_trans_dir /
253+
trans_src_file.name)
254+
255+
with open(trans_src_file, 'r') as src_read_trans_f, \
256+
open(trans_target_file, 'r+', encoding='utf-8') as \
257+
target_write_trans_f:
258+
259+
src_trans = json.load(src_read_trans_f)
260+
target_trans = json.load(target_write_trans_f)
261+
target_trans_keys = target_trans.keys()
262+
update_trans = {}
263+
264+
for trans_string in src_trans.keys():
265+
if trans_string not in target_trans_keys:
266+
update_trans[trans_string] = src_trans[trans_string]
267+
268+
if update_trans:
269+
target_trans.update(update_trans)
270+
271+
target_write_trans_f.seek(0)
272+
target_write_trans_f.truncate(0)
273+
json.dump(target_trans, target_write_trans_f, indent=4,
274+
ensure_ascii=False, sort_keys=True)
275+
236276
def __install(self, command, program, package='install'):
237277
"""Install/build package"""
238278

0 commit comments

Comments
 (0)