Skip to content

Instantly share code, notes, and snippets.

@juliobitencourt
Created March 7, 2025 19:30
Show Gist options
  • Save juliobitencourt/668909e3967458a0e23954ae0f73ce27 to your computer and use it in GitHub Desktop.
Save juliobitencourt/668909e3967458a0e23954ae0f73ce27 to your computer and use it in GitHub Desktop.
Add vue-i18n syntax to Laravel Vue Starter Kit
<?php
function convertToVueI18n($filePath)
{
$safeList = [
'getInitials(auth.user?.name)',
'item.title',
'label',
'title',
'description',
'message',
'getInitials(user.name)',
'user.name',
'user.email',
'tooltip',
'quote.author',
'[email protected]',
'status',
'Laravel Starter Kit',
"Let's get started",
'Laracasts',
'Deploy now',
];
if (!file_exists($filePath)) {
die("O arquivo não foi encontrado.");
}
$content = file_get_contents($filePath);
$jsonPath = 'resources/js/lang/pt_br.json';
$translations = json_decode(file_get_contents($jsonPath), true) ?: [];
// Substitui atributos title e alt
$content = preg_replace_callback(
'/(?<!:)\b(title|alt|description|placeholder)="([^"]+)"/',
function ($matches) use (&$translations, $safeList) {
$text = trim($matches[2]);
if (($key = array_search($text, $safeList)) !== false) return $matches[0];
if (strpos($text, '$t(') === 0) return null;
if ($text === '') return null;
if (!array_key_exists($text, $translations)) {
$translations[$text] = "$text"; // Adiciona ao JSON de tradução
}
return ":{$matches[1]}=\"\$t('$text')\"";
},
$content
);
// Substitui textos dentro de componentes
$content = preg_replace_callback(
'/<([a-zA-Z0-9_-]+)(\s+[^>]*)?>(?:\s*{{\s*(.*?)\s*}}\s*|([^<>{}]+))<\/\1>/',
function ($matches) use (&$translations, $safeList) {
if (isset($matches[4])) {
$text = trim(preg_replace('/\s\s+/', ' ', $matches[4]));
if (($key = array_search($text, $safeList)) !== false) return $matches[0];
if (strpos($text, '$t(') === 0) return null;
if ($text === '') return null;
if (!array_key_exists($text, $translations)) {
$translations[$text] = "$text"; // Adiciona ao JSON de tradução
}
return "<{$matches[1]}{$matches[2]}>{{ \$t('$text') }}</{$matches[1]}>";
}
$text = trim(preg_replace('/\s\s+/', ' ', $matches[3]));
if (($key = array_search($text, $safeList)) !== false) return $matches[0];
if (strpos($text, '$t(') === 0) return null;
if ($text === '') return null;
if (!array_key_exists($text, $translations)) {
$translations[$text] = "$text"; // Adiciona ao JSON de tradução
}
return "<{$matches[1]}{$matches[2]}>{{ \$t($text) }}</{$matches[1]}>";
},
$content
);
// Escreve de volta no arquivo
file_put_contents($filePath, $content);
// Salva o novo conteúdo no arquivo Vue
file_put_contents($filePath, $content);
// Salva o JSON com os textos extraídos
file_put_contents($jsonPath, json_encode($translations, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE));
}
function scanVueFiles(string $directory, array &$files = []): array {
$items = scandir($directory);
foreach ($items as $item) {
if ($item === '.' || $item === '..') {
continue;
}
$path = $directory . DIRECTORY_SEPARATOR . $item;
if (is_dir($path)) {
// Se for um diretório, chama a função recursivamente
scanVueFiles($path, $files);
} elseif (pathinfo($path, PATHINFO_EXTENSION) === 'vue') {
// Se for um arquivo .vue, adiciona à lista
$files[] = $path;
}
}
return $files;
}
$directory = __DIR__ . '/resources/js'; // Substitua pelo diretório desejado
$vueFiles = scanVueFiles($directory);
foreach ($vueFiles as $vueFile) {
convertToVueI18n($vueFile);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment