How to show utf8 text with snap and heist?

前端 未结 1 1655
别那么骄傲
别那么骄傲 2020-12-22 00:40

I have used writeBS writeText from Snap and renderTemplate from heist but none of them seems to support unicode.

site          


        
相关标签:
1条回答
  • 2020-12-22 00:48

    The problem here is not with writeBS or writeText. It's with the conversion used by the OverloadedStrings extension. It is also important to understand the distinction between ByteString and Text. ByteString is for raw bytes. There is no concept of characters or an encoding. That is where Text comes in. The Data.Text.Encoding module has a bunch of functions for converting between Text and ByteString using different encodings. For me, both of the following generate the same output:

    writeBS $ encodeUtf8 "你好世界"
    writeText "你好世界"
    

    The reason your code didn't work is because your string literal is being converted to ByteString by the OverloadedStrings extension, and it is not giving you the behavior you want. The solution is to treat it as the proper type...Text.

    On the Heist side of things, the following works fine for me:

    route [("test", cRender "test")]
    

    In fact, this one renders correctly in my browser, while the previous two don't. The difference is that cRender sets an appropriate content-type. I found it enlightening to observe the differences using the following snippet.

    site = route [ ("/test1", writeBS "你好世界")
                 , ("/test2", writeBS $ encodeUtf8 "你好世界")
                 , ("/test3", writeText "你好世界")
                 , ("/test4", modifyResponse (setContentType "text/html;charset=utf-8") >> writeText "你好世界")
                 , ("/testHeist", cRender "test")
                 ]
    

    In my browser test4 and testHeist work correctly. Tests 2 and 3 give you the correct behavior but might not be rendered properly by browsers because of the lack of content-type.

    0 讨论(0)
提交回复
热议问题