Skip to content

Commit

Permalink
New lang path for Laravel 9.x (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
Amir Rami authored Feb 23, 2022
1 parent 68638e6 commit 624d8df
Show file tree
Hide file tree
Showing 9 changed files with 28 additions and 13 deletions.
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@
"autoload": {
"psr-4": {
"Amirami\\Localizator\\": "src"
}
},
"files": [
"src/helpers.php"
]
},
"autoload-dev": {
"psr-4": {
Expand Down
6 changes: 3 additions & 3 deletions src/Services/Collectors/DefaultKeyCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function getTranslated(string $locale): Collection
*/
protected function getFiles(string $locale): Collection
{
$dir = resource_path('lang'.DIRECTORY_SEPARATOR.$locale);
$dir = lang_path($locale);

if (! file_exists($dir)) {
if (! mkdir($dir, 0755) && ! is_dir($dir)) {
Expand All @@ -59,8 +59,8 @@ protected function getFiles(string $locale): Collection
*/
protected function requireFile(string $locale, SplFileInfo $fileInfo): array
{
return require resource_path(
'lang'.DIRECTORY_SEPARATOR.$locale.DIRECTORY_SEPARATOR.$fileInfo->getRelativePathname()
return require lang_path(
$locale.DIRECTORY_SEPARATOR.$fileInfo->getRelativePathname()
);
}
}
2 changes: 1 addition & 1 deletion src/Services/Collectors/JsonKeyCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class JsonKeyCollector implements Collectable
*/
public function getTranslated(string $locale): Collection
{
$file = resource_path('lang'.DIRECTORY_SEPARATOR."{$locale}.json");
$file = lang_path("{$locale}.json");

if (! file_exists($file)) {
return new JsonKeyCollection;
Expand Down
2 changes: 1 addition & 1 deletion src/Services/Writers/DefaultWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,6 @@ protected function exportArray(array $contents): string
*/
protected function getFile(string $locale, string $fileName): string
{
return resource_path('lang'.DIRECTORY_SEPARATOR.$locale.DIRECTORY_SEPARATOR.$fileName.'.php');
return lang_path($locale.DIRECTORY_SEPARATOR.$fileName.'.php');
}
}
2 changes: 1 addition & 1 deletion src/Services/Writers/JsonWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class JsonWriter implements Writable
*/
public function put(string $locale, Translatable $keys): void
{
$file = resource_path('lang'.DIRECTORY_SEPARATOR."{$locale}.json");
$file = lang_path("{$locale}.json");

(new Filesystem)->put(
$file,
Expand Down
12 changes: 12 additions & 0 deletions src/helpers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

if (! function_exists('lang_path')) {
/**
* @param string $path
* @return string
*/
function lang_path($path = '')
{
return resource_path('lang'.($path !== '' ? DIRECTORY_SEPARATOR.$path : ''));
}
}
8 changes: 4 additions & 4 deletions tests/Concerns/CreatesTestFiles.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ protected function createTestView(string $contents, string $fileName = 'test'):
*/
protected function createTestLangFile(string $contents, string $fileName): void
{
$this->createTestFile(
$contents,
'lang'.DIRECTORY_SEPARATOR.$fileName
file_put_contents(
lang_path($fileName),
$contents
);
}

Expand All @@ -67,7 +67,7 @@ protected function createTestJsonLangFile(array $contents, string $locale): void
protected function createTestDefaultLangFile(array $contents, string $fileName, string $locale): void
{
$export = sprintf("<?php\n\nreturn %s;\n", var_export($contents, true));
$dir = resource_path('lang'.DIRECTORY_SEPARATOR.$locale);
$dir = lang_path($locale);

if (! file_exists($dir) && ! mkdir($dir, 0755) && ! is_dir($dir)) {
throw new RuntimeException(sprintf('Directory "%s" was not created', $dir));
Expand Down
2 changes: 1 addition & 1 deletion tests/Concerns/ImportsLangFiles.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ trait ImportsLangFiles
*/
protected function getLangFilePath(string $fileName): string
{
return resource_path('lang'.DIRECTORY_SEPARATOR.$fileName);
return lang_path($fileName);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public function getEnvironmentSetUp($app): void
protected static function assertLangFileExists(string $fileName, string $message = ''): void
{
static::assertFileExists(
resource_path('lang'.DIRECTORY_SEPARATOR.$fileName),
lang_path($fileName),
$message
);
}
Expand Down

0 comments on commit 624d8df

Please sign in to comment.