From e83f2e6361458b86fc01e8721d5977b446eee9f5 Mon Sep 17 00:00:00 2001 From: Maykon Henrique Facincani Date: Tue, 4 Feb 2020 18:02:49 -0300 Subject: [PATCH] Multimensional PHP Arrays (#25) When working with multidimensional arrays, the function doesn't work when is checking for already translated strings and printing the skipped value, because try to convert Array to String. --- src/Commands/TranslateFilesCommand.php | 45 ++++++++++++++++++++------ 1 file changed, 35 insertions(+), 10 deletions(-) diff --git a/src/Commands/TranslateFilesCommand.php b/src/Commands/TranslateFilesCommand.php index 36aa724..99788b4 100644 --- a/src/Commands/TranslateFilesCommand.php +++ b/src/Commands/TranslateFilesCommand.php @@ -236,16 +236,7 @@ public function translate_php_array_files($locale) $to_be_translateds = trans($file, [], $this->base_locale); $new_lang = []; if(is_array($to_be_translateds)){ - foreach ($to_be_translateds as $key => $to_be_translated) { - if (isset($already_translateds[$key]) && $already_translateds[$key] != '' && !$this->force) { - $new_lang[$key] = $already_translateds[$key]; - if ($this->verbose) { - $this->line('Exists Skipping -> ' . $to_be_translated . ' : ' . $new_lang[$key]); - } - continue; - } - $new_lang[$key] = $this->translate_attribute($to_be_translated,$locale); - } + $new_lang = $this->skipMultidensional($to_be_translateds, $already_translateds, $locale); } //save new lang to new file if(!file_exists(resource_path('lang/' . $locale ))){ @@ -259,6 +250,40 @@ public function translate_php_array_files($locale) return; } + /** + * Walks array recursively to find strings already translated + * + * @author Maykon Facincani + * + * @param array $to_be_translateds + * @param array $already_translateds + * @param String $locale + * + * @return array + */ + private function skipMultidensional($to_be_translateds, $already_translateds, $locale){ + $data = []; + foreach($to_be_translateds as $key => $to_be_translated){ + if ( is_array($to_be_translateds[$key]) ) { + if( !isset($already_translateds[$key]) ) { + $already_translateds[$key] = []; + } + $data[$key] = $this->skipMultidensional($to_be_translateds[$key], $already_translateds[$key], $locale); + } else { + if ( isset($already_translateds[$key]) && $already_translateds[$key] != '' && !$this->force) { + $data[$key] = $already_translateds[$key]; + if ($this->verbose) { + $this->line('Exists Skipping -> ' . $to_be_translated . ' : ' . $data[$key]); + } + continue; + } else { + $data[$key] = $this->translate_attribute($to_be_translated,$locale); + } + } + } + return $data; + } + private function translate_attribute($attribute,$locale){ if(is_array($attribute)){ $return = [];