Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

Template Questions /

I18N how to avoid trouble with special chars


Reply


3 Posts   531 Views

Avatar
jaaf

Community Member, 24 Posts

16 March 2014 at 2:47am

Edited: 16/03/2014 2:49am

Hi,

I have written a module to browse images. I use a div data-option attribute to pass parameters from the backend to the javascritpt. These options includes translated texts for a frontend user menu.
Here is how the template is written

   

<div id="param" data-options =' $getParams'></div>

in the controller the getParams function is like this:

function getParams(){
return (   '{"nbThumbPerRow": "'.$this->nbThumbsPerRow.'", '.
      '"nbThumbRows": "'.$this->nbRows.'", '.
      '"pauseTime": "'.$this->totalTime.'", '.
      '"transTime": "'.$this->transTime.'", '.
      '"menuTextCollapsed" : "'. _t("ImageBrowserPage.MENUTEXTCOLLAPSED").'", '.
      '"menuTextExpanded" : "'. _t("ImageBrowserPage.MENUTEXTEXPANDED").'", '.
      '"panelToggleText" : "'. _t("ImageBrowserPage.PANELTOGGLETEXT").'", '.
      '"menuSettingText1" : "'. _t("ImageBrowserPage.MENUSETTINGTEXT1").'" ,'.
      '"menuSettingText2" : "'. _t("ImageBrowserPage.MENUSETTINGTEXT2").'"}' );
}

All seems to work correctly. My only trouble is when a translated text (for example ImageBrowserPage.MENUSETTINGTEXT1) includes some special characters that are normal characters of the translated language as <'> which is common in French . This seems to break the template. I have tried to use addslashes before _t("ImageBrowserPage.MENUSETTINGTEXT1") but it doesn't change anything as \' appears as it is in the string.

How should I proceed to make the program insensitive to the introduction by a translator of such a char?

Avatar
Devlin

Community Member, 225 Posts

25 March 2014 at 4:52am

I recommend to use json_encode() to parse your JSON string and let PHP handle escaping of quotes.

http://www.php.net/manual/en/function.json-encode.php

e.g.

$params = array(
   'menuTextCollapsed' => _t("ImageBrowserPage.MENUTEXTCOLLAPSED"),
   'menuTextExpanded' => _t("ImageBrowserPage.MENUTEXTEXPANDED"),
);
$json = json_encode($params, JSON_HEX_QUOT | JSON_HEX_APOS);

Avatar
jaaf

Community Member, 24 Posts

25 March 2014 at 6:36am

Thank you for answer. At the momen I solved the trouble with

function getParams(){
return (   htmlentities('{"nbThumbPerRow": "'.$this->nbThumbsPerRow.'", '.
      '"nbThumbRows": "'.$this->nbRows.'", '.
      '"pauseTime": "'.$this->totalTime.'", '.
      '"transTime": "'.$this->transTime.'", '.
      '"menuTextCollapsed" : "'. _t("ImageBrowserPage.MENUTEXTCOLLAPSED").'", '.
      '"menuTextExpanded" : "'. _t("ImageBrowserPage.MENUTEXTEXPANDED").'", '.
      '"panelToggleText" : "'. _t("ImageBrowserPage.PANELTOGGLETEXT").'", '.
      '"menuSettingText1" : "'. _t("ImageBrowserPage.MENUSETTINGTEXT1").'" ,'.
      '"menuSettingText2" : "'. _t("ImageBrowserPage.MENUSETTINGTEXT2").'"}' ,ENT_QUOTES));
}

I will try you way also.