C program example

  • Sample codes for C Language to show deference between non-I18N and I18n.
  • Simple application to get string from prompt and show that character-length with current date.

#define _(String) gettext(String)
#define N_(String) (String)


int
main (int argc, char *argv[])
{
    
  //char instr[256];
  wchar_t instr[256];
 
  FILE *infp = stdin;
 
  time_t t;
  struct tm* TM;
 
  char tstr[256];
  ...

 

...
  setlocale(LC_ALL ,"");    
  bindtextdomain("sample", ".");
  textdomain("sample");

 
  //fgets(instr, 256, infp);
  fgetws(instr, 256, infp);
 
  fclose(infp);  
 
  //printf("Character length is  %d\n", strlen(instr)-1);
  printf(_(N_("Character length is  %d\n")), wcslen(instr)-1);
  ...
...
  t= time(NULL);
  TM = localtime(&t);
 
  strftime(tstr, sizeof(tstr), nl_langinfo(D_T_FMT), TM);
 
  //printf("Current Date is  %s\n", tstr);  
  printf(_(N_("Current Date is  %s\n")), tstr);

 return 0;

}

Result of execution: 

***Non-I18n***
  export LANG=en_US.UTF-8
  aaa
  Character length is  3
  Current Date is  Tue Dec  4 16:51:20 2007
  export LANG=ja_JP.UTF-8
 あああ
   Character length is  9
  Current Date is  Tue Dec  4 16:53:00 2007
***I18N***
 export LANG=en_US.UTF-8
 aaa
 Character length is  3
 Current Date is  Tue 04 Dec 2007 04:54:45 PM JST
 export LANG=ja_JP.UTF-8
 あああ
 文字数は  3
 現在の日付は  2007年12月04日 16時55分38秒