Jump to:

3444 Posts in 1030 Topics by 871 members

Template Questions

SilverStripe Forums » Template Questions » I18N how to avoid trouble with special chars

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 405 Views
  • jaaf
    Avatar
    Community Member
    24 Posts

    I18N how to avoid trouble with special chars Link to this post

    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?

  • Devlin
    Avatar
    Community Member
    215 Posts

    Re: I18N how to avoid trouble with special chars Link to this post

    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);

  • jaaf
    Avatar
    Community Member
    24 Posts

    Re: I18N how to avoid trouble with special chars Link to this post

    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.

    405 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.