Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "tanmuhittin/laravel-google-translate",
"name": "alfandy12/laravel-google-translate",
"description": "Translate translation files to other languages using google translate api",
"license": "MIT",
"require": {
Expand Down
152 changes: 82 additions & 70 deletions src/TranslationFileTranslators/PhpArrayFileTranslator.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@
class PhpArrayFileTranslator implements FileTranslatorContract
{
use ConsoleHelper;

private $base_locale;
private $target_files;
private $excluded_files;
private $target_files = [];
private $excluded_files = [];
private $verbose;
private $force;

Expand All @@ -23,53 +24,58 @@ public function __construct($base_locale, $verbose = true, $force = false)
$this->force = $force;
}

public function handle($target_locale) : void
public function handle($target_locale): void
{
$files = $this->get_translation_files();
$this->create_missing_target_folders($target_locale, $files);

foreach ($files as $file) {
$existing_translations = [];
$file_address = $this->get_language_file_address($target_locale, $file.'.php');
$this->line($file_address.' is preparing');
$file_address = $this->get_language_file_address($target_locale, $file . '.php');
$this->line($file_address . ' is preparing');

if (file_exists($file_address)) {
$this->line('File already exists');
$existing_translations = trans($file, [], $target_locale);
$this->line('Existing translations collected');
}

$to_be_translateds = trans($file, [], $this->base_locale);
$this->line('Source text collected');

$translations = [];
if (is_array($to_be_translateds)) {
$translations = $this->handleTranslations($to_be_translateds, $existing_translations, $target_locale);
}

$this->write_translations_to_file($target_locale, $file, $translations);
}
return;
}

// file, folder operations:

private function create_missing_target_folders($target_locale, $files)
{
$target_locale_folder = $this->get_language_file_address($target_locale);
if(!is_dir($target_locale_folder)){

if (!is_dir($target_locale_folder)) {
mkdir($target_locale_folder);
}
foreach ($files as $file){
if(Str::contains($file, '/')){

foreach ($files as $file) {
if (Str::contains($file, '/')) {
$folder_address = $this->get_language_file_address($target_locale, dirname($file));
if(!is_dir($folder_address)){
if (!is_dir($folder_address)) {
mkdir($folder_address, 0777, true);
}
}
}
}

private function write_translations_to_file($target_locale, $file, $translations){
$file = fopen($this->get_language_file_address($target_locale, $file.'.php'), "w+");
private function write_translations_to_file($target_locale, $file, $translations)
{
$file = fopen($this->get_language_file_address($target_locale, $file . '.php'), 'w+');
$export = var_export($translations, true);

//use [] notation instead of array()
// Use [] notation instead of array()
$patterns = [
"/array \(/" => '[',
"/^([ ]*)\)(,?)$/m" => '$1]$2',
Expand All @@ -78,86 +84,92 @@ private function write_translations_to_file($target_locale, $file, $translations
];
$export = preg_replace(array_keys($patterns), array_values($patterns), $export);


$write_text = "<?php \nreturn " . $export . ";";
fwrite($file, $write_text);
fclose($file);
return 1;
}

private function get_language_file_address($locale, $sub_folder = null){
return $sub_folder!==null ?
FileHelper::getFile($locale.'/'.$sub_folder) :
FileHelper::getFile($locale);
private function get_language_file_address($locale, $sub_folder = null)
{
return $sub_folder !== null
? FileHelper::getFile($locale . '/' . $sub_folder)
: FileHelper::getFile($locale);
}

private function strip_php_extension($filename){
if(substr($filename,-4) === '.php'){
$filename = substr($filename,0, -4);
}
return $filename;
private function strip_php_extension($filename)
{
return substr($filename, -4) === '.php' ? substr($filename, 0, -4) : $filename;
}

private function get_translation_files($folder = null){
if (count($this->target_files) > 0) {
$files = $this->target_files;
}
else{
$files = [];
$dir_contents = preg_grep('/^([^.])/', scandir($this->get_language_file_address($this->base_locale, $folder)));
foreach ($dir_contents as $dir_content){
if(!is_null($folder))
$dir_content = $folder.'/'.$dir_content;
if (in_array($this->strip_php_extension($dir_content), $this->excluded_files)) {
continue;
}
if(is_dir($this->get_language_file_address($this->base_locale, $dir_content))){
$files = array_merge($files,$this->get_translation_files($dir_content));
}
else{
$files[] = $this->strip_php_extension($dir_content);
}
private function get_translation_files($folder = null)
{
$files = count($this->target_files) > 0 ? $this->target_files : [];
$dir_contents = preg_grep('/^([^.])/', scandir($this->get_language_file_address($this->base_locale, $folder)));

foreach ($dir_contents as $dir_content) {
$dir_content = $folder ? $folder . '/' . $dir_content : $dir_content;

if (in_array($this->strip_php_extension($dir_content), $this->excluded_files)) {
continue;
}

if (is_dir($this->get_language_file_address($this->base_locale, $dir_content))) {
$files = array_merge($files, $this->get_translation_files($dir_content));
} else {
$files[] = $this->strip_php_extension($dir_content);
}
}

return $files;
}


// in file operations :

/**
* Walks array recursively to find and translate strings
*
* @param array $to_be_translateds
* @param array $existing_translations
* @param String $target_locale
*
* @return array
*/
private function handleTranslations($to_be_translateds, $existing_translations, $target_locale)
{
$translations = [];
foreach ($to_be_translateds as $key => $to_be_translated) {
if (is_array($to_be_translated)) {
if (!isset($existing_translations[$key])) {
$existing_translations[$key] = [];
}
$translations[$key] = $this->handleTranslations($to_be_translated, $existing_translations[$key], $target_locale);
$translations[$key] = $this->handleTranslations(
$to_be_translated,
$existing_translations[$key] ?? [],
$target_locale
);
} else {
if (isset($existing_translations[$key]) && $existing_translations[$key] != '' && !$this->force) {
$translations[$key] = $existing_translations[$key];
$this->line('Exists Skipping -> ' . $to_be_translated . ' : ' . $translations[$key]);
continue;
} else {
$translations[$key] = Str::apiTranslateWithAttributes($to_be_translated, $target_locale, $this->base_locale);
$this->line($to_be_translated . ' : ' . $translations[$key]);
}
[$placeholderText, $placeholders] = $this->replaceHtmlTagsWithPlaceholders($to_be_translated);
$translatedText = isset($existing_translations[$key]) && !$this->force
? $existing_translations[$key]
: Str::apiTranslateWithAttributes($placeholderText, $target_locale, $this->base_locale);
$translations[$key] = $this->restoreHtmlTagsFromPlaceholders($translatedText, $placeholders);
$this->line($to_be_translated . ' : ' . $translations[$key]);
}
}
return $translations;
}

// others
private function replaceHtmlTagsWithPlaceholders($text)
{
$placeholders = [];
$counter = 0;

$placeholderCallback = function ($matches) use (&$placeholders, &$counter) {
$placeholder = "__HTML_PLACEHOLDER_$counter";
$placeholders[$placeholder] = $matches[0];
$counter++;
return $placeholder;
};

$textWithPlaceholders = preg_replace_callback('/<[^>]+>/', $placeholderCallback, $text);

return [$textWithPlaceholders, $placeholders];
}

private function restoreHtmlTagsFromPlaceholders($text, $placeholders)
{
$restoreCallback = function ($matches) use ($placeholders) {
return $placeholders[$matches[0]] ?? $matches[0];
};

return preg_replace_callback('/__HTML_PLACEHOLDER_\d+/', $restoreCallback, $text);
}

public function setTargetFiles($target_files)
{
Expand Down