0

I have created a contract using solidity which has a comment field. I want to provide multi language support so that user can insert and retrieve comment in any language.

Please suggest how we can achieve this feature?

1 Answers1

1

Instead of using just a single string to store the comment, you will have to use an array, struct or mapping to store all the translations. For example, you could create a mapping of two-letter language codes to the versions of the comment in that language:

mapping (bytes2 => string) languageCodeToComment;

Now, you can add a comment in multiple langauges:

languageCodeToComment["EN"] = "This is the English comment!";

languageCodeToComment["NL"] = "Dit is het Nederlandse bericht!";

languageCodeToComment["FY"] = "Dit is yn it Frysk!";

Jesbus
  • 10,478
  • 6
  • 35
  • 62
  • Thanks Jesse, How to insert and get comment in Chinese language? – vaibhav Oct 25 '17 at 08:09
  • @vaibhav You can use the same technique: languageCodeToComment["ZH"] = "漢字"; – Jesbus Oct 25 '17 at 08:55
  • I understand it. but when i fetch Chinese value, it returns hexa value. how to convert hexa to chinese ? – vaibhav Oct 25 '17 at 10:36
  • @vaibhav Can you paste here what it returns? I hadn't tested non-ASCII characters before, but it seems to work fine. Check out this test contract I just deployed: https://etherscan.io/address/0x6df5df1583fa3f31d7599503d56b4fe6925e0cb9#readContract – Jesbus Oct 25 '17 at 11:15
  • @vaibhav Read this https://ethereum.stackexchange.com/questions/28344/should-i-use-bytes-or-string-always, solidity stores strings as sequence of bytes. They suggest converting your string to utf-8 encoding before storing, and decoding from utf-8 to your string type. For example javascript strings are utf-16 so you have to convert to utf-8 before storing them. – Ismael Oct 26 '17 at 05:52