Skip to content

Commit

Permalink
Revert "Translate vendor language files (php) (#49)" (#57)
Browse files Browse the repository at this point in the history
This reverts commit 39c853e.
  • Loading branch information
tanmuhittin authored Jul 23, 2022
1 parent 39c853e commit 942494e
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 97 deletions.
121 changes: 36 additions & 85 deletions src/TranslationFileTranslators/PhpArrayFileTranslator.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Tanmuhittin\LaravelGoogleTranslate\TranslationFileTranslators;

use Illuminate\Support\Facades\Lang;
use Illuminate\Support\Str;
use Tanmuhittin\LaravelGoogleTranslate\Contracts\FileTranslatorContract;
use Tanmuhittin\LaravelGoogleTranslate\Helpers\ConsoleHelper;
Expand All @@ -12,7 +11,6 @@ class PhpArrayFileTranslator implements FileTranslatorContract
{
use ConsoleHelper;
private $base_locale;
private $base_locale_path;
private $target_files;
private $excluded_files;
private $verbose;
Expand All @@ -21,7 +19,6 @@ class PhpArrayFileTranslator implements FileTranslatorContract
public function __construct($base_locale, $verbose = true, $force = false)
{
$this->base_locale = $base_locale;
$this->base_locale_path = $this->to_unix_dir_separator(resource_path('lang/' . $this->base_locale . '/'));
$this->verbose = $verbose;
$this->force = $force;
}
Expand All @@ -31,21 +28,16 @@ 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 = [];
$group_to_translate = $this->file_to_namespace($file);
$file_address = $this->get_language_file_address($target_locale, $file);

$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($group_to_translate, [], $target_locale);
$existing_translations = trans($file, [], $target_locale);
$this->line('Existing translations collected');
}

$to_be_translateds = trans($group_to_translate, [], $this->base_locale);
$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);
Expand All @@ -60,29 +52,21 @@ public function handle($target_locale) : void
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)
{
$target = $this->get_language_file_address($target_locale, $file);

if (! file_exists(dirname($target))) {
mkdir(dirname($target), 0777, true);
}

$file = fopen($target, "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()
Expand All @@ -94,54 +78,51 @@ 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)
{
// We replace the first base locale code ocurrence and change it to the target locale code, this
// allows to us to use it with package translations (under /lang/vendor)
$dest_path = Str::replaceFirst(
'/' . $this->base_locale . '/',
'/' . $locale . '/',
(empty($sub_folder) ? $this->base_locale_path : $sub_folder)
);

return $dest_path;
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);
private function strip_php_extension($filename){
if(substr($filename,-4) === '.php'){
$filename = substr($filename,0, -4);
}
return $filename;
}

private function get_translation_files()
{

private function get_translation_files($folder = null){
if (count($this->target_files) > 0) {
return $this->target_files;
$files = $this->target_files;
}

$lang_path = $this->to_unix_dir_separator(resource_path('lang'));
$directory_iterator = new \RecursiveDirectoryIterator($lang_path, \FilesystemIterator::SKIP_DOTS | \FilesystemIterator::UNIX_PATHS);
$recursive_iterator = new \RecursiveIteratorIterator($directory_iterator);
$regex = '/^.+\/' . $this->base_locale . '\/.+\.php$/i';
$regex_iterator = new \RegexIterator($recursive_iterator, $regex, \RecursiveRegexIterator::GET_MATCH);

$files = [];

foreach ($regex_iterator as $file_info) {
$files[] = $this->to_unix_dir_separator($file_info[0]);
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);
}
}
}
return $files;
}


// in file operations :

/**
Expand Down Expand Up @@ -173,41 +154,11 @@ private function handleTranslations($to_be_translateds, $existing_translations,
}
}
}

return $translations;
}

// others

/**
* Returns the formatted translation group name from a file, taking into account if it's a vendor language file
* @param string $file
* @return string
*/
public function file_to_namespace(string $file): string
{
if (! Str::contains($file, '/vendor/')) {
return basename(strtolower($file), '.php');
}

// It's a vendor file, so the group is like vendor/package::group
$sub_vendor_path = Str::after($file, '/vendor/');
$namespace = Str::before($sub_vendor_path, '/' . $this->base_locale . '/');
$group = Str::after($sub_vendor_path, '/' . $this->base_locale . '/');

$group = $namespace . '::' . basename(strtolower($group), '.php');

// We make sure the namespace is added to the translator app
Lang::addNamespace($namespace, $file);

return $group;
}

public function to_unix_dir_separator(string $path): string
{
return str_replace('\\', '/', $path);
}

public function setTargetFiles($target_files)
{
$this->target_files = $target_files;
Expand Down
5 changes: 1 addition & 4 deletions tests/Unit/TranslateFilesCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,6 @@ public function testTranslatePhpFilesCommand()
$this->assertFileExists(resource_path('lang/tr/tests.php'));
unlink(resource_path('lang/tr/tests.php'));
rmdir(resource_path('lang/tr'));

$this->assertFileExists(resource_path('lang/vendor/package/tr/vendor_tests.php'));
unlink(resource_path('lang/vendor/package/tr/vendor_tests.php'));
rmdir(resource_path('lang/vendor/package/tr'));
}

public function testTranslateJsonFilesCommand()
Expand All @@ -42,3 +38,4 @@ public function testTranslateJsonFilesCommand()
unlink(resource_path('lang/tr.json'));
}
}

This file was deleted.

0 comments on commit 942494e

Please sign in to comment.