Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Internationalisation #126

Closed
wants to merge 54 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
54 commits
Select commit Hold shift + click to select a range
32e9cea
Add generation of report
May 9, 2023
3df06b6
improve report in French
May 12, 2023
648dcbb
add command in matter list for reports
May 12, 2023
345564b
add expiration date, next renewal date and qt in export
May 12, 2023
9555757
update report in English
May 12, 2023
c80e756
Deal more return code from Dolibarr API
May 15, 2023
55c4c4e
Update infra for interntionalisation
Jun 16, 2023
fde18fe
Internationalize Actors
Jun 16, 2023
1d0a5af
Internationalize Categories
Jun 16, 2023
6c21b0a
Internationalize Classifiers type
Jun 16, 2023
55e3567
+def actors
Jun 16, 2023
634837e
Internationalize Documents templates and merging tools
Jun 16, 2023
29abc7b
Internationalize Events
Jun 16, 2023
db42327
Internationalize Fees
Jun 16, 2023
7df93b5
Internationalize Matters
Jun 16, 2023
df33e66
Internationalize Matter Types
Jun 16, 2023
fe5d5f3
Internationalize Renewals
Jun 16, 2023
17230b3
Internationalize Roles
Jun 16, 2023
efc4d14
Internationalize Rules
Jun 16, 2023
169d1ec
Internationalize Template Members
Jun 16, 2023
ec4c02b
Internationalize Users
Jun 16, 2023
67dd78b
Internationalize Home
Jun 16, 2023
5acacb8
Internationalize remaining controllers
Jun 16, 2023
bfe837a
update composer.lock
Jun 16, 2023
c756740
Add translation catalogs
Jun 16, 2023
fbd5c2a
Add translation tools
Jun 16, 2023
f402699
Merge branch 'master' into i18n
Nov 18, 2023
e188a05
move internationalisation to native method
Nov 19, 2023
62cb97b
More internationalisation
Jan 16, 2024
5f61b30
Merge branch 'master' into i18n2
Jan 19, 2024
598ad5c
remove unuseful migration file
Feb 2, 2024
4ec4fdf
Merge branch 'master' into i18n2
Feb 26, 2024
c22b614
Use translated strings in autocompletion fields
Mar 12, 2024
5ae390a
Really use translation for Create
Mar 13, 2024
99a8698
fix autocompletion of translated country
Mar 15, 2024
46752cd
translate content table of rules
Mar 15, 2024
8b45bbf
Add translation of countries
Mar 18, 2024
64c2dc3
Translation of "see family"
Mar 18, 2024
b6a5919
Remove entry doubled (actors)
Mar 18, 2024
89d9ffc
Translate also detail
Mar 18, 2024
bc73f9a
Use middleware to set the locale for translations
Mar 19, 2024
805aad5
fix translations in French
Mar 19, 2024
3dc06a2
Add missing file from previous commits
Mar 19, 2024
4c441f6
Restore import in matterController
Mar 25, 2024
36c7b01
Fix display and translation of new label Granted/Reg'd
Mar 25, 2024
81486f0
Translate also the status when it is not a link
Apr 4, 2024
6c35d14
Don't translate code for filter in matter/index
Apr 5, 2024
216561d
Filter status on the translated values
Apr 5, 2024
5e17dd4
clean: Remove logs
Apr 5, 2024
fa32686
fix: keep constant the height of category list when hovering it
Apr 12, 2024
72e2143
Merge branch 'master' into i18n2
Apr 12, 2024
5d58d6e
fix: don't translate CC
Jun 13, 2024
9902f76
fix: syntax in documents create page after i18n
Jun 13, 2024
9f103a1
fix: syntax in documents show page after i18n
Jun 13, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
256 changes: 256 additions & 0 deletions app/Console/Commands/TranslationsParseBladeTemplatesCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,256 @@
<?php
// from https://dev.to/agenceappy/generating-po-files-with-laravel-translating-blade-templates-15im
// By Agence Appy
namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\File;

class TranslationsParseBladeTemplatesCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'translations:parse-blade';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Parse blade templates to find translations and insert it in lang files';

/**
* The functions used in your blade templates to translate strings
*
* @var string
*/
protected $functions = ['singular' => ['__'], 'plural' => ['_n']];

/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
$path_directories = resource_path('views');

$directories = File::directories($path_directories);


// Parse all directories within the resources/views folder
foreach ($directories as $dir):

// Init translations global container
echo "Exploring " . $dir . "\n";
$translations = [];
foreach ($this->functions['singular'] as $strFunction):
$translations[$strFunction] = [];
endforeach;
foreach ($this->functions['plural'] as $strFunction):
$translations[$strFunction] = [];
endforeach;

// Parse all files from folder
$files = File::allFiles($dir);
foreach ($files as $file):
// Parse all lines from file
echo "Parsing " . $file . "\n";
$lines = File::lines($file);
foreach ($lines as $index => $line):
// Get strings from line
$strings = $this->parseLine($line);

// Insert strings into global translations container
foreach ($this->functions as $type => $functions):
foreach ($functions as $strFunction):
if (!empty($strings[$strFunction])):

foreach ($strings[$strFunction] as $val):
if (is_array($val)) {
$string = $val[0].','.$val[1];
} else {
$string = $val;
}
if (isset($translations[$strFunction][$string])) {
$translations[$strFunction][$string][] = $file->getPathname().':'.($index+1);
} else {
$translations[$strFunction][$string] = [$file->getPathname().':'.($index+1)];
}
endforeach;

endif;
endforeach;
endforeach;
endforeach;
endforeach;

// Create directories if not exists
$language_dir = lang_path('blade-translations');
if (!File::isDirectory($language_dir)):
File::makeDirectory($language_dir, 0755);
endif;

$path = explode('/', $dir);
$interface = array_pop($path);
$language_dir_interface = lang_path('blade-translations/'.$interface);
echo $language_dir_interface;
if (!File::isDirectory($language_dir_interface)):
File::makeDirectory($language_dir_interface, 0755);
endif;

// Create file content
$content = "<?php\n\n";
$content .= "return [\n\n";
$content .= " /*\n";
$content .= " |--------------------------------------------------------------------------\n";
$content .= " | STATIC STRINGS TRANSLATIONS\n";
$content .= " |--------------------------------------------------------------------------\n";
$content .= " |\n";
$content .= " | !!! WARNING - This is a file generated by the 'translations:parse-blade' command. !!!\n";
$content .= " | You should not modify it, as it shall be replaced next time this command is executed.\n";
$content .= " |\n";
$content .= " */\n\n";

$i = 0;
foreach ($this->functions as $type => $functions):
foreach ($functions as $strFunction):
foreach ($translations[$strFunction] as $translation => $paths):
foreach ($paths as $p):
$content .= " // $p \n";
endforeach;
$content .= " $i => $strFunction($translation), \n";
$i++;
endforeach;
endforeach;
endforeach;
$content .= "\n];";

// Generate file
File::put($language_dir_interface.'/static.php', $content);

endforeach;

$this->info('Strings have been exported from blade templates successfully !');
}

/**
* Return translated strings within the line
*/
private function parseLine($line) {
$return = [];

// First let's see if the line has to be parsed
$found = false;
foreach ($this->functions as $type => $functions):
foreach ($functions as $strFunction):
$pos = strpos($line, $strFunction.'(');
if ($pos !== false) {
$found = true;
break 2;
}
endforeach;
endforeach;

// if not, return
if (!$found) return [];

// Then parse for each function
foreach ($this->functions as $type => $functions):
foreach ($functions as $strFunction):
$return[$strFunction] = [];
$pos = strpos($line, $strFunction.'(');

// if not found, head to next function
if ($pos === false) continue;

while ($pos !== false):
$arr = $this->getNextString($line, $pos, $type);
if (!$arr):
// Error findind string, leave loop
$pos = false;
else:
$line = $arr['line'];
$return[$strFunction][] = $arr['string'];
$pos = strpos($line, $strFunction.'(');
endif;
endwhile;
endforeach;
endforeach;

return $return;
}

/**
* Return first string found and the rest of the line to be parsed
*/
private function getNextString($subline, $pos, $type) {

$substr = trim(substr($subline, $pos+3));

$separator = $substr[0];
$nextSeparatorPos = $this->getNextSeparator($substr, $separator);

if (!$nextSeparatorPos) return [];

if ($type == 'singular'):
$string = substr($substr, 0, $nextSeparatorPos+1);
$rest = substr($substr, $nextSeparatorPos+1);

// security check : string must start and end with the separator => same character
if ($string[0] != $string[strlen($string)-1]):
return [];
endif;

return ['string' => $string, 'line' => $rest];
else:
$first_string = substr($substr, 0, $nextSeparatorPos+1);
$rest = substr($substr, $nextSeparatorPos+1);

// security check : string must start and end with the separator => same character
if ($first_string[0] != $first_string[strlen($first_string)-1]):
return [];
endif;

$comma_pos = strpos($rest, ',');
$rest = trim(substr($rest, $comma_pos+1));

$separator = $substr[0];
$nextSeparatorPos = $this->getNextSeparator($rest, $separator);

if (!$nextSeparatorPos) return [];

$second_string = substr($rest, 0, $nextSeparatorPos+1);
$rest = substr($rest, $nextSeparatorPos+1);

// security check : string must start and end with the separator => same character
if ($second_string[0] != $second_string[strlen($second_string)-1]):
return [];
endif;

return ['string' => [$first_string, $second_string], 'line' => $rest];
endif;
}

/**
* Return first unescaped separator of string
*/
private function getNextSeparator($str, $separator) {
$substr = substr($str, 1);
$found = false;
preg_match_all('/'.$separator.'/', $substr, $matches, PREG_OFFSET_CAPTURE);
foreach($matches[0] as $match):
$pos = $match[1];
if ($substr[$pos-1] != '\\'):
$found = true;
break;
endif;
endforeach;

if ($found) return $pos+1;
return false;
}
}
Loading
Loading