WebDialog and utf-8 Problem
-
Hello!
I've problems with the transfer of Unicode-characters from a WebDialog back to ruby.
The WebDialog-charset is utf-8.For example, I write the text "tető" into a inputbox and I'd like to transfer it to ruby.
I write:window.location="skp;export_command@" + text;
Ok that works. In Sketchup I get the text with
wd.add_action_callback("export_command") do |wd, args| ...
Now, my problem is that the text in 'args' is different than the originally one.
Instead of"tető".unpack('U*') [116, 101, 116, 337]
I get: [116, 101, 116, 111]
Is there a way to handle this correctly? My very dirty solution is to write the utf-8 value for each character via javascript into a string, e.g. "[116, 101, 116, 337]" and then send this sequence back to ruby. After that I build an array and make
array.pack('U*')
to get the correct text.
-
Are you sure you process everything as UTF-8 all the way through?
ASCII Byte 111 is "o". Something is lost here at some point.
As long as your HTML and Javascript is both in UTF-8 it should work fine. I've so far not seen this behaviour.
You use "U*" - but I find it easier to inspect the actual bytes "C*".
Where are your string "tető" originating from? From the ruby script? If so, is that by any chance encoded in ANSI?
I wonder if you feed the webdialog an ANSI string, which has different bytes for the characters outside the ASCII range which then get jumbled up in the Webdialoge.When you try in the Ruby Console the characters are UTF-8 encoded and therefore works fine, but if you then enter that same code into an ANSI encoded file you can easily get odd output.
Usually when you get unexpected characters it's some part of the string handling chain that isn't UTF-8. While making translation system for my last plugin I ran into issues where it turned out that one of the language files wasn't in UTF-8 and that caused havoc for everything else.
-
To successfully transfer UTF-8 (or any encoding) back and forth from Ruby to a WebDialog, the easiest approach is to convert your characters to printable hex in javascript and pass that back. Then, in Ruby, pack it. Works great. If you search this forum, I believe you'll find some examples.
Todd
-
When you do a callback from a webdialoge to ruby it is done by a URI string - which means that the data in the URI needs to be URI encoded. Quite possibly your character is converted here. Since that you have a 2048 character limit for URI's under Explorer I've been using hidden input elements to transfer data.
So instead of a callback with arguments, I do:
- Put the data I want transferred into an hidden input element.
- Do a callback to ruby.
- Receiving ruby method reads data from hidden input.
That should preserve your characters,
let you avoid escaping data in the URI string,
let you use any length of data. -
thanks for your advice, I will try your suggested methods.
Advertisement