Skip to content

Commit ab7a22e

Browse files
authored
Merge pull request #36 from dmstr/dev/encode-translated-opts
get properly encoded string-values after Yii::t() calls
2 parents 5ef0868 + 902cf93 commit ab7a22e

File tree

1 file changed

+69
-54
lines changed

1 file changed

+69
-54
lines changed

src/JsonEditorWidget.php

Lines changed: 69 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -208,67 +208,82 @@ public static function metaSchema()
208208
return Json::decode($metaSchema);
209209
}
210210

211+
/**
212+
* for the translated editor options, we create a php array and encode it after(!) the Yii::t() calls to get properly encoded string-values
213+
*
214+
* @return string
215+
*/
216+
protected function getTranslatedEditorOptsJson()
217+
{
218+
219+
$labels = [
220+
'button_add_row_title' => \Yii::t('json-editor', 'Add {{0}}', [], $this->language),
221+
'button_collapse' => \Yii::t('json-editor', 'Collapse', [], $this->language),
222+
'button_copy_row_title_short' => \Yii::t('json-editor', 'Copy', [], $this->language),
223+
'button_delete_all' => \Yii::t('json-editor', 'All', [], $this->language),
224+
'button_delete_all_title' => \Yii::t('json-editor', 'Delete All', [], $this->language),
225+
'button_delete_last' => \Yii::t('json-editor', 'Last {{0}}', [], $this->language),
226+
'button_delete_last_title' => \Yii::t('json-editor', 'Delete Last {{0}}', [], $this->language),
227+
'button_delete_node_warning' => \Yii::t('json-editor', 'Are you sure you want to remove this node?', [], $this->language),
228+
'button_delete_row_title' => \Yii::t('json-editor', 'Delete {{0}}', [], $this->language),
229+
'button_delete_row_title_short' => \Yii::t('json-editor', 'Delete', [], $this->language),
230+
'button_expand' => \Yii::t('json-editor', 'Expand', [], $this->language),
231+
'button_move_down_title' => \Yii::t('json-editor', 'Move down', [], $this->language),
232+
'button_move_up_title' => \Yii::t('json-editor', 'Move up', [], $this->language),
233+
'button_object_properties' => \Yii::t('json-editor', 'Object Properties', [], $this->language),
234+
'choices_placeholder_text' => \Yii::t('json-editor', 'Start typing to add value', [], $this->language),
235+
'default_array_item_title' => \Yii::t('json-editor', 'item', [], $this->language),
236+
'error_additionalItems' => \Yii::t('json-editor', 'No additional items allowed in this array', [], $this->language),
237+
'error_additional_properties' => \Yii::t('json-editor', 'No additional properties allowed, but property {{0}} is set', [], $this->language),
238+
'error_anyOf' => \Yii::t('json-editor', 'Value must validate against at least one of the provided schemas', [], $this->language),
239+
'error_date' => \Yii::t('json-editor', 'Date must be in the format {{0}}', [], $this->language),
240+
'error_datetime_local' => \Yii::t('json-editor', 'Datetime must be in the format {{0}}', [], $this->language),
241+
'error_dependency' => \Yii::t('json-editor', 'Must have property {{0}}', [], $this->language),
242+
'error_disallow' => \Yii::t('json-editor', 'Value must not be of type {{0}}', [], $this->language),
243+
'error_disallow_union' => \Yii::t('json-editor', 'Value must not be one of the provided disallowed types', [], $this->language),
244+
'error_enum' => \Yii::t('json-editor', 'Value must be one of the enumerated values', [], $this->language),
245+
'error_hostname' => \Yii::t('json-editor', 'The hostname has the wrong format', [], $this->language),
246+
'error_invalid_epoch' => \Yii::t('json-editor', 'Date must be greater than 1 January 1970', [], $this->language),
247+
'error_ipv4' => \Yii::t('json-editor', 'Value must be a valid IPv4 address in the form of 4 numbers between 0 and 255, separated by dots', [], $this->language),
248+
'error_ipv6' => \Yii::t('json-editor', 'Value must be a valid IPv6 address', [], $this->language),
249+
'error_maxItems' => \Yii::t('json-editor', 'Value must have at most {{0}} items', [], $this->language),
250+
'error_maxLength' => \Yii::t('json-editor', 'Value must be at most {{0}} characters long', [], $this->language),
251+
'error_maxProperties' => \Yii::t('json-editor', 'Object must have at most {{0}} properties', [], $this->language),
252+
'error_maximum_excl' => \Yii::t('json-editor', 'Value must be less than {{0}}', [], $this->language),
253+
'error_maximum_incl' => \Yii::t('json-editor', 'Value must be at most {{0}}', [], $this->language),
254+
'error_minItems' => \Yii::t('json-editor', 'Value must have at least {{0}} items', [], $this->language),
255+
'error_minLength' => \Yii::t('json-editor', 'Value must be at least {{0}} characters long', [], $this->language),
256+
'error_minProperties' => \Yii::t('json-editor', 'Object must have at least {{0}} properties', [], $this->language),
257+
'error_minimum_excl' => \Yii::t('json-editor', 'Value must be greater than {{0}}', [], $this->language),
258+
'error_minimum_incl' => \Yii::t('json-editor', 'Value must be at least {{0}}', [], $this->language),
259+
'error_multipleOf' => \Yii::t('json-editor', 'Value must be a multiple of {{0}}', [], $this->language),
260+
'error_not' => \Yii::t('json-editor', 'Value must not validate against the provided schema', [], $this->language),
261+
'error_notempty' => \Yii::t('json-editor', 'Value required', [], $this->language),
262+
'error_notset' => \Yii::t('json-editor', 'Property must be set', [], $this->language),
263+
'error_oneOf' => \Yii::t('json-editor', 'Value must validate against exactly one of the provided schemas. It currently validates against {{0}} of the schemas.', [], $this->language),
264+
'error_pattern' => \Yii::t('json-editor', 'Value must match the pattern {{0}}', [], $this->language),
265+
'error_required' => \Yii::t('json-editor', 'Object is missing the required property \"{{0}}\"', [], $this->language),
266+
'error_time' => \Yii::t('json-editor', 'Time must be in the format {{0}}', [], $this->language),
267+
'error_type' => \Yii::t('json-editor', 'Value must be of type {{0}}', [], $this->language),
268+
'error_type_union' => \Yii::t('json-editor', 'Value must be one of the provided types', [], $this->language),
269+
'error_uniqueItems' => \Yii::t('json-editor', 'Array must have unique items', [], $this->language),
270+
'flatpickr_clear_button' => \Yii::t('json-editor', 'Clear', [], $this->language),
271+
'flatpickr_toggle_button' => \Yii::t('json-editor', 'Toggle', [], $this->language),
272+
];
273+
274+
return Json::encode($labels);
275+
276+
}
277+
211278
/**
212279
* Register additional languages via app language
213280
*/
214281
public function registerAdditionalLanguages()
215282
{
283+
216284
$view = $this->getView();
217285
$view->registerJs("
218-
JSONEditor.defaults.languages['" . $this->language . "'] = {
219-
button_add_row_title: '" . \Yii::t('json-editor', 'Add {{0}}', [], $this->language) . "',
220-
button_collapse: '" . \Yii::t('json-editor', 'Collapse', [], $this->language) . "',
221-
button_copy_row_title_short: '" . \Yii::t('json-editor', 'Copy', [], $this->language) . "',
222-
button_delete_all: '" . \Yii::t('json-editor', 'All', [], $this->language) . "',
223-
button_delete_all_title: '" . \Yii::t('json-editor', 'Delete All', [], $this->language) . "',
224-
button_delete_last: '" . \Yii::t('json-editor', 'Last {{0}}', [], $this->language) . "',
225-
button_delete_last_title: '" . \Yii::t('json-editor', 'Delete Last {{0}}', [], $this->language) . "',
226-
button_delete_node_warning: '" . \Yii::t('json-editor', 'Are you sure you want to remove this node?', [], $this->language) . "',
227-
button_delete_row_title: '" . \Yii::t('json-editor', 'Delete {{0}}', [], $this->language) . "',
228-
button_delete_row_title_short: '" . \Yii::t('json-editor', 'Delete', [], $this->language) . "',
229-
button_expand: '" . \Yii::t('json-editor', 'Expand', [], $this->language) . "',
230-
button_move_down_title: '" . \Yii::t('json-editor', 'Move down', [], $this->language) . "',
231-
button_move_up_title: '" . \Yii::t('json-editor', 'Move up', [], $this->language) . "',
232-
button_object_properties: '" . \Yii::t('json-editor', 'Object Properties', [], $this->language) . "',
233-
choices_placeholder_text: '" . \Yii::t('json-editor', 'Start typing to add value', [], $this->language) . "',
234-
default_array_item_title: '" . \Yii::t('json-editor', 'item', [], $this->language) . "',
235-
error_additionalItems: '" . \Yii::t('json-editor', 'No additional items allowed in this array', [], $this->language) . "',
236-
error_additional_properties: '" . \Yii::t('json-editor', 'No additional properties allowed, but property {{0}} is set', [], $this->language) . "',
237-
error_anyOf: '" . \Yii::t('json-editor', 'Value must validate against at least one of the provided schemas', [], $this->language) . "',
238-
error_date: '" . \Yii::t('json-editor', 'Date must be in the format {{0}}', [], $this->language) . "',
239-
error_datetime_local: '" . \Yii::t('json-editor', 'Datetime must be in the format {{0}}', [], $this->language) . "',
240-
error_dependency: '" . \Yii::t('json-editor', 'Must have property {{0}}', [], $this->language) . "',
241-
error_disallow: '" . \Yii::t('json-editor', 'Value must not be of type {{0}}', [], $this->language) . "',
242-
error_disallow_union: '" . \Yii::t('json-editor', 'Value must not be one of the provided disallowed types', [], $this->language) . "',
243-
error_enum: '" . \Yii::t('json-editor', 'Value must be one of the enumerated values', [], $this->language) . "',
244-
error_hostname: '" . \Yii::t('json-editor', 'The hostname has the wrong format', [], $this->language) . "',
245-
error_invalid_epoch: '" . \Yii::t('json-editor', 'Date must be greater than 1 January 1970', [], $this->language) . "',
246-
error_ipv4: '" . \Yii::t('json-editor', 'Value must be a valid IPv4 address in the form of 4 numbers between 0 and 255, separated by dots', [], $this->language) . "',
247-
error_ipv6: '" . \Yii::t('json-editor', 'Value must be a valid IPv6 address', [], $this->language) . "',
248-
error_maxItems: '" . \Yii::t('json-editor', 'Value must have at most {{0}} items', [], $this->language) . "',
249-
error_maxLength: '" . \Yii::t('json-editor', 'Value must be at most {{0}} characters long', [], $this->language) . "',
250-
error_maxProperties: '" . \Yii::t('json-editor', 'Object must have at most {{0}} properties', [], $this->language) . "',
251-
error_maximum_excl: '" . \Yii::t('json-editor', 'Value must be less than {{0}}', [], $this->language) . "',
252-
error_maximum_incl: '" . \Yii::t('json-editor', 'Value must be at most {{0}}', [], $this->language) . "',
253-
error_minItems: '" . \Yii::t('json-editor', 'Value must have at least {{0}} items', [], $this->language) . "',
254-
error_minLength: '" . \Yii::t('json-editor', 'Value must be at least {{0}} characters long', [], $this->language) . "',
255-
error_minProperties: '" . \Yii::t('json-editor', 'Object must have at least {{0}} properties', [], $this->language) . "',
256-
error_minimum_excl: '" . \Yii::t('json-editor', 'Value must be greater than {{0}}', [], $this->language) . "',
257-
error_minimum_incl: '" . \Yii::t('json-editor', 'Value must be at least {{0}}', [], $this->language) . "',
258-
error_multipleOf: '" . \Yii::t('json-editor', 'Value must be a multiple of {{0}}', [], $this->language) . "',
259-
error_not: '" . \Yii::t('json-editor', 'Value must not validate against the provided schema', [], $this->language) . "',
260-
error_notempty: '" . \Yii::t('json-editor', 'Value required', [], $this->language) . "',
261-
error_notset: '" . \Yii::t('json-editor', 'Property must be set', [], $this->language) . "',
262-
error_oneOf: '" . \Yii::t('json-editor', 'Value must validate against exactly one of the provided schemas. It currently validates against {{0}} of the schemas.', [], $this->language) . "',
263-
error_pattern: '" . \Yii::t('json-editor', 'Value must match the pattern {{0}}', [], $this->language) . "',
264-
error_required: '" . \Yii::t('json-editor', 'Object is missing the required property \"{{0}}\"', [], $this->language) . "',
265-
error_time: '" . \Yii::t('json-editor', 'Time must be in the format {{0}}', [], $this->language) . "',
266-
error_type: '" . \Yii::t('json-editor', 'Value must be of type {{0}}', [], $this->language) . "',
267-
error_type_union: '" . \Yii::t('json-editor', 'Value must be one of the provided types', [], $this->language) . "',
268-
error_uniqueItems: '" . \Yii::t('json-editor', 'Array must have unique items', [], $this->language) . "',
269-
flatpickr_clear_button: '" . \Yii::t('json-editor', 'Clear', [], $this->language) . "',
270-
flatpickr_toggle_button: '" . \Yii::t('json-editor', 'Toggle', [], $this->language) . "'
271-
};
286+
JSONEditor.defaults.languages['" . $this->language . "'] = " . $this->getTranslatedEditorOptsJson() . ";
272287
JSONEditor.defaults.language = '" . $this->language . "';",
273288
\yii\web\View::POS_READY,
274289
'json-editor'

0 commit comments

Comments
 (0)