0

I am using GNU gettext for translations and it works. The following test code shows the idea of my implementation:

#include <iostream>
#include <stdio.h>
#include <stdlib.h>

#include <libintl.h>
#include <locale.h>

#define _(STRING) gettext(STRING)

int main()
{
  /* Setting the i18n environment */
  setlocale (LC_ALL, "");
  bindtextdomain ("hello", getenv("PWD"));
  textdomain ("hello");

  /* Example of i18n usage */
  std::cout << _("Hello World!") << std::endl;

  setenv("LANGUAGE", "fr", 1);

  std::cout << _("Hello World!") << std::endl;

  return EXIT_SUCCESS;
}

I have a working .mo file, so when I run the program I get:

Hello World!
Bonjour le monde!

So far so good. But I have to forward the translated strings to a 3rd party application, and there I need to indicate the encoding (Latin 1, Latin 9, Cyrillic, UTF-8, etc).

How can I get the encoding at run time?

MikeCAT
  • 73,922
  • 11
  • 45
  • 70
  • The C++ locale object provides no means of determining the locale's encoding. This is possible only by using C library/POSIX functions. – Sam Varshavchik Jun 23 '21 at 12:21
  • Take a look on [that answer](https://stackoverflow.com/a/67819605/1387438) basically standard library is design to set encoding of strings globally (for application and all libraries). And stream encoding is defined separately - conversion is done silently if needed. How do you `forward the translated strings to a 3rd party application`? – Marek R Jun 23 '21 at 12:22
  • @MarekR Strings are forwarded in objects. A string object which holds the byte representation of the string (char array). The string object references a font object which holds the encoding. Some older consoles only supports Latin 1 and Latin 9, others supports a few more charsets, and the most recent will support UTF-16. So I need to figure out how the convert the gettext() output to a supported charset, and that would be easier if I knew the charset which the translator had used. – Henrik Bøgelund Jun 23 '21 at 13:14
  • What do you use to send data from one application to the other? Is this some kind of `std:ostream`? – Marek R Jun 23 '21 at 13:23
  • @MarekR. No. It is sent to another device via CAN (Controller Area Network). There is a defined protocol, which I cannot change, so I need to be able to find the encoding. Alternatively I could make sure that all translations are done using UTF-8, and then analyze the text-strings when a new language is selected. That would require a means to loop through all text strings in the .mo file, But I can't find a way to do that either. – Henrik Bøgelund Jun 23 '21 at 21:05
  • Surely gettext transparently converts the loaded translations to your current charset, doesn't it? So did you actually experience an issue with `gettext` returning strings in different charsets depending on the `.mo`? – Lauri Nurmi Jun 29 '21 at 10:49

1 Answers1

0

Trying to find out the encoding in use for translated strings is a little bit of guess work. But you can enforce a certain encoding by calling bind_textdomain_codeset(DOMAINNAME, CODESET), see https://man7.org/linux/man-pages/man3/bind_textdomain_codeset.3.html.

Guido Flohr
  • 1,871
  • 15
  • 28