Embedding unicode string literals in your cpp files

Hi Folks,

Thought I’d pitch in on this old thread, as I’ve been unpicking all the UTF-8 / font issues for myself while localising Wotja.

To compile for Windows, such that the Source Code is compiled as UTF-8, simply define this compiler flag:
/utf-8

You can then do stuff like this (provided you’re compiling for C++17 or later).
auto myString = juce::String::fromUTF8(u8"放松");

That is Simplified Chinese, BTW.

This code works 100% fine for iOS, macOS, Windows and Android.

To force use of the correct font, implement something like this in your LookAndFeel class.
Sorry about the layout :slight_smile:

This works for all of iOS, macOS, Windows and Android (at least for me, anyhow!).
You’d have to expand it to handle other locales/fonts.

virtual juce::Typeface::Ptr getTypefaceForFont (const juce::Font& font) override {

  #if defined(IM_TARGET_IOS)
  juce::String locale =  juce::SystemStats::getDisplayLanguage();

  if (locale.startsWith("zh")) {
    juce::Font f(font);
    f.setTypefaceName("PingFang SC");
    return juce::Typeface::createSystemTypefaceFor(f);
  }

  #elif defined(IM_TARGET_MACOS)

  juce::String locale =  juce::SystemStats::getDisplayLanguage();

  if (locale.startsWith("zh")) {
    juce::Font f(font);
    f.setTypefaceName("PingFang SC");
    return juce::Typeface::createSystemTypefaceFor(f);
  }

  #elif defined(IM_TARGET_WINDOWS)

  // See https://forum.juce.com/t/font-woes-again-chinese/23832

  juce::String locale =  juce::SystemStats::getDisplayLanguage();

  if (locale.startsWith("zh")) {
    juce::Font f(font);
    f.setTypefaceName("Microsoft YaHei");
    return juce::Typeface::createSystemTypefaceFor(f);
  }
  #endif //

  return lookAndFeel.juce::LookAndFeel_V3::getTypefaceForFont(font);
}

Pete

3 Likes