Skip to content

Commit 624d8df

Browse files
author
Amir Rami
authored
New lang path for Laravel 9.x (#33)
1 parent 68638e6 commit 624d8df

9 files changed

+28
-13
lines changed

Diff for: composer.json

+4-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,10 @@
2929
"autoload": {
3030
"psr-4": {
3131
"Amirami\\Localizator\\": "src"
32-
}
32+
},
33+
"files": [
34+
"src/helpers.php"
35+
]
3336
},
3437
"autoload-dev": {
3538
"psr-4": {

Diff for: src/Services/Collectors/DefaultKeyCollector.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public function getTranslated(string $locale): Collection
3636
*/
3737
protected function getFiles(string $locale): Collection
3838
{
39-
$dir = resource_path('lang'.DIRECTORY_SEPARATOR.$locale);
39+
$dir = lang_path($locale);
4040

4141
if (! file_exists($dir)) {
4242
if (! mkdir($dir, 0755) && ! is_dir($dir)) {
@@ -59,8 +59,8 @@ protected function getFiles(string $locale): Collection
5959
*/
6060
protected function requireFile(string $locale, SplFileInfo $fileInfo): array
6161
{
62-
return require resource_path(
63-
'lang'.DIRECTORY_SEPARATOR.$locale.DIRECTORY_SEPARATOR.$fileInfo->getRelativePathname()
62+
return require lang_path(
63+
$locale.DIRECTORY_SEPARATOR.$fileInfo->getRelativePathname()
6464
);
6565
}
6666
}

Diff for: src/Services/Collectors/JsonKeyCollector.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class JsonKeyCollector implements Collectable
1414
*/
1515
public function getTranslated(string $locale): Collection
1616
{
17-
$file = resource_path('lang'.DIRECTORY_SEPARATOR."{$locale}.json");
17+
$file = lang_path("{$locale}.json");
1818

1919
if (! file_exists($file)) {
2020
return new JsonKeyCollection;

Diff for: src/Services/Writers/DefaultWriter.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,6 @@ protected function exportArray(array $contents): string
7373
*/
7474
protected function getFile(string $locale, string $fileName): string
7575
{
76-
return resource_path('lang'.DIRECTORY_SEPARATOR.$locale.DIRECTORY_SEPARATOR.$fileName.'.php');
76+
return lang_path($locale.DIRECTORY_SEPARATOR.$fileName.'.php');
7777
}
7878
}

Diff for: src/Services/Writers/JsonWriter.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class JsonWriter implements Writable
1414
*/
1515
public function put(string $locale, Translatable $keys): void
1616
{
17-
$file = resource_path('lang'.DIRECTORY_SEPARATOR."{$locale}.json");
17+
$file = lang_path("{$locale}.json");
1818

1919
(new Filesystem)->put(
2020
$file,

Diff for: src/helpers.php

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
if (! function_exists('lang_path')) {
4+
/**
5+
* @param string $path
6+
* @return string
7+
*/
8+
function lang_path($path = '')
9+
{
10+
return resource_path('lang'.($path !== '' ? DIRECTORY_SEPARATOR.$path : ''));
11+
}
12+
}

Diff for: tests/Concerns/CreatesTestFiles.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ protected function createTestView(string $contents, string $fileName = 'test'):
3939
*/
4040
protected function createTestLangFile(string $contents, string $fileName): void
4141
{
42-
$this->createTestFile(
43-
$contents,
44-
'lang'.DIRECTORY_SEPARATOR.$fileName
42+
file_put_contents(
43+
lang_path($fileName),
44+
$contents
4545
);
4646
}
4747

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

7272
if (! file_exists($dir) && ! mkdir($dir, 0755) && ! is_dir($dir)) {
7373
throw new RuntimeException(sprintf('Directory "%s" was not created', $dir));

Diff for: tests/Concerns/ImportsLangFiles.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ trait ImportsLangFiles
1010
*/
1111
protected function getLangFilePath(string $fileName): string
1212
{
13-
return resource_path('lang'.DIRECTORY_SEPARATOR.$fileName);
13+
return lang_path($fileName);
1414
}
1515

1616
/**

Diff for: tests/TestCase.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public function getEnvironmentSetUp($app): void
6666
protected static function assertLangFileExists(string $fileName, string $message = ''): void
6767
{
6868
static::assertFileExists(
69-
resource_path('lang'.DIRECTORY_SEPARATOR.$fileName),
69+
lang_path($fileName),
7070
$message
7171
);
7272
}

0 commit comments

Comments
 (0)