#!/usr/bin/env python import os import sys from django.core.management import call_command from django.apps import apps from pydwiki.settings import LANGUAGES, INSTALLED_APPS # 対応言語リスト取得 langs = [lang[0] for lang in LANGUAGES] def get_app_names(): """ アプリ名一覧を取得します。 配下に、アプリ名と同じディレクトリがあるものだけを抽出して返します。 Return: アプリ名一覧 """ filterd_app_names = list(filter(lambda name: os.path.isdir(name), INSTALLED_APPS)) return filterd_app_names def makemessages(): """ python manage.py makemessages -l '言語' と同様の処理を実施し、 locale/{言語}/LC_MESSAGES 配下に、po ファイルを生成します。 """ # ディレクトリ作成 app_names = get_app_names() for app_name in app_names: for lang in langs: locale_dir= f"{app_name}/locale/{lang}/LC_MESSAGES" os.makedirs(locale_dir, exist_ok=True) # po ファイル生成 # python manage.py makemessages -l ja call_command('makemessages', locale=langs) def compilemessages(): """ po ファイルより、mo ファイルを生成します。 """ # python manage.py compilemessages -l ja call_command('compilemessages', locale=langs) def print_usage(): print("") print("Usage) python trans.py [command]") print("command:") print(" makemessages: create .po file") print(" compilemessages: compile .po file (.po -> .mo)") print("") def main(): # コマンドと対応する関数のマップ command_map = { "makemessages": makemessages, "compilemessages": compilemessages, "help": print_usage, } # コマンドに対応する関数を実行する args = sys.argv command = args[1] if len(args) >= 2 else "help" command_func = command_map.get(command) if command_func: command_func() if __name__ == '__main__': main()