chore: automatically translate empty strings (monicahq/chandler#498)
This commit is contained in:
@@ -3,9 +3,14 @@
|
||||
namespace App\Console\Commands;
|
||||
|
||||
use Illuminate\Console\Command;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Stichoza\GoogleTranslate\GoogleTranslate;
|
||||
use Symfony\Component\Finder\Finder;
|
||||
|
||||
class MonicaLocalize extends Command
|
||||
{
|
||||
private GoogleTranslate $googleTranslate;
|
||||
|
||||
/**
|
||||
* The name and signature of the console command.
|
||||
*
|
||||
@@ -28,5 +33,49 @@ class MonicaLocalize extends Command
|
||||
$locales = config('localizer.supported-locales');
|
||||
array_shift($locales);
|
||||
$this->call('localize', ['lang' => implode(',', $locales)]);
|
||||
|
||||
$this->loadTranslations($locales);
|
||||
}
|
||||
|
||||
/**
|
||||
* Heavily inspired by https://stevensteel.com/blog/automatically-find-translate-and-save-missing-translation-keys
|
||||
*/
|
||||
private function loadTranslations(array $locales): void
|
||||
{
|
||||
$path = lang_path();
|
||||
$finder = new Finder();
|
||||
$finder->in($path)->name(['*.json'])->files();
|
||||
$this->googleTranslate = new GoogleTranslate();
|
||||
|
||||
foreach ($finder as $file) {
|
||||
$locale = $file->getFilenameWithoutExtension();
|
||||
|
||||
if (! in_array($locale, $locales)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$this->info('loading locale: '.$locale);
|
||||
$jsonString = $file->getContents();
|
||||
$strings = json_decode($jsonString, true);
|
||||
|
||||
$this->translateStrings($locale, $strings);
|
||||
}
|
||||
}
|
||||
|
||||
private function translateStrings(string $locale, array $strings)
|
||||
{
|
||||
foreach ($strings as $index => $value) {
|
||||
if ($value === '') {
|
||||
$this->googleTranslate->setTarget($locale);
|
||||
$translated = $this->googleTranslate->translate($index);
|
||||
$this->info('translating: `'.$index.'` to `'.$translated.'`');
|
||||
|
||||
// we store the translated string in the array
|
||||
$strings[$index] = $translated;
|
||||
}
|
||||
}
|
||||
|
||||
// now we need to save the array back to the file
|
||||
Storage::disk('lang')->put($locale.'.json', json_encode($strings, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -37,6 +37,7 @@
|
||||
"socialiteproviders/linkedin": "^4.2",
|
||||
"socialiteproviders/microsoft-azure": "^5.1",
|
||||
"socialiteproviders/twitter": "^4.1",
|
||||
"stichoza/google-translate-php": "^5.1",
|
||||
"tightenco/ziggy": "1.5.2",
|
||||
"uploadcare/uploadcare-php": "^3.2"
|
||||
},
|
||||
|
||||
Generated
+87
-10
@@ -4,7 +4,7 @@
|
||||
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
|
||||
"This file is @generated automatically"
|
||||
],
|
||||
"content-hash": "0d7d0a04240307bd55773da8d655a44d",
|
||||
"content-hash": "23188496d30f378c09b29622bbde74a5",
|
||||
"packages": [
|
||||
{
|
||||
"name": "asbiin/laravel-webauthn",
|
||||
@@ -6212,16 +6212,16 @@
|
||||
},
|
||||
{
|
||||
"name": "sentry/sentry-laravel",
|
||||
"version": "3.3.3",
|
||||
"version": "3.4.0",
|
||||
"source": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/getsentry/sentry-laravel.git",
|
||||
"reference": "8a72bb67e208687fbb2ebc8ca1bd229c26f28dd1"
|
||||
"reference": "50ec5eae5d09d182d2d048981d0b45c24579a7f7"
|
||||
},
|
||||
"dist": {
|
||||
"type": "zip",
|
||||
"url": "https://api.github.com/repos/getsentry/sentry-laravel/zipball/8a72bb67e208687fbb2ebc8ca1bd229c26f28dd1",
|
||||
"reference": "8a72bb67e208687fbb2ebc8ca1bd229c26f28dd1",
|
||||
"url": "https://api.github.com/repos/getsentry/sentry-laravel/zipball/50ec5eae5d09d182d2d048981d0b45c24579a7f7",
|
||||
"reference": "50ec5eae5d09d182d2d048981d0b45c24579a7f7",
|
||||
"shasum": ""
|
||||
},
|
||||
"require": {
|
||||
@@ -6232,9 +6232,6 @@
|
||||
"sentry/sentry": "^3.16",
|
||||
"symfony/psr-http-message-bridge": "^1.0 | ^2.0"
|
||||
},
|
||||
"conflict": {
|
||||
"laravel/lumen-framework": "*"
|
||||
},
|
||||
"require-dev": {
|
||||
"friendsofphp/php-cs-fixer": "^3.11",
|
||||
"laravel/framework": "^6.0 | ^7.0 | ^8.0 | ^9.0 | ^10.0",
|
||||
@@ -6289,7 +6286,7 @@
|
||||
],
|
||||
"support": {
|
||||
"issues": "https://github.com/getsentry/sentry-laravel/issues",
|
||||
"source": "https://github.com/getsentry/sentry-laravel/tree/3.3.3"
|
||||
"source": "https://github.com/getsentry/sentry-laravel/tree/3.4.0"
|
||||
},
|
||||
"funding": [
|
||||
{
|
||||
@@ -6301,7 +6298,7 @@
|
||||
"type": "custom"
|
||||
}
|
||||
],
|
||||
"time": "2023-04-18T09:01:41+00:00"
|
||||
"time": "2023-05-08T14:15:56+00:00"
|
||||
},
|
||||
{
|
||||
"name": "shalvah/clara",
|
||||
@@ -7004,6 +7001,86 @@
|
||||
],
|
||||
"time": "2023-02-13T17:21:24+00:00"
|
||||
},
|
||||
{
|
||||
"name": "stichoza/google-translate-php",
|
||||
"version": "v5.1.1",
|
||||
"source": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/Stichoza/google-translate-php.git",
|
||||
"reference": "7f528ab7e8dad78d0cfa261e4a4cac1b187f63df"
|
||||
},
|
||||
"dist": {
|
||||
"type": "zip",
|
||||
"url": "https://api.github.com/repos/Stichoza/google-translate-php/zipball/7f528ab7e8dad78d0cfa261e4a4cac1b187f63df",
|
||||
"reference": "7f528ab7e8dad78d0cfa261e4a4cac1b187f63df",
|
||||
"shasum": ""
|
||||
},
|
||||
"require": {
|
||||
"ext-json": "*",
|
||||
"ext-mbstring": "*",
|
||||
"guzzlehttp/guzzle": "^7.0",
|
||||
"php": "^8.0"
|
||||
},
|
||||
"require-dev": {
|
||||
"phpunit/phpunit": "^9.5.10"
|
||||
},
|
||||
"type": "library",
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"Stichoza\\GoogleTranslate\\": "src/"
|
||||
}
|
||||
},
|
||||
"notification-url": "https://packagist.org/downloads/",
|
||||
"license": [
|
||||
"MIT"
|
||||
],
|
||||
"authors": [
|
||||
{
|
||||
"name": "Levan Velijanashvili",
|
||||
"email": "me@stichoza.com"
|
||||
}
|
||||
],
|
||||
"description": "Free Google Translate API PHP Package",
|
||||
"homepage": "https://github.com/Stichoza/google-translate-php",
|
||||
"keywords": [
|
||||
"google",
|
||||
"php",
|
||||
"translate",
|
||||
"translating",
|
||||
"translator"
|
||||
],
|
||||
"support": {
|
||||
"issues": "https://github.com/Stichoza/google-translate-php/issues",
|
||||
"source": "https://github.com/Stichoza/google-translate-php/tree/v5.1.1"
|
||||
},
|
||||
"funding": [
|
||||
{
|
||||
"url": "https://btc.com/bc1qc25j4x7yahghm8nnn6lypnw59nptylsw32nkfl",
|
||||
"type": "custom"
|
||||
},
|
||||
{
|
||||
"url": "https://www.paypal.me/stichoza",
|
||||
"type": "custom"
|
||||
},
|
||||
{
|
||||
"url": "https://ko-fi.com/stichoza",
|
||||
"type": "ko_fi"
|
||||
},
|
||||
{
|
||||
"url": "https://liberapay.com/stichoza",
|
||||
"type": "liberapay"
|
||||
},
|
||||
{
|
||||
"url": "https://opencollective.com/stichoza",
|
||||
"type": "open_collective"
|
||||
},
|
||||
{
|
||||
"url": "https://www.patreon.com/stichoza",
|
||||
"type": "patreon"
|
||||
}
|
||||
],
|
||||
"time": "2022-12-01T16:51:22+00:00"
|
||||
},
|
||||
{
|
||||
"name": "symfony/console",
|
||||
"version": "v6.2.10",
|
||||
|
||||
@@ -35,6 +35,11 @@ return [
|
||||
'root' => storage_path('app'),
|
||||
],
|
||||
|
||||
'lang' => [
|
||||
'driver' => 'local',
|
||||
'root' => lang_path(),
|
||||
],
|
||||
|
||||
'public' => [
|
||||
'driver' => 'local',
|
||||
'root' => storage_path('app/public'),
|
||||
|
||||
Reference in New Issue
Block a user