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?

2
  • 1
    Can you please post more details? Oct 25, 2017 at 6:53
  • I want to insert and retrieve Chinese language.
    – vaibhav
    Oct 25, 2017 at 8:11

1 Answer 1

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!";

5
  • Thanks Jesse, How to insert and get comment in Chinese language?
    – vaibhav
    Oct 25, 2017 at 8:09
  • @vaibhav You can use the same technique: languageCodeToComment["ZH"] = "漢字";
    – Jesbus
    Oct 25, 2017 at 8:55
  • I understand it. but when i fetch Chinese value, it returns hexa value. how to convert hexa to chinese ?
    – vaibhav
    Oct 25, 2017 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: etherscan.io/address/…
    – Jesbus
    Oct 25, 2017 at 11:15
  • @vaibhav Read this ethereum.stackexchange.com/questions/28344/…, 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, 2017 at 5:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.