Skip to content

Commit

Permalink
Support compilation
Browse files Browse the repository at this point in the history
The PO folder must contain one sub-folder for each language, each sub-folder contains PO files in that language.
  • Loading branch information
PhuNH committed Aug 27, 2023
1 parent b1b23ff commit 07058f5
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
7 changes: 7 additions & 0 deletions hugo_gettext/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from .extraction import extract
from .generation import generate
from .compilation import compile_po


def main():
Expand All @@ -27,6 +28,12 @@ def main():
generate_cmd.add_argument('-k', '--keep-locale', action='store_true', help='do not delete locale folder')
generate_cmd.set_defaults(func=generate)

compile_po_cmd = subparsers.add_parser('compile', help='compile translated messages to binary format',
formatter_class=RawTextHelpFormatter)
compile_po_cmd.add_argument('dir', help='path of the directory containing subdirectories with PO files inside\n'
'in the form of {dir}/{lang}/*.po')
compile_po_cmd.set_defaults(func=compile_po)

args = parser.parse_args()
level = logging.WARNING if args.quiet else logging.INFO
logging.basicConfig(format='%(levelname)s: %(message)s', level=level)
Expand Down
27 changes: 27 additions & 0 deletions hugo_gettext/compilation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# SPDX-FileCopyrightText: 2023 Phu Hung Nguyen <[email protected]>
# SPDX-License-Identifier: LGPL-2.1-or-later

import logging
import os
import subprocess


def compile_po(args):
"""
Compile translated messages to binary format stored in 'locale/{lang}/LC_MESSAGES' directory
:param args: arguments passed in command line, containing
- dir: path of the directory containing subdirectories with PO files inside, in the form of {lang}/*.po
:return: None
"""
po_dir = args.dir
for lang in os.listdir(po_dir):
target_path = f'locale/{lang}/LC_MESSAGES'
os.makedirs(target_path, exist_ok=True)
src_path = f'{po_dir}/{lang}'

for po in os.listdir(src_path):
po_path = f'{src_path}/{po}'
mo_path = f'{target_path}/{po[:-2]}mo'
command = f'msgfmt {po_path} -o {mo_path}'
subprocess.run(command, shell=True, check=True)
logging.info(f'Compiled {lang}')
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

[tool.poetry]
name = "hugo-gettext"
version = "0.2.0"
version = "0.3.0"
description = "I18n with gettext for Hugo"
authors = ["Phu Hung Nguyen <[email protected]>"]
license = "GPL-3.0-or-later"
Expand Down

0 comments on commit 07058f5

Please sign in to comment.