Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add messages.pot update script to pre-commit. #8900

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,18 @@ repos:
- stylelint-declaration-strict-value@1.9.2
- postcss-less@3.1.4
args: [--fix]

- repo: local
hooks:
- id: generate-pot
name: Generate POT
description: This hook generates the .pot file for internationalization.
entry: python ./scripts/i18n-messages extract
types_or: [python, html]
language: python
pass_filenames: false
# Dependencies must be manually updated to match versions in requirements.txt
additional_dependencies:
- Babel==2.12.1
rebecca-shoptaw marked this conversation as resolved.
Show resolved Hide resolved
- multipart==0.2.4
- "git+https://github.com/webpy/webpy.git@d3649322b85777b291ac2b7b3699fb6fc839e382"
53 changes: 44 additions & 9 deletions openlibrary/i18n/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import os
import shutil
import sys
import subprocess
from collections.abc import Iterator
from io import BytesIO
from pathlib import Path

import web

Expand Down Expand Up @@ -33,6 +35,23 @@ def warning_color_fn(text: str) -> str:
return '\033[93m' + text + '\033[0m'


def get_untracked_files(dirs: list[str], extensions: tuple[str, str] | str) -> set:
"""Returns a set of all currently untracked files with specified extension(s)."""
untracked_files = {
Path(line)
for dir in dirs
for line in subprocess.run(
['git', 'ls-files', '--others', '--exclude-standard', dir],
stdout=subprocess.PIPE,
text=True,
check=True,
).stdout.split('\n')
if line.endswith(extensions)
}

return untracked_files


def _compile_translation(po, mo):
try:
catalog = read_po(open(po, 'rb'))
Expand Down Expand Up @@ -139,31 +158,47 @@ def extract_templetor(fileobj, keywords, comment_tags, options):
return extract_python(f, keywords, comment_tags, options)


def extract_messages(dirs: list[str]):
def extract_messages(dirs: list[str], verbose: bool, forced: bool):
catalog = Catalog(project='Open Library', copyright_holder='Internet Archive')
METHODS = [("**.py", "python"), ("**.html", "openlibrary.i18n:extract_templetor")]
COMMENT_TAGS = ["NOTE:"]

untracked_files = get_untracked_files(dirs, ('.py', '.html'))
template = read_po((Path(root) / 'messages.pot').open('rb'))
msg_set = {msg.id for msg in template if msg.id != ''}
new_set = set()

for d in dirs:
extracted = extract_from_dir(
d, METHODS, comment_tags=COMMENT_TAGS, strip_comment_tags=True
)

counts: dict[str, int] = {}
for filename, lineno, message, comments, context in extracted:
file_path = Path(d) / filename
if file_path in untracked_files:
continue
new_set.add(message)
counts[filename] = counts.get(filename, 0) + 1
catalog.add(message, None, [(filename, lineno)], auto_comments=comments)

for filename, count in counts.items():
path = filename if d == filename else os.path.join(d, filename)
print(f"{count}\t{path}", file=sys.stderr)
if verbose:
for filename, count in counts.items():
path = filename if d == filename else os.path.join(d, filename)
print(f"{count}\t{path}", file=sys.stderr)

path = os.path.join(root, 'messages.pot')
f = open(path, 'wb')
write_po(f, catalog)
f.close()
has_changed = msg_set != new_set
if has_changed or forced:
path = os.path.join(root, 'messages.pot')
f = open(path, 'wb')
write_po(f, catalog, include_lineno=False)
f.close()

print('wrote template to', path)
print('Updated strings written to', path)
else:
print(
'No modified strings discovered. To force a template update, re-run with --force-write.'
)


def compile_translations(locales: list[str]):
Expand Down
Loading
Loading