Skip to content

Commit

Permalink
Multimensional PHP Arrays (#25)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
kaneymhf authored Feb 4, 2020
1 parent 90f96d3 commit e83f2e6
Showing 1 changed file with 35 additions and 10 deletions.
45 changes: 35 additions & 10 deletions src/Commands/TranslateFilesCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ))){
Expand All @@ -259,6 +250,40 @@ public function translate_php_array_files($locale)
return;
}

/**
* Walks array recursively to find strings already translated
*
* @author Maykon Facincani <facincani.maykon@gmail.com>
*
* @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 = [];
Expand Down

0 comments on commit e83f2e6

Please sign in to comment.