1

Im using php and slickgrid to show a datagrid structure..everything works fine.but my site is multilanguage when i change to japanese language..i have this error on the java script side ,the data is send from php as json

SyntaxError: unterminated string literal
{ id: "Lang_length", name: "最大値以上の長さ << error here

I have done utf8_encode and htmlspecialchars for the content.but still it is breaking

How to properly encode japanese character with php

data im getting from php side for slickgrid

 var columns = [
{ id: "go_to", name: "行ã", field: "go_to", formatter: htmlFormatter, sortable: true, width:100 },
{ id: "product_name", name: "製å“", field: "product_name", formatter: htmlFormatter, sortable: true, width: 400 },
{ id: "product_description", name: "記述", field: "product_description", formatter: htmlFormatter, sortable: true, width: 475 }

Update for @pekka Code on the Php side

 if(strtolower($this->view->title)=="packaging materials")
            {
             $pahead_det = '{ id: "go_to", name: "' . PackAssist_Locale::translate('Go to') . '", field: "go_to", formatter: htmlFormatter, sortable: true,width: 75 },
                { id: "product_name", name: "' . PackAssist_Locale::translate('Group/Part') . '", field: "product_name", formatter: htmlFormatter, sortable: true, width: 400  },
                { id: "product_description", name: "' . PackAssist_Locale::translate('Description') . '", field: "product_description", formatter: htmlFormatter, sortable: true, width: 500  },';
            }else{
                $pahead_det = '{ id: "go_to", name: "' . PackAssist_Locale::translate('Go to') . '", field: "go_to", formatter: htmlFormatter, sortable: true, width: 75 },
                { id: "product_name", name: "' . PackAssist_Locale::translate('Group/Part') . '", field: "product_name", formatter: htmlFormatter, sortable: true, width: 400  },
                { id: "product_description", name: "' . PackAssist_Locale::translate('Description') . '", field: "product_description", formatter: htmlFormatter, sortable: true, width: 500  },';
            }

            if (strtolower($this->view->title) == "packaging materials") {

                $checkin = array();
                foreach ($mergearray as $m) {
                    if (isset($m['part_id'])) {
                        $getpropertyhead = $propertygrp_model->gridListPropertyHead($m['part_group_id']);
                        if (count($getpropertyhead) > 0) {
                            foreach ($getpropertyhead as $property) {
                                array_push($property_data_list, $property->pg_pd_id);
                                if (!in_array($property->pd_name_uniquename, $checkin)) {   //build the string with check to avoid redundancy
                                    $pahead_det .= '{ id: "' . $property->pd_name_uniquename . '", name: "' . $property->language_data_content . '", field: "' . $property->pd_name_uniquename . '", sortable: true, width: 150,formatter: htmlFormatter },';
                                }
                                array_push($checkin, $property->pd_name_uniquename);
                            }
                        }
                    }
                }
            }


            $this->view->pahead_det = substr($pahead_det, 0, -1); //header
coolguy
  • 7,866
  • 9
  • 45
  • 71
  • Can you show how you are feeding the data to the grid? If what you show is JSON data, non-ASCII characters should show up escaped. Maybe the string is just missing a closing `"` though? – Pekka Jul 17 '13 at 11:40
  • @Pekka웃 its in json format and it comes from the database – coolguy Jul 17 '13 at 11:41
  • but it works fine in english ,dutch and german languages (same data) – coolguy Jul 17 '13 at 11:42
  • Please show the code you are using to feed it; also can you show the full JS code (1 line above, 1 line below the error)? – Pekka Jul 17 '13 at 11:43
  • sure ill ..ill edit the question – coolguy Jul 17 '13 at 11:43
  • Cool. also can you show 1 line above, 1 line below the error you quote above? – Pekka Jul 17 '13 at 11:43
  • The code you show seems broken? `製` and such. Is this what shows up in the source code? Can you show the PHP code you are using to generate the JSON? – Pekka Jul 17 '13 at 11:47
  • @Pekka웃 Php code updated – coolguy Jul 17 '13 at 11:49
  • Im using Zend framework for the php side. – coolguy Jul 17 '13 at 11:50
  • Oh my gosh. Why don't you use `json_encode` ? – galymzhan Jul 17 '13 at 11:50
  • @galymzhan ..`json_encode` requires ajax to show things on the client side..well my client insists on that – coolguy Jul 17 '13 at 11:51
  • It appears your custom-built encoding function is somehow not outputting the data in the right encoding. It's impossible to say why with the info present, check out [UTF-8 all the way through](http://stackoverflow.com/q/279170) – Pekka Jul 17 '13 at 11:51
  • @uber I don't follow. `json_encode()` doesn't require Ajax. It does pretty much what your function does – Pekka Jul 17 '13 at 11:52
  • Well ill look n building it using `json_encode` and will work on it thanks @Pekka웃 and @galyzman – coolguy Jul 17 '13 at 11:54
  • so just build the array with key and index and encode it like `json_encode($array)` and pass it to the client side right @Pekka웃 ? – coolguy Jul 17 '13 at 11:56
  • You may still have to look at the question I link to above, because you may have an encoding problem that doesn't go away by using `utf8_encode()` alone. But oyu'll see that soon enough if it's the case. – Pekka Jul 17 '13 at 11:56
  • sure ill let you know..if things goes well...thanks for the support – coolguy Jul 17 '13 at 11:57
  • `utf8_encode()` converts from `ISO-8859-1` (aka `Latin-1`), which is designed for Western Europe. You can't encode Japanese in Latin-1! – Álvaro González Jul 18 '13 at 11:39

1 Answers1

1

Thanks all..i got the answer i used this function

 public static function characterEscape($str) {
        return addcslashes($str, "\\\'\"&\n\r<>");
    }
coolguy
  • 7,866
  • 9
  • 45
  • 71