-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #32 from Laravel-Lang/1.x
Added command to translate keys
- Loading branch information
Showing
22 changed files
with
490 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace LaravelLang\StatusGenerator\Commands; | ||
|
||
use LaravelLang\StatusGenerator\Constants\Command as CommandName; | ||
use LaravelLang\StatusGenerator\Processors\Translate\Translate as TranslateProcessor; | ||
|
||
class Translate extends Command | ||
{ | ||
protected array|string $processor = [ | ||
TranslateProcessor::class, | ||
]; | ||
|
||
protected function configure(): Command | ||
{ | ||
return parent::configure() | ||
->setName(CommandName::TRANSLATE()) | ||
->setDescription('Translation of untranslated keys'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace LaravelLang\StatusGenerator\Helpers; | ||
|
||
use DragonCode\Support\Facades\Helpers\Arr; | ||
use LaravelLang\Publisher\Constants\Locales; | ||
use LaravelLang\StatusGenerator\Objects\Translatable; | ||
use Stichoza\GoogleTranslate\GoogleTranslate; | ||
|
||
class GoogleLocale | ||
{ | ||
protected static Locales $default = Locales::ENGLISH; | ||
|
||
public static function translate(string $value, string $locale): string | ||
{ | ||
if ($resolved = self::locale($locale)) { | ||
return self::get($value, $resolved, self::$default->value); | ||
} | ||
|
||
return $value; | ||
} | ||
|
||
protected static function get(string $value, string $targetLocale, string $sourceLocale): string | ||
{ | ||
$object = self::prepare($value); | ||
|
||
$translated = GoogleTranslate::trans($object->value, $targetLocale, $sourceLocale); | ||
|
||
return $object->compile($translated); | ||
} | ||
|
||
protected static function prepare(string $value): Translatable | ||
{ | ||
return Translatable::make(compact('value')); | ||
} | ||
|
||
protected static function locale(string $locale): ?string | ||
{ | ||
return Arr::get(self::locales(), $locale); | ||
} | ||
|
||
protected static function locales(): array | ||
{ | ||
return [ | ||
Locales::AFRIKAANS->value => 'af', | ||
Locales::ALBANIAN->value => 'sq', | ||
Locales::ARABIC->value => 'ar', | ||
Locales::ARMENIAN->value => 'hy', | ||
Locales::AZERBAIJANI->value => 'az', | ||
Locales::BASQUE->value => 'eu', | ||
Locales::BELARUSIAN->value => 'be', | ||
Locales::BENGALI->value => 'bn', | ||
Locales::BOSNIAN->value => 'bs', | ||
Locales::BULGARIAN->value => 'bg', | ||
Locales::CATALAN->value => 'ca', | ||
Locales::CHINESE->value => 'zh-CN', | ||
Locales::CHINESE_T->value => 'zh-TW', | ||
Locales::CROATIAN->value => 'hr', | ||
Locales::CZECH->value => 'cs', | ||
Locales::DANISH->value => 'da', | ||
Locales::DUTCH->value => 'nl', | ||
Locales::ESTONIAN->value => 'et', | ||
Locales::PILIPINO->value => 'fil', | ||
Locales::FINNISH->value => 'fi', | ||
Locales::FRENCH->value => 'fr', | ||
Locales::GALICIAN->value => 'gl', | ||
Locales::GEORGIAN->value => 'ka', | ||
Locales::GERMAN->value => 'de', | ||
Locales::GREEK->value => 'el', | ||
Locales::GUJARATI->value => 'gu', | ||
Locales::HEBREW->value => 'he', | ||
Locales::HINDI->value => 'hi', | ||
Locales::HUNGARIAN->value => 'hu', | ||
Locales::ICELANDIC->value => 'is', | ||
Locales::INDONESIAN->value => 'id', | ||
Locales::ITALIAN->value => 'it', | ||
Locales::JAPANESE->value => 'ja', | ||
Locales::KANNADA->value => 'kn', | ||
Locales::KAZAKH->value => 'kk', | ||
Locales::CENTRAL_KHMER->value => 'km', | ||
Locales::KOREAN->value => 'ko', | ||
Locales::LATVIAN->value => 'lv', | ||
Locales::LITHUANIAN->value => 'lt', | ||
Locales::MACEDONIAN->value => 'mk', | ||
Locales::MALAY->value => 'ms', | ||
Locales::MARATHI->value => 'mr', | ||
Locales::MONGOLIAN->value => 'mn', | ||
Locales::NEPALI->value => 'ne', | ||
Locales::PASHTO->value => 'ps', | ||
Locales::PERSIAN->value => 'fa', | ||
Locales::POLISH->value => 'pl', | ||
Locales::PORTUGUESE->value => 'pt', | ||
Locales::PORTUGUESE_BRAZIL->value => 'pt', | ||
Locales::ROMANIAN->value => 'ro', | ||
Locales::RUSSIAN->value => 'ru', | ||
Locales::SERBIAN_CYRILLIC->value => 'sr', | ||
Locales::SINHALA->value => 'si', | ||
Locales::SLOVAK->value => 'sk', | ||
Locales::SLOVENIAN->value => 'sl', | ||
Locales::SPANISH->value => 'es', | ||
Locales::SWAHILI->value => 'sw', | ||
Locales::SWEDISH->value => 'sv', | ||
Locales::TAGALOG->value => 'tl', | ||
Locales::TAJIK->value => 'tg', | ||
Locales::THAI->value => 'th', | ||
Locales::TURKISH->value => 'tr', | ||
Locales::TURKMEN->value => 'tk', | ||
Locales::UKRAINIAN->value => 'uk', | ||
Locales::URDU->value => 'ur', | ||
Locales::UIGHUR->value => 'ug', | ||
Locales::UZBEK_CYRILLIC->value => 'uz', | ||
Locales::VIETNAMESE->value => 'vi', | ||
Locales::WELSH->value => 'cy', | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace LaravelLang\StatusGenerator\Objects; | ||
|
||
use DragonCode\SimpleDataTransferObject\DataTransferObject; | ||
use DragonCode\Support\Facades\Helpers\Str; | ||
|
||
class Translatable extends DataTransferObject | ||
{ | ||
public ?string $value = null; | ||
|
||
public array $replaces = []; | ||
|
||
public function compile(string $translated): string | ||
{ | ||
return Str::replace($translated, array_values($this->replaces), array_keys($this->replaces)); | ||
} | ||
|
||
protected function castValue(string $value): string | ||
{ | ||
$this->extractReplaces($value); | ||
|
||
return $this->replaceValue($value); | ||
} | ||
|
||
protected function extractReplaces(string $value): void | ||
{ | ||
Str::of($value) | ||
->trim() | ||
->trim('.?!') | ||
->explode(' ') | ||
->map(function (string $item) { | ||
if (Str::startsWith($item, ':')) { | ||
return $this->numerify($item); | ||
} | ||
|
||
return $item; | ||
}); | ||
} | ||
|
||
protected function replaceValue(string $value): string | ||
{ | ||
return Str::replace($value, array_keys($this->replaces), array_values($this->replaces)); | ||
} | ||
|
||
protected function numerify(string $value): int | ||
{ | ||
if (isset($this->replaces[$value])) { | ||
return $this->replaces[$value]; | ||
} | ||
|
||
return $this->replaces[$value] = (count($this->replaces) + 1) * 100; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace LaravelLang\StatusGenerator\Processors\Translate; | ||
|
||
use DragonCode\Support\Facades\Filesystem\File; | ||
use DragonCode\Support\Facades\Helpers\Arr; | ||
use LaravelLang\StatusGenerator\Helpers\GoogleLocale; | ||
use LaravelLang\StatusGenerator\Processors\Processor; | ||
|
||
class Translate extends Processor | ||
{ | ||
public function handle(): void | ||
{ | ||
$source = $this->locales()->getSource(); | ||
|
||
foreach ($this->directories() as $locale) { | ||
$this->output->task($locale . ' translation', function () use ($locale, $source) { | ||
$locales = $this->locales()->getLocale($locale); | ||
|
||
foreach ($source as $file => $source_values) { | ||
$path = $this->getTargetFilename($locale, $file); | ||
|
||
$locale_values = Arr::get($locales, $file, []); | ||
|
||
$result = $this->merge($source_values, $locale_values, $locale); | ||
|
||
! empty($result) ? $this->store($path, $result) : $this->delete($path); | ||
} | ||
}); | ||
} | ||
|
||
$this->output->emptyLine(); | ||
} | ||
|
||
protected function merge(array $source, array $target, string $locale): array | ||
{ | ||
$target = Arr::only($target, Arr::keys($source)); | ||
|
||
foreach ($target as $key => $value) { | ||
$source[$key] = $this->isSameKey($source, $key, $value) ? $this->translate($value, $locale) : $value; | ||
} | ||
|
||
return $source; | ||
} | ||
|
||
protected function translate(string $value, string $locale): string | ||
{ | ||
return GoogleLocale::translate($value, $locale); | ||
} | ||
|
||
protected function store(string $path, array $values): void | ||
{ | ||
$this->filesystem->store($path, $values, false); | ||
} | ||
|
||
protected function delete(string $path): void | ||
{ | ||
File::ensureDelete($path); | ||
} | ||
|
||
protected function getTargetFilename(string $locale, string $filename): string | ||
{ | ||
return $this->getLocalesPath($locale . '/' . $filename . '.json', false); | ||
} | ||
|
||
protected function isSameKey(array $source, int|string $key, string $value): bool | ||
{ | ||
return ($source[$key] ?? null) === $value; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
[ | ||
"Administrator" | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"Added.": "Added.", | ||
"Administrator": "Administrator", | ||
"Foo": "Foo", | ||
"Bar.": "Bar." | ||
} |
7 changes: 7 additions & 0 deletions
7
tests/Fixtures/Resources/Translate/locales/de/php-inline.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"accepted": "This field must be accepted.", | ||
"accepted_if": "This field must be accepted when :other is :value.", | ||
"active_url": "This field is not a valid URL.", | ||
"custom2": "Custom field 2", | ||
"sub.array2": "Sub Array 2" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"0": "Numeric Zero", | ||
"10": "Numeric Ten", | ||
"100": "Numeric One Hundred", | ||
"accepted": "The :attribute must be accepted.", | ||
"accepted_if": "The :attribute must be accepted when :other is :value.", | ||
"active_url": "The :attribute is not a valid URL.", | ||
"custom": "Custom field", | ||
"sub.array": "Sub Array" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"Added.": "Added.", | ||
"Administrator": "Administrator", | ||
"Foo": "Foo", | ||
"Bar.": "Bar." | ||
} |
7 changes: 7 additions & 0 deletions
7
tests/Fixtures/Resources/Translate/locales/en/php-inline.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"accepted": "This field must be accepted.", | ||
"accepted_if": "This field must be accepted when :other is :value.", | ||
"active_url": "This field is not a valid URL.", | ||
"custom2": "Custom field 2", | ||
"sub.array2": "Sub Array 2" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"0": "Numeric Zero", | ||
"10": "Numeric Ten", | ||
"100": "Numeric One Hundred", | ||
"accepted": "The :attribute must be accepted.", | ||
"accepted_if": "The :attribute must be accepted when :other is :value.", | ||
"active_url": "The :attribute is not a valid URL.", | ||
"custom": "Custom field", | ||
"sub.array": "Sub Array" | ||
} |
Oops, something went wrong.