dmstr / yii2-json-editor

Yii2 wrapper for "json-editor/json-editor" (is a fork of "jdorn/json-editor").
BSD 2-Clause "Simplified" License
8 stars 9 forks source link

get properly encoded string-values after Yii::t() calls #36

Closed handcode closed 1 year ago

handcode commented 1 year ago

Currently the Editor options are added as translations without escaping in JsonEditorWidget::registerAdditionalLanguages().

$view->registerJs("
JSONEditor.defaults.languages['" . $this->language . "'] = {
    button_add_row_title: '" . \Yii::t('json-editor', 'Add {{0}}', [], $this->language) . "',
    button_collapse: '" . \Yii::t('json-editor', 'Collapse', [], $this->language) . "',
    button_copy_row_title_short: '" . \Yii::t('json-editor', 'Copy', [], $this->language) . "',
    button_delete_all: '" . \Yii::t('json-editor', 'All', [], $this->language) . "',
    button_delete_all_title: '" . \Yii::t('json-editor', 'Delete All', [], $this->language) . "',
    button_delete_last: '" . \Yii::t('json-editor', 'Last {{0}}', [], $this->language) . "',
    button_delete_last_title: '" . \Yii::t('json-editor', 'Delete Last {{0}}', [], $this->language) . "',
    #.......

This leads to broken json if one add a ' within the translated string.

Delete Last {{0}} - Cancella l'ultimo {{0}}

This PR add a new method getTranslatedEditorOptsJson() where a PHP array is build with the translated options which then will be encoded as Json before inserted in the dynamic JS String.

schmunk42 commented 1 year ago

available in 1.4.4