SFML community forums

Help => General => Topic started by: Sumzary on May 22, 2013, 07:36:28 pm

Title: Getting special letters from xml to the screen properly
Post by: Sumzary 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:
(https://legacy.sfmluploads.org/cache/pics/360_text.png)

So, how would I fix this problem of mine? :]
Thanks in advance.
Title: Re: Getting special letters from xml to the screen properly
Post by: Laurent 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);
}
Title: Re: Getting special letters from xml to the screen properly
Post by: Sumzary 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