Home | History | Annotate | Download | only in nls
      1 #include "config.h"
      2 
      3 #include "i18n.h"
      4 #include <locale.h>
      5 
      6 #include <stdio.h>
      7 #include <string.h>
      8 
      9 struct _testcase {
     10   char *locale;
     11   char *untranslated;
     12   char *expected;
     13 };
     14 
     15 typedef struct _testcase testcase;
     16 
     17 /* Translators: Just ignore the stuff in the test subdirectory. */
     18 static testcase testcases[] = {
     19   { "de_DE.UTF-8",
     20     N_("[DO_NOT_TRANSLATE_THIS_MARKER]"),
     21        "[DO_NOT_TRANSLATE_THIS_MARKER_de]" },
     22   { "C",
     23     N_("[DO_NOT_TRANSLATE_THIS_MARKER]"),
     24     N_("[DO_NOT_TRANSLATE_THIS_MARKER]") },
     25 };
     26 
     27 int main(int argc, char *argv[])
     28 {
     29   char *localedir;
     30   int i;
     31 
     32   if (argc != 2) {
     33     puts("Syntax: test-nls <localedir>\n");
     34     return 1;
     35   }
     36 
     37   localedir = argv[1];
     38 
     39   do {
     40     const char *newloc = setlocale(LC_ALL, NULL);
     41     printf("Default locale: %s\n", newloc);
     42   } while (0);
     43 
     44 
     45   for (i=0; i < sizeof(testcases)/sizeof(testcases[0]); i++) {
     46     char *locale       = testcases[i].locale;
     47     char *untranslated = testcases[i].untranslated;
     48     char *expected     = testcases[i].expected;
     49     char *translation;
     50 
     51     if (1) {
     52       printf("setlocale(\"%s\")\n", locale);
     53       const char *actual_locale = setlocale(LC_MESSAGES, locale);
     54       if (actual_locale == NULL) {
     55 	fprintf(stderr, "Error: Cannot set locale to %s.\n", locale);
     56 	return 4;
     57       }
     58       printf("new locale: %s\n", actual_locale);
     59     }
     60 
     61     if (1) {
     62       const char *basedir = bindtextdomain(GETTEXT_PACKAGE, localedir);
     63       printf("message basedir: %s\n", basedir);
     64     }
     65 
     66     if (1) {
     67       const char *domain = textdomain(GETTEXT_PACKAGE);
     68       printf("message domain: %s\n", domain);
     69     }
     70 
     71     if (1) {
     72       const char *codeset = bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8");
     73       printf("message codeset: %s\n", codeset);
     74     }
     75 
     76     puts("before translation");
     77     translation = gettext(untranslated);
     78     puts("after translation");
     79 
     80     if (strcmp(expected, translation) != 0) {
     81       fprintf(stderr,
     82 	      "locale:       %s\n"
     83 	      "localedir:    %s\n"
     84 	      "untranslated: %s\n"
     85 	      "expected:     %s\n"
     86 	      "translation:  %s\n"
     87 	      "Error: translation != expected\n",
     88 	      locale,
     89 	      localedir,
     90 	      untranslated,
     91 	      expected,
     92 	      translation);
     93 
     94       return 1;
     95     } else {
     96       fprintf(stderr,
     97 	      "expected:     %s\n"
     98 	      "translation:  %s\n"
     99 	      "Match!\n",
    100 	      expected,
    101 	      translation);
    102     }
    103   }
    104   return 0;
    105 }
    106