Skip to content

Commit

Permalink
Laravel 9 support (#55)
Browse files Browse the repository at this point in the history
* fix laravel 9 support for updated lang folder location

* fix lang folder
  • Loading branch information
tanmuhittin authored Feb 18, 2022
1 parent a5214c2 commit 65e53f8
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 6 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"license": "MIT",
"require": {
"illuminate/console": ">=5.1",
"php": ">=7.0.0",
"php": ">=7.1.0",
"illuminate/support": "^5.5|^6|^7|^8|^9",
"illuminate/translation": "^5.5|^6|^7|^8|^9",
"stichoza/google-translate-php": "^4.0",
Expand Down
20 changes: 20 additions & 0 deletions src/Helpers/FileHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php
namespace Tanmuhittin\LaravelGoogleTranslate\Helpers;


class FileHelper
{
public static function getFile($fileAddress)
{
if (file_exists(resource_path('lang/')))
{
return resource_path('lang/' . $fileAddress);
}
if (file_exists(base_path('lang/')))
{
return base_path('lang/' . $fileAddress);
}

throw new \Exception("Language folder cannot be found");
}
}
7 changes: 4 additions & 3 deletions src/TranslationFileTranslators/JsonArrayFileTranslator.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Symfony\Component\Finder\Finder;
use Tanmuhittin\LaravelGoogleTranslate\Contracts\FileTranslatorContract;
use Tanmuhittin\LaravelGoogleTranslate\Helpers\ConsoleHelper;
use Tanmuhittin\LaravelGoogleTranslate\Helpers\FileHelper;

class JsonArrayFileTranslator implements FileTranslatorContract
{
Expand Down Expand Up @@ -43,16 +44,16 @@ public function handle($target_locale) : void
}

private function write_translated_strings_to_file($translated_strings,$target_locale){
$file = fopen(resource_path('lang/' . $target_locale . '.json'), "w+");
$file = fopen(FileHelper::getFile($target_locale . '.json'), "w+");
$write_text = json_encode($translated_strings, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
fwrite($file, $write_text);
fclose($file);
}

private function fetch_existing_translations($target_locale){
$existing_translations = [];
if (file_exists(resource_path('lang/' . $target_locale . '.json'))) {
$json_translations_string = file_get_contents(resource_path('lang/' . $target_locale . '.json'));
if (file_exists(FileHelper::getFile($target_locale . '.json'))) {
$json_translations_string = file_get_contents(FileHelper::getFile($target_locale . '.json'));
$existing_translations = json_decode($json_translations_string, true);
}
return $existing_translations;
Expand Down
5 changes: 3 additions & 2 deletions src/TranslationFileTranslators/PhpArrayFileTranslator.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Support\Str;
use Tanmuhittin\LaravelGoogleTranslate\Contracts\FileTranslatorContract;
use Tanmuhittin\LaravelGoogleTranslate\Helpers\ConsoleHelper;
use Tanmuhittin\LaravelGoogleTranslate\Helpers\FileHelper;

class PhpArrayFileTranslator implements FileTranslatorContract
{
Expand Down Expand Up @@ -86,8 +87,8 @@ private function write_translations_to_file($target_locale, $file, $translations

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

private function strip_php_extension($filename){
Expand Down

0 comments on commit 65e53f8

Please sign in to comment.