2006-01-04 20:51:02 +08:00
|
|
|
/** \file translate.c
|
|
|
|
|
|
|
|
Translation library, internally uses catgets
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
2006-02-28 21:17:16 +08:00
|
|
|
|
2006-01-04 20:51:02 +08:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2006-01-19 02:37:58 +08:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <wchar.h>
|
|
|
|
|
|
|
|
#if HAVE_LIBINTL_H
|
2006-01-04 20:51:02 +08:00
|
|
|
#include <libintl.h>
|
2006-01-19 02:37:58 +08:00
|
|
|
#endif
|
2006-01-04 20:51:02 +08:00
|
|
|
|
|
|
|
#include "common.h"
|
2006-02-28 21:17:16 +08:00
|
|
|
#include "fallback.h"
|
2006-01-04 20:51:02 +08:00
|
|
|
#include "util.h"
|
2006-02-28 21:17:16 +08:00
|
|
|
|
2006-02-09 23:50:20 +08:00
|
|
|
#include "halloc_util.h"
|
2006-01-04 20:51:02 +08:00
|
|
|
|
|
|
|
#if HAVE_GETTEXT
|
|
|
|
|
2006-01-24 04:40:14 +08:00
|
|
|
/**
|
|
|
|
Number of string_buffer_t in the ring of buffers
|
|
|
|
*/
|
2006-01-04 20:51:02 +08:00
|
|
|
#define BUFF_COUNT 64
|
|
|
|
|
2006-01-24 04:40:14 +08:00
|
|
|
/**
|
|
|
|
The ring of string_buffer_t
|
|
|
|
*/
|
2006-01-04 20:51:02 +08:00
|
|
|
static string_buffer_t buff[BUFF_COUNT];
|
2006-01-24 04:40:14 +08:00
|
|
|
/**
|
|
|
|
Current position in the ring
|
|
|
|
*/
|
2006-01-04 20:51:02 +08:00
|
|
|
static int curr_buff=0;
|
|
|
|
|
2006-01-24 04:40:14 +08:00
|
|
|
/**
|
|
|
|
Buffer used by translate_wcs2str
|
|
|
|
*/
|
2006-01-05 21:41:59 +08:00
|
|
|
static char *wcs2str_buff=0;
|
2006-01-24 04:40:14 +08:00
|
|
|
/**
|
|
|
|
Size of buffer used by translate_wcs2str
|
|
|
|
*/
|
2006-01-05 21:41:59 +08:00
|
|
|
static size_t wcs2str_buff_count=0;
|
|
|
|
|
2006-06-20 08:50:10 +08:00
|
|
|
/**
|
|
|
|
Flag to tell whether the translation library has been initialized
|
|
|
|
*/
|
2006-02-04 21:09:14 +08:00
|
|
|
static int is_init = 0;
|
|
|
|
|
2006-06-20 08:50:10 +08:00
|
|
|
/**
|
|
|
|
Internal shutdown function. Automatically called on shutdown if the library has been initialized.
|
|
|
|
*/
|
2006-02-09 23:50:20 +08:00
|
|
|
static void internal_destroy()
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if( !is_init )
|
|
|
|
return;
|
|
|
|
|
|
|
|
is_init = 0;
|
|
|
|
|
|
|
|
for(i=0; i<BUFF_COUNT; i++ )
|
|
|
|
sb_destroy( &buff[i] );
|
|
|
|
|
|
|
|
free( wcs2str_buff );
|
|
|
|
}
|
|
|
|
|
2006-06-20 08:50:10 +08:00
|
|
|
/**
|
|
|
|
Internal init function. Automatically called when a translation is first requested.
|
|
|
|
*/
|
2006-02-04 21:09:14 +08:00
|
|
|
static void internal_init()
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
is_init = 1;
|
|
|
|
|
|
|
|
for(i=0; i<BUFF_COUNT; i++ )
|
2006-02-09 23:50:20 +08:00
|
|
|
{
|
2006-02-04 21:09:14 +08:00
|
|
|
sb_init( &buff[i] );
|
2006-02-09 23:50:20 +08:00
|
|
|
}
|
|
|
|
halloc_register_function_void( global_context, &internal_destroy );
|
2006-02-04 21:09:14 +08:00
|
|
|
|
|
|
|
bindtextdomain( PACKAGE_NAME, LOCALEDIR );
|
|
|
|
textdomain( PACKAGE_NAME );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-01-24 04:40:14 +08:00
|
|
|
/**
|
|
|
|
Wide to narrow character conversion. Internal implementation that
|
|
|
|
avoids exessive calls to malloc
|
|
|
|
*/
|
|
|
|
static char *translate_wcs2str( const wchar_t *in )
|
2006-01-05 21:41:59 +08:00
|
|
|
{
|
|
|
|
size_t len = MAX_UTF8_BYTES*wcslen(in)+1;
|
|
|
|
if( len > wcs2str_buff_count )
|
|
|
|
{
|
|
|
|
wcs2str_buff = realloc( wcs2str_buff, len );
|
|
|
|
if( wcs2str_buff == 0 )
|
|
|
|
{
|
2006-07-03 18:39:57 +08:00
|
|
|
DIE_MEM();
|
2006-01-05 21:41:59 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
wcstombs( wcs2str_buff,
|
|
|
|
in,
|
|
|
|
MAX_UTF8_BYTES*wcslen(in)+1 );
|
|
|
|
|
|
|
|
return wcs2str_buff;
|
|
|
|
}
|
|
|
|
|
2006-01-04 20:51:02 +08:00
|
|
|
const wchar_t *wgettext( const wchar_t *in )
|
|
|
|
{
|
2006-01-08 10:56:56 +08:00
|
|
|
if( !in )
|
|
|
|
return in;
|
2006-02-04 21:09:14 +08:00
|
|
|
|
|
|
|
if( !is_init )
|
|
|
|
internal_init();
|
|
|
|
|
2006-01-05 21:41:59 +08:00
|
|
|
char *mbs_in = translate_wcs2str( in );
|
2006-01-04 20:51:02 +08:00
|
|
|
char *out = gettext( mbs_in );
|
|
|
|
wchar_t *wres=0;
|
|
|
|
|
|
|
|
sb_clear( &buff[curr_buff] );
|
2006-01-05 21:41:59 +08:00
|
|
|
|
2006-01-04 20:51:02 +08:00
|
|
|
sb_printf( &buff[curr_buff], L"%s", out );
|
|
|
|
wres = (wchar_t *)buff[curr_buff].buff;
|
|
|
|
curr_buff = (curr_buff+1)%BUFF_COUNT;
|
|
|
|
|
|
|
|
return wres;
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
const wchar_t *wgettext( const wchar_t *in )
|
|
|
|
{
|
|
|
|
return in;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|