Skip to content

Conversation

kargnas
Copy link
Owner

@kargnas kargnas commented May 24, 2025

Summary

  • revert modifications to the existing translator commands
  • add TranslatePoStrings and TranslatePoFileCommand for gettext PO files
  • implement PoTranslationContextProvider
  • document PO command usage in README

Testing

  • No tests run as PHP execution isn't supported in Codex

@kargnas kargnas marked this pull request as ready for review June 6, 2025 18:13
@Copilot Copilot AI review requested due to automatic review settings June 6, 2025 18:13
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This pull request introduces support for translating gettext PO files by reverting modifications to the existing translator commands and adding new PO-specific translation functionality. Key changes include:

  • Implementation of PoLangTransformer to parse and write PO files.
  • Creation of the TranslatePoFileCommand for PO file translation using an AI provider.
  • Addition of PoTranslationContextProvider to supply translation context and updates to the README for PO command usage.

Reviewed Changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.

File Description
src/Transformers/PoLangTransformer.php Implements PO file parsing and transformation for translations.
src/Console/TranslatePoFileCommand.php Provides a dedicated CLI command for translating PO files using AI.
src/AI/PoTranslationContextProvider.php Supplies contextual data for PO file translations based on available source files.
README.md Updates documentation to inform users about PO file translation support.
Comments suppressed due to low confidence (1)

README.md:63

  • The documentation refers to a 'translate-po' command while the actual command signature in the code is 'ai-translator:translate-po-file'; update the README for consistency.
+ Supports gettext PO files via the `translate-po` command

Comment on lines +28 to +35
foreach ($lines as $line) {
if (preg_match('/^msgid\s+"(.*)"/', $line, $m)) {
$currentId = stripcslashes($m[1]);
} elseif ($currentId !== null && preg_match('/^msgstr\s+"(.*)"/', $line, $m)) {
$this->entries[$currentId] = stripcslashes($m[1]);
$currentId = null;
}
}
Copy link
Preview

Copilot AI Jun 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The current regex only captures single-line msgid entries, which may break for multi-line PO strings; consider enhancing the parser to support multi-line entries in accordance with the PO file specification.

Suggested change
foreach ($lines as $line) {
if (preg_match('/^msgid\s+"(.*)"/', $line, $m)) {
$currentId = stripcslashes($m[1]);
} elseif ($currentId !== null && preg_match('/^msgstr\s+"(.*)"/', $line, $m)) {
$this->entries[$currentId] = stripcslashes($m[1]);
$currentId = null;
}
}
$currentId = null;
$currentStr = null;
$isMsgid = false;
$isMsgstr = false;
foreach ($lines as $line) {
if (preg_match('/^msgid\s+"(.*)"/', $line, $m)) {
$currentId = stripcslashes($m[1]);
$isMsgid = true;
$isMsgstr = false;
} elseif ($isMsgid && preg_match('/^"(.*)"/', $line, $m)) {
$currentId .= stripcslashes($m[1]);
} elseif (preg_match('/^msgstr\s+"(.*)"/', $line, $m)) {
$currentStr = stripcslashes($m[1]);
$isMsgid = false;
$isMsgstr = true;
} elseif ($isMsgstr && preg_match('/^"(.*)"/', $line, $m)) {
$currentStr .= stripcslashes($m[1]);
} elseif ($currentId !== null && $currentStr !== null) {
$this->entries[$currentId] = $currentStr;
$currentId = null;
$currentStr = null;
$isMsgid = false;
$isMsgstr = false;
}
}
// Handle any remaining entry
if ($currentId !== null && $currentStr !== null) {
$this->entries[$currentId] = $currentStr;
}

Copilot uses AI. Check for mistakes.


public function handle()
{
$GLOBALS['instant_results'] = [];
Copy link
Preview

Copilot AI Jun 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using a global variable for storing instant results can lead to maintainability issues and unintended side effects; consider refactoring to use dependency injection or a dedicated storage mechanism.

Suggested change
$GLOBALS['instant_results'] = [];
$this->instantResults = [];

Copilot uses AI. Check for mistakes.

@kargnas kargnas closed this Jul 13, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant