Skip to content

Commit 5b41d24

Browse files
okaufmannrmariuzzo
authored andcommitted
Add experimental support for json language files to support strings (https://laravel.com/docs/master/localization#retrieving-translation-strings)
1 parent 9462e44 commit 5b41d24

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

src/Mariuzzo/LaravelJsLocalization/Generators/LangJsGenerator.php

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,14 @@ class LangJsGenerator
3333
*/
3434
protected $messagesIncluded = [];
3535

36+
/**
37+
* Name of the domain in which all string-translation should be stored under.
38+
* More about string-translation: https://laravel.com/docs/master/localization#retrieving-translation-strings
39+
*
40+
* @var string
41+
*/
42+
protected $stringsDomain = 'strings';
43+
3644
/**
3745
* Construct a new LangJsGenerator instance.
3846
*
@@ -116,8 +124,8 @@ protected function getMessages()
116124

117125
foreach ($this->file->allFiles($path) as $file) {
118126
$pathName = $file->getRelativePathName();
119-
120-
if ($this->file->extension($pathName) !== 'php') {
127+
$extension = $this->file->extension($pathName);
128+
if ($extension != 'php' && $extension != 'json') {
121129
continue;
122130
}
123131

@@ -128,12 +136,19 @@ protected function getMessages()
128136
$key = substr($pathName, 0, -4);
129137
$key = str_replace('\\', '.', $key);
130138
$key = str_replace('/', '.', $key);
131-
139+
132140
if (starts_with($key, 'vendor')) {
133141
$key = $this->getVendorKey($key);
134142
}
135143

136-
$messages[$key] = include $path . DIRECTORY_SEPARATOR . $pathName;
144+
$fullPath = $path.DIRECTORY_SEPARATOR.$pathName;
145+
if ($extension == 'php') {
146+
$messages[$key] = include $fullPath;
147+
} else {
148+
$key = $key.$this->stringsDomain;
149+
$fileContent = file_get_contents($fullPath);
150+
$messages[$key] = json_decode($fileContent, true);
151+
}
137152
}
138153

139154
$this->sortMessages($messages);

0 commit comments

Comments
 (0)