%PDF- %PDF-
| Direktori : /usr/share/l.v.e-manager/utils/ |
| Current File : //usr/share/l.v.e-manager/utils/generate_locale_files_for_plesk.py |
#!/opt/cloudlinux/venv/bin/python3 -bb
# coding=utf-8
#
# Copyright © Cloud Linux GmbH & Cloud Linux Software, Inc 2010-2019 All Rights Reserved
#
# Licensed under CLOUD LINUX LICENSE AGREEMENT
# http://cloudlinux.com/docs/LICENSE.TXT
from __future__ import print_function
from __future__ import division
from __future__ import absolute_import
import sys
import os
import cldetectlib as detect
import json
SOURCE_PATH = '/usr/share/l.v.e-manager/'
LOCALES_SOURCE_PATH = SOURCE_PATH + 'commons/spa-resources/i18n/'
PLESK_LOCALES_DIR = SOURCE_PATH + 'plesk/plib/modules/plesk-lvemanager/resources/locales/'
DEFAULT_SOURCE_LANG_JSON = LOCALES_SOURCE_PATH + 'en-en.json'
# Phrases below wee need only to translate of the Custom Buttons
# in other cases SPA will be translated using JSON-s from i18n/
ORIGINAL_PHRASES = ['CloudLinux Manager', 'PHP Selector', 'Resource Usage', 'X-Ray', 'AccelerateWP']
class Locales(object):
"""
Generate locales for Plesk custom buttons
"""
@staticmethod
def add_locale_file(source_lang_json, out_file, template):
"""
Add single locale file for Plesk
:param source_lang_json: json file from i18n/
:param out_file: generated file
:param template: template used to generate file
:return: None
"""
content_array = []
if not os.path.isfile(source_lang_json):
source_lang_json = DEFAULT_SOURCE_LANG_JSON # use english lang if no translation
with open(source_lang_json, 'r') as f:
json_content = json.loads(f.read())
for phrase in ORIGINAL_PHRASES:
content_array.append(' \'{}\' => \'{}\''
.format(phrase, json_content.get(phrase, phrase)))
with open(out_file, 'w') as f:
translations = ',\n'.join(content_array)
f.write(template.format(translations))
def generate_locales(self, template):
"""
Generate required locales for Plesk.
:param template: string: Template for rendering of new localization files.
:return: None
"""
if not os.path.isdir(PLESK_LOCALES_DIR):
os.makedirs(PLESK_LOCALES_DIR)
for locale in os.listdir(LOCALES_SOURCE_PATH):
filename = '{}/{}'.format(LOCALES_SOURCE_PATH, locale)
if os.path.isfile(filename) and filename.endswith('.json'):
locale = locale.split('.')[0]
source_lang_json = LOCALES_SOURCE_PATH + locale + '.json'
plesk_locale = "{}-{}".format(locale.split('-')[0], locale.split('-')[-1].upper())
out_file = PLESK_LOCALES_DIR + plesk_locale + '.php'
self.add_locale_file(source_lang_json, out_file, template)
def main(self):
if detect.is_plesk():
template = '<?php\n $messages = array(\n{}\n);\n'
self.generate_locales(template)
else:
sys.exit('This script should be run only on Plesk')
if __name__ == '__main__':
Locales().main()