pebble
  • Tutorials
  • Get the SDK
  • Guides
  • Documentation
  • PebbleOS
  • Mobile App
  • Examples
  • Index 01
  • Community
  • Blog
  • More
Privacy
Cookies
Publish

PebbleOS

  • Overview
  • Start here
    • Getting started guide
    • Playground
  • Development
    • The pbl CLI
    • Build system
    • Samples
      • Threads
      • Mutexes
      • Semaphores
      • Message queues
      • Poll groups
      • Timers
      • Work queues
      • Delayed work
      • Heaps
      • Time
      • Idle states
      • Dining philosophers
      • Bounded buffer
      • Readers and writers
      • Sleeping barber
      • Priority inversion
      • Barrier
      • CRC
      • SHA-256
      • AES
      • Files
      • Settings
      • Translations
      • Input events
      • Loader
      • Log levels
      • Profiler
      • Shell commands
      • Task watchdog
      • Accelerometer
      • Ambient light
      • Backlight
      • Battery
      • Display
      • Flash
      • GPU
      • Magnetometer
      • Microphone
      • External RAM
      • Random numbers
      • Real-time clock
      • Speaker
      • Die temperature
      • Touchscreen
      • Vibration
      • Rust
      • Kernel benchmark
    • Emulator and simulator
    • Testing
    • Debugging
    • Conventions
  • System
    • Architecture overview
    • Kernel
    • Subsystems
    • Drivers
    • Services
    • Libraries
    • Third-party
    • Application SDK
  • Hardware
    • Boards
    • Programmers
  • Community
    • Contribution Guidelines
  • Reference
    • Reference
    • Kconfig options

Translations

This sample translates strings with a French gettext catalog, using internationalization. The build compiles po/fr_FR.po with msgfmt, and #embed puts the result in the image; a catalog is read through a callback, so it could as well be in flash or a file.

Strings are looked up by their English original, and those without a translation stay in English, as all do without a catalog. A context tells apart originals that are the same word: the noun and the verb Watch. pbl_i18n_get() caches translations for an owner until it frees them; pbl_i18n_get_with_buffer() copies one into a buffer instead, truncated to fit.

Run:

pbl samples samples/subsys/i18n/catalog

Boards:

every board

Sources

po/fr_FR.po
msgid ""
msgstr ""
"Project-Id-Version: 3\n"
"Language: fr_FR\n"
"Name: Français\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"

msgid "Hello"
msgstr "Bonjour"

msgid "Your watch is charging"
msgstr "Votre montre est en charge"

msgctxt "noun"
msgid "Watch"
msgstr "Montre"

msgctxt "verb"
msgid "Watch"
msgstr "Regarder"
src/main.c
#include <errno.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>

#include <pbl/i18n/i18n.h>

/* A gettext MO file, compiled from po/fr_FR.po at build time. */
static const uint8_t s_fr_mo[] = {
#embed "fr_FR.mo"
};

/* Catalogs are read through a callback, so they can live in flash, a file or memory. */
static int prv_read(void *data, uint32_t offset, void *buf, size_t length) {
  if (offset > sizeof(s_fr_mo) || length > sizeof(s_fr_mo) - offset) {
    return -EINVAL;
  }

  memcpy(buf, &s_fr_mo[offset], length);

  return 0;
}

static const struct pbl_i18n_catalog s_catalog = {
  .read = prv_read,
  .size = sizeof(s_fr_mo),
};

/* Translations from pbl_i18n_get() are cached per owner, here this file, until freed. */
static const int s_owner;

static void prv_print(const char *when) {
  printf("i18n: %s: %s / %s / %s / %s\n", when, pbl_i18n_get("Hello", &s_owner),
         pbl_i18n_ctx_get("noun", "Watch", &s_owner), pbl_i18n_ctx_get("verb", "Watch", &s_owner),
         pbl_i18n_get("Good night", &s_owner));
  pbl_i18n_free_all(&s_owner);
}

int main(void) {
  struct pbl_i18n_catalog_info info;
  char buf[16];
  int ret;

  prv_print("no catalog");

  ret = pbl_i18n_catalog_get_info(&s_catalog, &info);
  if (ret < 0) {
    fprintf(stderr, "i18n: not a catalog (%d)\n", ret);
    return ret;
  }
  printf("i18n: catalog %s, %s, version %u\n", info.locale, info.name, info.version);

  ret = pbl_i18n_set_catalog(&s_catalog);
  if (ret < 0) {
    fprintf(stderr, "i18n: catalog not set (%d)\n", ret);
    return ret;
  }
  printf("i18n: locale is now %s\n", pbl_i18n_get_locale());

  /* Strings without a translation stay in English. */
  prv_print("French catalog");

  /* No caching: the translation is copied into a buffer, truncated to fit. */
  pbl_i18n_get_with_buffer("Your watch is charging", buf, sizeof(buf));
  printf("i18n: %u characters, in %u bytes: %s\n",
         (unsigned int)pbl_i18n_get_length("Your watch is charging"), (unsigned int)sizeof(buf),
         buf);

  (void)pbl_i18n_set_catalog(nullptr);
  prv_print("catalog removed");

  printf("Sample done\n");

  return 0;
}
CMakeLists.txt
cmake_minimum_required(VERSION 3.20)

find_package(PebbleOS REQUIRED HINTS $ENV{PEBBLEOS_BASE})
project(i18n_catalog)

# The catalog is compiled from its .po and embedded into the image with #embed.
find_program(MSGFMT msgfmt REQUIRED)
add_custom_command(
  OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/fr_FR.mo
  COMMAND ${MSGFMT} --check -o ${CMAKE_CURRENT_BINARY_DIR}/fr_FR.mo
          ${CMAKE_CURRENT_SOURCE_DIR}/po/fr_FR.po
  DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/po/fr_FR.po
)
add_custom_target(i18n_catalog_mo DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/fr_FR.mo)
add_dependencies(app i18n_catalog_mo)

target_sources(app PRIVATE src/main.c)
target_compile_options(app PRIVATE --embed-dir=${CMAKE_CURRENT_BINARY_DIR})

Output

i18n: no catalog: Hello / Watch / Watch / Good night
i18n: catalog fr_FR, Français, version 3
i18n: locale is now fr_FR
i18n: French catalog: Bonjour / Montre / Regarder / Good night
i18n: 26 characters, in 16 bytes: Votre montre es
i18n: catalog removed: Hello / Watch / Watch / Good night
Sample done

Generated from PebbleOS 7753e801d. This page is maintained in the pebbleos repository: docs/samples/subsys/i18n/catalog.md.

Overview

  • Sources
  • Output