Blender V2.61 - r43446

blf_translation.c

Go to the documentation of this file.
00001 /*
00002  * ***** BEGIN GPL LICENSE BLOCK *****
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License
00006  * as published by the Free Software Foundation; either version 2
00007  * of the License, or (at your option) any later version. 
00008  *
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software Foundation,
00016  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00017  *
00018  * The Original Code is Copyright (C) 2011 Blender Foundation.
00019  * All rights reserved.
00020  *
00021  * Contributor(s): Blender Foundation,
00022  *                 Sergey Sharybin
00023  *
00024  * ***** END GPL LICENSE BLOCK *****
00025  */
00026 
00031 #include <stdlib.h>
00032 #include <string.h>
00033 
00034 #ifdef WITH_INTERNATIONAL
00035 #include <libintl.h>
00036 #include <locale.h>
00037 
00038 #define GETTEXT_CONTEXT_GLUE "\004"
00039 
00040 /* needed for windows version of gettext */
00041 #ifndef LC_MESSAGES
00042 #   define LC_MESSAGES 1729
00043 #endif
00044 
00045 #endif
00046 
00047 #include "MEM_guardedalloc.h"
00048 
00049 #include "BLI_utildefines.h"
00050 #include "BLI_path_util.h"
00051 #include "BLI_string.h"
00052 #include "BLI_path_util.h"
00053 #include "BLI_fileops.h"
00054 
00055 #include "BLF_translation.h"
00056 
00057 #include "DNA_userdef_types.h" /* For user settings. */
00058 
00059 #ifdef WITH_INTERNATIONAL
00060 static const char unifont_filename[]="droidsans.ttf.gz";
00061 static unsigned char *unifont_ttf= NULL;
00062 static int unifont_size= 0;
00063 
00064 unsigned char *BLF_get_unifont(int *unifont_size_r)
00065 {
00066     if(unifont_ttf==NULL) {
00067         char *fontpath = BLI_get_folder(BLENDER_DATAFILES, "fonts");
00068         if (fontpath) {
00069             char unifont_path[1024];
00070 
00071             BLI_snprintf(unifont_path, sizeof(unifont_path), "%s/%s", fontpath, unifont_filename);
00072 
00073             unifont_ttf= (unsigned char*)BLI_file_ungzip_to_mem(unifont_path, &unifont_size);
00074         }
00075         else {
00076             printf("%s: 'fonts' data path not found for international font, continuing\n", __func__);
00077         }
00078     }
00079 
00080     *unifont_size_r= unifont_size;
00081 
00082     return unifont_ttf;
00083 }
00084 
00085 void BLF_free_unifont(void)
00086 {
00087     if(unifont_ttf)
00088         MEM_freeN(unifont_ttf);
00089 }
00090 
00091 #endif
00092 
00093 const char* BLF_gettext(const char *msgid)
00094 {
00095 #ifdef WITH_INTERNATIONAL
00096     if( msgid[0] )
00097         return gettext( msgid );
00098     return "";
00099 #else
00100     return msgid;
00101 #endif
00102 }
00103 
00104 const char *BLF_pgettext(const char *context, const char *message)
00105 {
00106 #ifdef WITH_INTERNATIONAL
00107     char static_msg_ctxt_id[1024];
00108     char *dynamic_msg_ctxt_id = NULL;
00109     char *msg_ctxt_id;
00110     const char *translation;
00111 
00112     size_t overall_length = strlen(context) + strlen(message) + sizeof(GETTEXT_CONTEXT_GLUE) + 1;
00113 
00114     if (overall_length > sizeof(static_msg_ctxt_id)) {
00115         dynamic_msg_ctxt_id = malloc(overall_length);
00116         msg_ctxt_id = dynamic_msg_ctxt_id;
00117     }
00118     else {
00119         msg_ctxt_id = static_msg_ctxt_id;
00120     }
00121 
00122     sprintf(msg_ctxt_id, "%s%s%s", context, GETTEXT_CONTEXT_GLUE, message);
00123 
00124     translation = (char*)dcgettext(TEXT_DOMAIN_NAME, msg_ctxt_id, LC_MESSAGES);
00125 
00126     if (dynamic_msg_ctxt_id)
00127         free(dynamic_msg_ctxt_id);
00128 
00129     if (translation == msg_ctxt_id)
00130         translation = message;
00131 
00132     return translation;
00133 #else
00134     (void)context;
00135     return message;
00136 #endif
00137 }
00138 
00139 int BLF_translate_iface(void)
00140 {
00141 #ifdef WITH_INTERNATIONAL
00142     return (U.transopts & USER_DOTRANSLATE) && (U.transopts & USER_TR_IFACE);
00143 #else
00144     return 0;
00145 #endif
00146 }
00147 
00148 int BLF_translate_tooltips(void)
00149 {
00150 #ifdef WITH_INTERNATIONAL
00151     return (U.transopts & USER_DOTRANSLATE) && (U.transopts & USER_TR_TOOLTIPS);
00152 #else
00153     return 0;
00154 #endif
00155 }
00156 
00157 const char *BLF_translate_do_iface(const char *msgid)
00158 {
00159 #ifdef WITH_INTERNATIONAL
00160     if(BLF_translate_iface())
00161         return BLF_gettext(msgid);
00162     else
00163         return msgid;
00164 #else
00165     return msgid;
00166 #endif
00167 }
00168 
00169 const char *BLF_translate_do_tooltip(const char *msgid)
00170 {
00171 #ifdef WITH_INTERNATIONAL
00172     if(BLF_translate_tooltips())
00173         return BLF_gettext(msgid);
00174     else
00175         return msgid;
00176 #else
00177     return msgid;
00178 #endif
00179 }