yawserlangweb

Yaws Cyrillic output


Can you help me please? At the web-page, I need print this:

team 1 vs team 2

team 3 vs team 4

team 5 vs team 6

team 7 vs team 8

My code:

<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>

Result: My web page is blank. What am I doing wrong? I see in the debug console:

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]}

Solution

  • I believe you're not specifying the charset correctly in your HTTP response. First, since you have Cyrillic text embedded in your .yaws page, make sure your editor is saving it as UTF-8. How you accomplish this depends on your development environment and editor, but I use emacs, so I just added the following HTML comment to the top of the .yaws file:

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

    The next problem is getting the charset set correctly in the HTTP headers of the response. To do that, set the Content-Type header appropriately in your reply like this:

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

    Note that the whole return type is an Erlang list. The first tuple in the list erases the default Content-Type header that Yaws applies by default to .yaws pages. The second tuple in the list specifies the correct value for Content-Type including the UTF-8 charset. The third element of the list is the same ehtml tuple you specified in your problem statement above.

    BTW you can build Yaws with UTF-8 as the default charset by running the Yaws configure script as shown below:

    configure --with-defaultcharset=UTF-8
    

    After that, rebuild Yaws with make as usual. If you do this, the two tuples for setting the Content-Type of your reply are no longer needed, since UTF-8 would be the default charset for all replies.