Publi

Traducciones para nuestras aplicaciones C/C++ con gettext

4929705941_5cc3aca475_oHace un tiempo publiqué cómo traducir nuestras aplicaciones PHP con gettext, ahora le toca el turno a nuestras aplicaciones en C.

El procedimiento es muy parecido, por lo que no voy a especificar la creación de ficheros po y mo en este artículo, os remito al anterior.

El código fuente no es que varíe demasiado de PHP pero es interesante echarle un ojo. Si queremos traducir este programa:

1
2
3
4
5
6
7
8
#include <stdio.h>

int main(int argc, char *argv[])
{
  printf("Hello world\n");
 
  return 0;
}

Haremos algo parecido a lo siguiente:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <stdio.h>
#include <libintl.h>
#include <locale.h>

int main(int argc, char *argv[])
{
  char *locale="fr_FR.UTF-8";
  setlocale(LC_MESSAGES, locale);
  bindtextdomain("miProyecto", "locale/" );
  textdomain("miProyecto");

  printf(gettext("Hello world"));
 
  return 0;
}

Ahora, si tenemos el archivo locale/fr_FR/LC_MESSAGES/miProyecto.mo dentro, veremos el mensaje en francés. Si en lugar de imponer el francés, queremos detectar la locale del sistema, en el setlocale podemos poner:

1
setlocale(LC_MESSAGES, "");

Y si queremos saber el nombre de la locale actual:

1
2
char *currentlocale=setlocale(LC_MESSAGES, "");
printf("Locale actual: %s\n", currentlocale);

Foto: Martin Pilat (Flickr CC-by)

También podría interesarte....

There are 7 comments left Ir a comentario

  1. Pingback: Bitacoras.com /

  2. Pingback: Scripts multilingües en bash con gettext / Traducciones en scripts – Poesía Binaria /

  3. Alex Reynolds /
    Usando Google Chrome Google Chrome 118.0.0.0 en Windows Windows NT

    I just wanted to let you know that I just looked at your website and found it to be very interesting and helpful quordle

  4. orabelle hana /
    Usando Google Chrome Google Chrome 119.0.0.0 en Windows Windows NT

    To relax and have fun visit our website flagle

  5. Shweta Malik /
    Usando Google Chrome Google Chrome 120.0.0.0 en Windows Windows NT

    It is the best time to fulfil your wildest fantasies and dreams. Every man craves a hot and romantic date with a beautiful girl and if you have not done this yet, then goes for Escorts in Connaught Place . It is the place where you can choose the call girls of your type and go on a long drive. Many people lack confidence in them due to which they hesitate to contact girls. Connaught Place call girls are confident, bold, intelligent and educated as well.

  6. Vandana Rajput /
    Usando Google Chrome Google Chrome 120.0.0.0 en Windows Windows NT

    Hence, the Call Girls Service in Greater Noida are the ones you should meet as soon as possible as they are the Call girls that you desire the most. You don’t know this fact yet but once you visit our gallery, you would know why we as so bold to make such a statement.

  7. Ahana /
    Usando Google Chrome Google Chrome 121.0.0.0 en Windows Windows NT

    Many agencies do such a good job that their Escorts are very beautiful, which will fascinate you in the first instance, but here Andheri Escorts will explain to you that you will not be able to tell about the service of someone; for this, you have to talk to us. If we said that a Escorts is right for you, it means that we want to give you the best test by a Andheri call girl because we have been in this market for a long time and have more experience than you as a call girl. You should have a friendly attitude towards them.

Leave a Reply