Hello, I would like some advice regarding the country flags in the dropdown menu. I found this tutorial which I hope is correct.
As a newbie programmer, I’ve never done anything like this and do not fully understand any part of code, so I don’t know if I’m on the right way.
So I have new helper with this:
defmodule CookieWeb.FlagHelpers do
@flag_offset 127397
def get_flag(country_code) when byte_size(country_code) == 2 do
<<s1::utf8, s2::utf8>> = String.upcase(country_code)
<<(s1 + @flag_offset)::utf8, (s2 + @flag_offset)::utf8>>
end
def get_flag(country_code), do: country_code
end
In cookie_web.ex I have:
import CookieWeb.FlagHelpers
def available_languages, do: Application.get_env(:cookie, :available_languages)
|> Enum.map_join(&Flags.get_flag()/1)
available_languages I have define here in config.exs:
config :cookie,
available_languages: ~w(en sk)a
Menu I have define in html.heex here:
<div id="lang-menu-bar" class="hidden origin-top-right absolute right-0 mt-2 w-48 rounded-md shadow-lg py-1 bg-white ring-1 ring-black ring-opacity-5 focus:outline-none" role="menu" aria-orientation="vertical" aria-labelledby="user-menu-button" tabindex="-1">
<%= for lang <- available_languages() do %>
<%= link Gettext.gettext(CookieWeb.Gettext, to_string(lang)), to: Routes.lang_path(@conn, :set, lang), class: "block px-4 py-2 text-sm text-gray-700 hover:bg-gray-100 #{if lang == @current_user.default_lang, do: 'font-bold'}", role: "menuitem", tabindex: -1 %>
<% end %>
</div>
Now I have this error:
My Controller is here:
defmodule CookieWeb.LangController do
use CookieWeb, :controller
def set(conn, %{"lang" => lang}) do
Cookie.Accounts.update_user_default_lang(
conn.assigns.current_user,
String.to_existing_atom(lang)
)
case Plug.Conn.get_req_header(conn, "referer") do
[referer] ->
redirect(conn, external: referer)
_ ->
redirect(conn, to: "/")
end
end
end
If I’m on the right way, what do I need to do next to project the national flags into the menu.
Thank you for any help and answer!