Skip to content
Open
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
24 changes: 23 additions & 1 deletion invenio_cli/commands/translations.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

"""Invenio module to ease the creation and management of applications."""

from pathlib import Path

from ..commands import Commands
from ..helpers.cli_config import CLIConfig
from ..helpers.filesystem import force_symlink
Expand Down Expand Up @@ -49,9 +51,29 @@ def extract(
cmd=cmd,
env={"PIPENV_VERBOSITY": "-1"},
message=f"Extracting i18n messages from {input_dirs}...",
)
),
FunctionStep(
func=self._normalize_paths,
args={
"output_file": output_file,
"project_path": self.project_path,
},
message="Normalizing paths in extracted messages...",
),
]

def _normalize_paths(self, output_file: str, project_path: Path):
"""Normalize absolute paths to relative paths in the extracted .pot file."""
output_path = Path(output_file)
if not output_path.exists():
return

content = output_path.read_text()
# Replace absolute paths with relative paths
if project_path and str(project_path) in content:
normalized_content = content.replace(str(project_path) + "/", "")
output_path.write_text(normalized_content)

def init(self, output_dir, input_file, locale):
"""Initialize a new language catalog."""
pkg_man = self.cli_config.python_package_manager
Expand Down
61 changes: 61 additions & 0 deletions tests/commands/test_translations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# -*- coding: utf-8 -*-
#
# Copyright (C) 2026 CERN.
# Copyright (C) 2026 CESNET.
#
# Invenio-Cli is free software; you can redistribute it and/or modify it
# under the terms of the MIT License; see LICENSE file for more details.

"""Tests for translations commands."""

from pathlib import Path

import pytest

from invenio_cli.commands.translations import TranslationsCommands


@pytest.fixture
def temp_pot_file(tmp_path):
"""Create a temporary .pot file with absolute paths."""
pot_file = tmp_path / "messages.pot"
pot_file.write_text(
"# Translations template\n"
"#, fuzzy\n"
'msgid ""\n'
'msgstr ""\n'
f"#: {tmp_path}/test.py:1\n"
'msgid "test message"\n'
'msgstr ""\n'
)
return pot_file


def test_normalize_paths(temp_pot_file, tmp_path):
"""Test that absolute paths are normalized to relative paths."""
commands = TranslationsCommands(cli_config=None, project_path=tmp_path)

commands._normalize_paths(str(temp_pot_file), tmp_path)
content = temp_pot_file.read_text()

assert str(tmp_path) not in content
assert "test.py:1" in content
assert "#: test.py:1" in content


def test_normalize_paths_no_project_path(temp_pot_file, tmp_path):
"""Test that normalization works when project_path is None."""
commands = TranslationsCommands(cli_config=None, project_path=None)
original_content = temp_pot_file.read_text()
commands._normalize_paths(str(temp_pot_file), None)

normalized_content = temp_pot_file.read_text()
assert original_content == normalized_content


def test_normalize_paths_file_not_exists(tmp_path):
"""Test that normalization handles non-existent files gracefully."""
commands = TranslationsCommands(cli_config=None, project_path=tmp_path)

# Run the normalization on a non-existent file - should not crash
commands._normalize_paths(str(tmp_path / "nonexistent.pot"), tmp_path)