Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Getting special letters from xml to the screen properly  (Read 880 times)

0 Members and 1 Guest are viewing this topic.

Sumzary

  • Guest
Getting special letters from xml to the screen properly
« on: May 22, 2013, 07:36:28 pm »
I have this in a language file encoded in UTF-8:
<common
        main_menu_start="Begin"
        main_menu_arcade="Arcade"
        main_menu_options="Options"
        main_menu_about="About"
        main_menu_quit="Quit"
        about_made_by="A game by:"
        about_rasmus="Rasmus Riiner - Programming and the rest"
        about_mona="Mona Liinat - Background Images"
        about_oliver="Oliver Kütt - Music and Support"
        loading="Loading..." />

These are the relevant bits in my code:
const sf::String Game::GetText(const char *category, const char *line) {
   TiXmlElement *Category = langFile.FirstChildElement(category);
   sf::String string = Category->Attribute(line);
   return string;
}

/.../

aboutOliver.setString(Game::GetText("common", "about_oliver"));

The outcome of this code:


So, how would I fix this problem of mine? :]
Thanks in advance.
« Last Edit: May 22, 2013, 08:14:01 pm by Sumzary »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Getting special letters from xml to the screen properly
« Reply #1 on: May 22, 2013, 08:28:06 pm »
sf::String doesn't know that your char* is UTF-8, it expects it to be encoded with the current locale. You must explicitly convert from UTF-8 to UTF-32, which sf::String can understand out-of-the-box.

sf::String utf8ToSfString(const char* source, size_t size)
{
    std::basic_string<sf::Uint32> utf32;
    sf::Utf8::toUtf32(source, source + size, std::back_inserter(utf32));
    return sf::String(utf32);
}
Laurent Gomila - SFML developer

Sumzary

  • Guest
Re: Getting special letters from xml to the screen properly
« Reply #2 on: May 22, 2013, 08:45:36 pm »
This is the working code now:
const sf::String Game::GetText(const char *category, const char *line) {
   TiXmlElement *Category = langFile.FirstChildElement(category);
   const char *text = Category->Attribute(line);
   std::basic_string<sf::Uint32> utf32;
   
   sf::Utf8::toUtf32(text, text + strlen(text), std::back_inserter(utf32));
   return sf::String(utf32);
}
 

Thanks a whole bunch Laurent for the quick reply, you're the bestestest guy around >:D