Skip to content
This repository was archived by the owner on Jan 21, 2022. It is now read-only.

Commit a6ab6ae

Browse files
author
Shawn Hooper
committed
Fixed handling of newline characters on save
1 parent 80471ba commit a6ab6ae

File tree

2 files changed

+33
-14
lines changed

2 files changed

+33
-14
lines changed

classes/ReactIntlEditor/ReactIntlEditor.php

+29-14
Original file line numberDiff line numberDiff line change
@@ -293,27 +293,34 @@ public function saveChanges() {
293293
unset($realPost['type']);
294294
}
295295

296-
297-
298-
switch ($type) {
299-
case 'missing':
300-
$this->updateSourceStrings($realPost, $locale);
301-
break;
302-
default:
303-
throw new Exception('No handler for saving this type of record is implemented');
304-
break;
305-
}
296+
$this->updateSourceStrings($realPost, $locale, $type);
306297

307298
return true;
308299
}
309300

310301
/***
311302
* @param $strings array The strings passed in from the HTML form.
312303
*/
313-
private function updateSourceStrings($strings, $locale) {
304+
private function updateSourceStrings($strings, $locale, $type) {
305+
314306
foreach($strings as $key=>$value) {
315-
if ($value == '') continue;
316-
array_push($this->localeStrings[$locale], array($key, $value));
307+
308+
309+
if ($type == 'matching') {
310+
$i = 0;
311+
foreach ($this->localeStrings[$locale] as $localeString) {
312+
if ($key == $localeString[0]) {
313+
$this->localeStrings[$locale][$i][1] = $value;
314+
continue;
315+
}
316+
$i++;
317+
}
318+
}
319+
320+
if ($type == 'missing') {
321+
if ($value == '') continue;
322+
array_push($this->localeStrings[$locale], array($key, $value));
323+
}
317324
}
318325

319326
$this->saveSourceStringsToFile($locale);
@@ -324,8 +331,16 @@ private function saveSourceStringsToFile($locale) {
324331
$output = '{';
325332

326333
for ($i = 0; $i < count($this->localeStrings[$locale]); $i++) {
334+
335+
if ($this->localeStrings[$locale][$i][1] == '') continue;
336+
327337
$output .= '"' . $this->localeStrings[$locale][$i][0] . '": "';
328-
$output .= str_replace("\n", '\n', $this->localeStrings[$locale][$i][1]);
338+
339+
if (strpos($this->localeStrings[$locale][$i][1], "\n") !== false) {
340+
$output .= str_replace("\r\n", "\\n", $this->localeStrings[$locale][$i][1]);
341+
} else {
342+
$output .= $this->localeStrings[$locale][$i][1];
343+
}
329344
$output .= ($i == count($this->localeStrings[$locale]) -1 ) ? '"' : '",';
330345
}
331346

save_changes.php

+4
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@
2222
$reactIntlEditor->saveChanges($_POST);
2323
header('Location:index.php?update=true');
2424
break;
25+
case 'matching':
26+
$reactIntlEditor->saveChanges($_POST);
27+
header('Location:index.php?update=true');
28+
break;
2529
default:
2630
throw new \ReactIntlEditor\Exception('Unknown String Type Specified in Querystring');
2731
break;

0 commit comments

Comments
 (0)