【问题标题】:Yaws Cyrillic outputYaws 西里尔文输出
【发布时间】:2014-03-28 15:03:09
【问题描述】:

你能帮帮我吗? 在网页上,我需要打印这个:

第 1 队与第 2 队

第 3 队与第 4 队

第 5 队与第 6 队

第 7 队与第 8 队

我的代码:

<erl>
first_team_write(Head) ->
  {ehtml,
    %[{text,[], f("~p vs ", [Head])}]}.        % try
    [{p,[], [Val, " vs "]} || Val <- [Head]]}. % different
second_team_write(Head) ->
  {ehtml,
    %[{text,[], f("~p", Head)},                % ways
    [{p,[], [Head]},                           %
    {p,[], []}]}.

write_teams([], Num) ->
  Num;

write_teams([Head|Tail], Num) ->
if
  (Num rem 2) /= 0 ->
    first_team_write([Head]),
    io:format("~p vs ", [Head]),  %% debug
    write_teams(Tail, Num+1);
  (Num rem 2) == 0 ->
    second_team_write(Head),
    io:format("~p~n", [Head]),  %% debug
    write_teams(Tail, Num+1)
end.


out(A) ->
  application:start(odbc), 
  ConnString = 
  %"Driver={MySQL ODBC 5.2 ANSI Driver};" ++
  "Driver={MySQL ODBC 5.2 Unicode Driver};" ++
  "Server=127.0.0.1;Database=erandom;" ++ 
      "User=root;Password=1q2w3e;" ++ 
      "Option=3;" ++
      "CharSet=utf8;",
      {ok, Conn} = odbc:connect(ConnString, []), 
      % Cyrillic results
      Results = odbc:sql_query(Conn, "select team_name from teams where id in (select team_id from leagues where league_name = 'First League')"), 
      %{selected, [Selector], Results} = odbc:sql_query(Conn, "select team_name from teams where team_name in ('Sporting', 'Old School')"),
      %{selected, [Selector], Results} = odbc:sql_query(Conn, "select team_name from teams where id in (select team_id from leagues where league_name = 'First League')"),
      odbc:disconnect(Conn),
      application:stop(odbc),
      TeamList = element(3, Results),
      {ehtml,
        [{h4,[], "Пары этого тура:"},
        {hr},
          write_teams(TeamList,1)
      ]}.

</erl>

结果:我的网页是空白的。 我究竟做错了什么? 我在调试控制台中看到:

1>
=INFO REPORT==== 25-Feb-2014::23:08:27 ===
    application: odbc
    exited: stopped
type: temporary
1> {[208,159,209,128,208,190,208,188,208,181,209,130,208,181,208,185]} vs 1> {[208,161,208,145,208,162]}
1> {[208,158,208,187,208,184,208,188,208,191]} vs 1> {[208,159,209,128,208,190,208,179,209,128,208,181,209,129,209,129]}
1> {[208,164,208,154,32,208,148,209,142,208,182,208,184,208,189,208,176]} vs 1> {"Old school"}
1> {[208,161,208,191,208,176,209,128,209,130,208,176,208,186]} vs 1> {[208,160,208,190,209,130,208,190,209,128]}
1> {"Sporting"} vs 1> {[208,161,208,190,208,191,209,128,208,190,209,130,208,184,208,178,208,187,208,  181,208,189,208,184,208,181]}

【问题讨论】:

    标签: yaws erlangweb


    【解决方案1】:

    我相信您在 HTTP 响应中没有正确指定字符集。首先,由于您的 .yaws 页面中嵌入了西里尔文文本,请确保您的编辑器将其保存为 UTF-8。如何完成这个取决于你的开发环境和编辑器,但我使用的是 emacs,所以我只是在 .yaws 文件的顶部添加了以下 HTML 注释:

    <!-- -*- coding: utf-8 -*- -->
    

    下一个问题是在响应的 HTTP 标头中正确设置字符集。为此,请在您的回复中适当地设置 Content-Type 标头,如下所示:

    [{header, {content_type, erase}},
     {header, {content_type, "text/html; charset=UTF-8"}},
     {ehtml,
      [{h4,[], "Пары этого тура:"},
       {hr},
          write_teams(TeamList,1)
      ]}].
    

    请注意,整个返回类型是一个 Erlang 列表。列表中的第一个元组会删除 Yaws 默认应用于 .yaws 页面的默认 Content-Type 标头。列表中的第二个元组指定了 Content-Type 的正确值,包括 UTF-8 字符集。列表的第三个元素与您在上面的问题陈述中指定的 ehtml 元组相同。

    顺便说一句,您可以通过运行 Yaws configure 脚本以 UTF-8 作为默认字符集构建 Yaws,如下所示:

    configure --with-defaultcharset=UTF-8
    

    之后,像往常一样使用make 重建 Yaws。如果这样做,则不再需要用于设置回复的 Content-Type 的两个元组,因为 UTF-8 将是所有回复的默认字符集。

    【讨论】:

    • 谢谢。重建偏航对我有用。除了官方文档,我在哪里可以阅读 yaws 中的输出功能?我需要例子。不明白它是如何工作的。
    • 您可以阅读各种输出功能here on the Yaws website。您可以在 Yaws 源代码树的 www 目录中找到这些示例。
    猜你喜欢
    • 1970-01-01
    • 2013-01-19
    • 2013-08-06
    • 1970-01-01
    • 1970-01-01
    • 2012-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多