Skip to content

Commit

Permalink
Merge pull request #5 from q--/Fall_back_to_key_if_source_language_is…
Browse files Browse the repository at this point in the history
…_empty

Fall back to key if source language is empty
  • Loading branch information
q-- authored Feb 27, 2024
2 parents 455056e + c627775 commit 4b0a6d0
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/Drivers/Translation.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ public function autoTranslate($language = false)
}

/**
*
* Translate text using Google Translate
*
* @param $language
Expand All @@ -79,7 +78,7 @@ public function autoTranslate($language = false)
* @throws \ErrorException
*/
public function getGoogleTranslate($language,$token){
$tr = new GoogleTranslate($language);
$tr = new GoogleTranslate($language, $this->sourceLanguage);
return $tr->translate($token);
}

Expand All @@ -89,12 +88,19 @@ public function getGoogleTranslate($language,$token){
* @param $language
*/
public function translateLanguage($language){
//No need to translate e.g. English to English
if ($language === $this->sourceLanguage) {
return;
}

$translations = $this->getSourceLanguageTranslationsWith($language);

foreach ($translations as $type => $groups) {
foreach ($groups as $group => $translations) {
foreach ($translations as $key => $value) {
$sourceLanguageValue = $value[$this->sourceLanguage];
//Value will be empty if it's found in the app source code but not in the source language files
//We fall back to $key in that case
$sourceLanguageValue = in_array($value[$this->sourceLanguage], ["", null]) ? $key : $value[$this->sourceLanguage];
$targetLanguageValue = $value[$language];

if (in_array($targetLanguageValue, ["", null])) {
Expand Down

0 comments on commit 4b0a6d0

Please sign in to comment.