他のウェブサイトにDiscourseを埋め込む際のウムラウトの問題

ここに記載されているように (Embed Discourse comments on another website via Javascript - #453 by limetti)、Discourse を私のウェブサイトに埋め込むと、タイトルは正しく解析されます。しかし、ウムラウトが含まれているため、「Ich würde」のようなタイトルが「Ich würde」になってしまいます。

これは一般的な問題ですか、それとも私のページの問題ですか、あるいは回避策はありますか?よろしくお願いします!

これは典型的な「不正なコーデック」の問題です。

テストケースとして、投稿の生データを(ここではPythonで)読み込むと、次のようになります。

In [1]: import urllib

In [2]: u = urllib.request.urlopen('https://meta.discourse.org/posts/1418409/raw')

In [3]: r = u.read(); r
Out[3]: b'As described here (https://meta.discourse.org/t/embed-discourse-comments-on-another-website-via-javascript/31963/453), when embedding Discourse into my website, the title is correctly parsed. But as it contains umlauts, titles like \\xe2\\x80\\x9cIch w\\xc3\\xbcrde\\xe2\\x80\\x9d end up in \\xe2\\x80\\x9cIch w\\xc3\\x83\\xc2\\xbcrde\\xe2\\x80\\x9d.\\n\\nIs this a general problem, a problem with my page or any workaround for that? Thanks!'

バイトを取得しますが、それをどのようにデコードすればよいかわかりません。しかし、レスポンスヘッダーの1つがUTF-8を使用すべきであることを示しています。

In [4]: u.headers['content-type']
Out[4]: 'text/plain; charset=utf-8'

In [5]: r.decode('utf-8')
Out[5]: 'As described here (https://meta.discourse.org/t/embed-discourse-comments-on-another-website-via-javascript/31963/453), when embedding Discourse into my website, the title is correctly parsed. But as it contains umlauts, titles like “Ich würde” end up in “Ich würde”.\\n\\nIs this a general problem, a problem with my page or any workaround for that? Thanks!'

In [6]: print(r.decode('utf-8'))
As described here (https://meta.discourse.org/t/embed-discourse-comments-on-another-website-via-javascript/31963/453), when embedding Discourse into my website, the title is correctly parsed. But as it contains umlauts, titles like “Ich würde” end up in “Ich würde”.

Is this a general problem, a problem with my page or any workaround for that? Thanks!

投稿した文字とまったく同じように見えることに注意してください。しかし、これらのバイトの誤った解釈が行われると、特にISO-8859-1ではなくUTF-8として解釈するという一般的な間違いが犯されると(文字列は簡潔にするために短縮されています)、次のようになります。

In [7]: snippet = r[220:255]; snippet
Out[7]: b'titles like \\xe2\\x80\\x9cIch w\\xc3\\xbcrde\\xe2\\x80\\x9d end up'

In [8]: snippet.decode('utf-8')
Out[8]: 'titles like “Ich würde” end up'

In [9]: snippet.decode('iso-8859-1')
Out[9]: 'titles like â\x80\x9cIch würdeâ\x80\x9d end up'

これをprintすると、ターミナルがハングします。すごい。:smiley:

要約すると、Discourseから投稿データを取得するために使用しているものは、utf-8ではなくiso-8859-1として扱っています。

(推測)おそらく、Discourseサイトから取得した生のバイトを、iso-8859-1のコードページで提供されているページに埋め込んでいるのでしょう。

「いいね!」 3

ヒントをいただき、大変ありがとうございました。確かに、UTF-8のメタタグがタイトルタグの後になっていました :wink:

これで動作するようになりました!

「いいね!」 1

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.