Re: problem with g_locale_to_utf8()



On Fri, 2003-08-29 at 12:28, Paulo Ricardo Batista Mesquita wrote:
> Hello guys,
> 
> I am writing an application that receives data in portuguese language, it runs both Linux (English) and Windows (Brazilian Portuguese).
> 
> In the beginning, I was writing the code using non English characters, especially when using message boxes. That time, it was working fine.
> 
> When I started to accentuate the words, I got some errors messages telling me that PANGO don't accept non UTF-8 characters. Since then, I started to use g_locale_to_utf8() function.
> 
> When running my application in Windows, it works fine. But, when I tested in Linux, in the message boxes where I am using this function, the text doens't appears, just appears an empty space (I think).
> 
> I am researching for an explanation to this, but I didn't get it yet. May someone give me a help?
> 

This utf business is a little tricky. For example, if you use
g_locale_to_utf8 but your locale is not set right (not uncommon) the
function won't work as you expect since it will not obtain a valide
codeset to do the translation. Here is a small wrapper program I use you
might find handy. The locale-defined codeset can be overridden by the
environment variable SMB_CODESET:

gchar *my_utf_string(char *t)
{
    static gchar *s = NULL;
    GError *error = NULL;
    gsize r_bytes, w_bytes;
    unsigned char *c;
    const char *fc;
    gchar *from_codeset=NULL;
    
    if(!t) g_assert_not_reached();   
    if (g_utf8_validate (t,-1,NULL))  return t;   
    
    /* so we got a non-UTF-8 */
    
    if (getenv("SMB_CODESET") && strlen( getenv("SMB_CODESET"))){
	    from_codeset=g_strdup(getenv("SMB_CODESET"));
    }
    else {
    	g_get_charset(&fc);
    	if (fc) from_codeset = g_strdup(fc);
    	else from_codeset = g_strdup("ISO-8859-1");
    }
    
    if (!strcmp(from_codeset,"ISO-")){
	    g_free(from_codeset);
	    from_codeset = g_strdup("ISO-8859-1");
    }    
    if(s) g_free(s);

    for(c = (unsigned char *)t; *c != 0; c++)
	if(*c < 32 && *c != '\n')
	    *c = ' ';
    s = g_convert (t,strlen(t),"UTF-8",from_codeset,&r_bytes, &w_bytes,
&error);
    g_free(from_codeset);

    if(!s)
    {
	s=g_strdup(t);
	for(c = s; *c != 0; c++) if(*c > 128) *c = '?';
    }
    if(error){
        printf("DBG: %s. Codeset for system is: %s\n",
			error->message,from_codeset);
        printf("DBG: You should set the environment variable SMB_CODESET
to ISO-8859-1\n");
	g_error_free(error);
    }
    return s;
}

Edscott

> Thanks,
> 
> Paulo Ricardo
> _______________________________________________
> gtk-list mailing list
> gtk-list gnome org
> http://mail.gnome.org/mailman/listinfo/gtk-list




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]