Skip to content

Commit 2793f57

Browse files
author
Marijn Kampf
committed
Added allow-newlines option to config/laravel-translatable-string-exporter that indicates weather new lines are allowed in translations.
Including additional tests.
1 parent 654d67c commit 2793f57

File tree

3 files changed

+67
-0
lines changed

3 files changed

+67
-0
lines changed

config/laravel-translatable-string-exporter.php

+3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
'*.js',
1313
],
1414

15+
// Indicates weather new lines are allowed in translations.
16+
'allow-newlines' => false,
17+
1518
// Translation function names.
1619
// If your function name contains $ escape it using \$ .
1720
'functions'=> [

src/Core/CodeParser.php

+4
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ public function __construct()
3131
'@lang'
3232
]);
3333
$this->pattern = str_replace('[FUNCTIONS]', implode('|', $this->functions), $this->pattern);
34+
35+
if (config('laravel-translatable-string-exporter.allow-newlines')) {
36+
$this->pattern .= 's';
37+
}
3438
}
3539

3640
/**

tests/ExporterTest.php

+60
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,66 @@ public function testTranslationFunctionNames()
9393
$this->assertEquals($expected, $actual);
9494
}
9595

96+
public function testMultiLineDefaultSettingsTranslations()
97+
{
98+
$this->cleanLangsFolder();
99+
100+
$view = "{{ __('single line') }} " .
101+
"{{ __('translation.keys') }} " .
102+
// Verify legacy behaviours:
103+
// 1) Strings including escaped newlines (\n) are processed
104+
"{{ __('escaped\\nnewline') }}" .
105+
// 2) Strings including un-escaped newlines are ignored.
106+
"{{ __(\"ignored\nmultiple\nline\nstring\") }}";
107+
108+
$this->createTestView($view);
109+
110+
$this->artisan('translatable:export', ['lang' => 'es'])
111+
->expectsOutput('Translatable strings have been extracted and written to the es.json file.')
112+
->assertExitCode(0);
113+
114+
$actual = $this->getTranslationFileContent('es');
115+
$expected = [
116+
'single line' => 'single line',
117+
'translation.keys' => 'translation.keys',
118+
'escaped\\nnewline' => 'escaped\\nnewline',
119+
];
120+
121+
$this->assertEquals($expected, $actual);
122+
}
123+
124+
public function testMultiLineSettingsTranslations()
125+
{
126+
$this->app['config']->set('laravel-translatable-string-exporter.allow-newlines', true);
127+
128+
$this->cleanLangsFolder();
129+
130+
$view = "{{ __('single line') }} " .
131+
"{{ __('translation.keys') }} " .
132+
// No change to 1st legacy behaviour:
133+
// 1) Strings including escaped newlines (\n) are processed
134+
"{{ __('escaped\\nnewline') }}" .
135+
// Un-escaped newlines are now also processed.
136+
// 2) Strings including un-escaped newlines are ignored.
137+
"{{ __(\"detected\nmultiple\nline\nstring\") }}";
138+
139+
$this->createTestView($view);
140+
141+
$this->artisan('translatable:export', ['lang' => 'es'])
142+
->expectsOutput('Translatable strings have been extracted and written to the es.json file.')
143+
->assertExitCode(0);
144+
145+
$actual = $this->getTranslationFileContent('es');
146+
$expected = [
147+
'single line' => 'single line',
148+
'translation.keys' => 'translation.keys',
149+
'escaped\nnewline' => 'escaped\nnewline',
150+
"detected\nmultiple\nline\nstring" => "detected\nmultiple\nline\nstring",
151+
];
152+
153+
$this->assertEquals($expected, $actual);
154+
}
155+
96156
public function testUpdatingTranslations()
97157
{
98158
$this->cleanLangsFolder();

0 commit comments

Comments
 (0)