/*
 * i18ntest - internalizzazione e localizzazione, uso caratteri Unicode
 *
 * (c) Copyright 2005, Francesco Groccia <groccia.f?gmail.com>
 * <http://fgr.altervista.org/>
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 	1. Redistributions of source code must retain the above
 * 	   copyright notice, this list of conditions and the following
 * 	   disclaimer.
 *
 *	2. Redistributions in binary form must reproduce the above
 *	   copyright notice, this list of conditions and the following
 *	   disclaimer in the documentation and/or other materials
 *	   provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

/* compilazione: gcc i18ntest.c -std=c99 -Wall -o i18ntest */

#include <stdio.h>
#include <stdlib.h>
#include <locale.h>
#include <langinfo.h>
#include <monetary.h>
#include <time.h>
#include <string.h>
#include <wchar.h>
#include <alloca.h>
#include <libintl.h>

#define PACKAGE   "i18ntest"
#define LOCALEDIR "."

void print(const char* msg);
void datetime();
void money(double amount);
void wide_string(const char* word);
/////////////////////////////////////////////

int main(int argc, char **argv)
{
        // impostazione della localizzazione
    setlocale(LC_ALL, "");
    printf("\n");
    print("la data corrente è "); datetime();
    print("la valuta è "); money(1936.27);
    wide_string("à");
    printf("\n");

    return 0;
}
/////////////////////////////////////////////

void
print(const char* msg)
{
        // configurazione di gettext
    bindtextdomain(PACKAGE, LOCALEDIR);
    textdomain(PACKAGE);
    printf("%s: ", gettext(msg));
}
/////////////////////////////////////////////

void
datetime()
{
	char buf[256];
    time_t now = time(NULL);

	strftime(buf, sizeof(buf), nl_langinfo(D_T_FMT), localtime(&now));
	printf("%s\n", buf);
}
/////////////////////////////////////////////

void
money(double amount)
{
    char buf[256];

    strfmon(buf, sizeof(buf), "%.2n", amount);
    printf("%s\n", buf);
}
/////////////////////////////////////////////

void
wide_string(const char* word)
{
    wchar_t* wbuf;
    size_t wsize;
    size_t size = strlen(word) + 1;

    wbuf = (wchar_t*) alloca(size * sizeof(wchar_t));
        // converte la stringa in un array di wchar_t e ne conta il numero
    if(( wsize = mbstowcs(wbuf, word, wsize )) == -1 ) {
        perror("errore conversione wchar. Controlla impostazioni della lingua!");
        return;
    }
    printf("%s : bytes=%u chars=%u\n", wbuf, size, wsize);
}
