Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
fix bug
  • Loading branch information
kargnas committed Aug 23, 2025
commit 4bfc7d712e183baeacc29fb6a594ad9a241d9771
6 changes: 5 additions & 1 deletion src/AI/JSONTranslationContextProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -273,8 +273,12 @@ protected function getLanguageDirectory(string $baseDirectory, string $locale):
* @param string $text The text to check
* @return bool True if the text is considered very long
*/
protected function isVeryLongText(string $text): bool
protected function isVeryLongText(?string $text): bool
{
if (is_null($text)) {
return false;
}

return substr_count($text, "\n") >= self::MAX_LINE_BREAKS;
}
}
45 changes: 26 additions & 19 deletions src/Console/TranslateJsonStructured.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ class TranslateJsonStructured extends Command
* Constants for magic numbers
*/
protected const MAX_LINE_BREAKS = 5;

protected const SHORT_STRING_LENGTH = 50;

protected const PRIORITY_RATIO = 0.7;

/**
Expand Down Expand Up @@ -289,13 +291,13 @@ public function translate(int $maxContextItems = 100): void
// Calculate relative path from source directory
$sourceBaseDir = $this->sourceDirectory.'/'.$this->sourceLocale;
$relativePath = str_replace($sourceBaseDir.'/', '', $file);

// Target file path (maintaining directory structure)
$outputFile = $this->getOutputDirectoryLocale($locale).'/'.$relativePath;

// Create output directory if it doesn't exist
$outputDir = dirname($outputFile);
if (!is_dir($outputDir)) {
if (! is_dir($outputDir)) {
mkdir($outputDir, 0755, true);
}

Expand Down Expand Up @@ -324,13 +326,14 @@ public function translate(int $maxContextItems = 100): void
if ($targetStringTransformer->isTranslated($key)) {
return false;
}

// Skip very long texts (5+ line breaks)
if ($this->isVeryLongText($value)) {
$this->line($this->colors['gray']." ⏩ Skipping very long text: {$key}".$this->colors['reset']);

return false;
}

return true;
})
->toArray();
Expand Down Expand Up @@ -445,7 +448,7 @@ protected function displayFileInfo(string $sourceFile, string $locale, string $o
// Remove source directory path to display relative path
$sourceBaseDir = $this->sourceDirectory.'/'.$this->sourceLocale;
$relativeFile = str_replace($sourceBaseDir.'/', '', $sourceFile);

$this->line("\n".$this->colors['purple_bg'].$this->colors['white'].$this->colors['bold'].' File Translation '.$this->colors['reset']);
$this->line($this->colors['yellow'].' File: '.
$this->colors['reset'].$this->colors['bold'].$relativeFile.
Expand Down Expand Up @@ -727,7 +730,7 @@ public function getExistingLocales(): array
$directories = array_diff(scandir($root), ['.', '..']);
// Filter only directories and exclude those starting with _
$directories = array_filter($directories, function ($directory) use ($root) {
return is_dir($root.'/'.$directory) && !str_starts_with($directory, '_');
return is_dir($root.'/'.$directory) && ! str_starts_with($directory, '_');
});

return collect($directories)->values()->toArray();
Expand All @@ -747,36 +750,36 @@ public function getOutputDirectoryLocale(string $locale): string
public function getStringFilePaths(string $locale): array
{
$root = $this->sourceDirectory.'/'.$locale;
if (!is_dir($root)) {

if (! is_dir($root)) {
return [];
}

return $this->getAllJsonFiles($root);
}

/**
* Recursively find all JSON files in a directory
*/
protected function getAllJsonFiles(string $directory): array
{
$files = [];
if (!is_dir($directory)) {

if (! is_dir($directory)) {
return [];
}

$iterator = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($directory, RecursiveDirectoryIterator::SKIP_DOTS),
RecursiveIteratorIterator::SELF_FIRST
);

foreach ($iterator as $file) {
if ($file->isFile() && $file->getExtension() === 'json') {
$files[] = $file->getPathname();
}
}

return $files;
}

Expand Down Expand Up @@ -806,12 +809,16 @@ protected function validateAndFilterLocales(array $specifiedLocales, array $avai

/**
* Check if text is very long (has too many line breaks)
*
* @param string $text The text to check
*
* @param string|null $text The text to check
* @return bool True if the text is considered very long
*/
protected function isVeryLongText(string $text): bool
protected function isVeryLongText(?string $text): bool
{
if (is_null($text)) {
return false;
}

return substr_count($text, "\n") >= self::MAX_LINE_BREAKS;
}
}