Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Basic translation support

  • Loading branch information...
commit 8aeb1a0dd385746702a7815f48aaee8c3162a764 1 parent 86f1254
Sébastien Bourdeauducq sbourdeauducq authored
29 include/mtki18n.h
View
@@ -0,0 +1,29 @@
+/*
+ * MTK
+ * Copyright (C) 2011 Sebastien Bourdeauducq
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, version 3 of the License.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef __MTK_INCLUDE_MTKI18N_H_
+#define __MTK_INCLUDE_MTKI18N_H_
+
+struct mtk_i18n_entry {
+ const char *original;
+ const char *translated;
+};
+
+void mtk_set_language(struct mtk_i18n_entry *table);
+const char *mtk_translate(char *original);
+
+#endif /* __MTK_INCLUDE_MTKI18N_H_ */
4 include/mtklib.h
View
@@ -23,8 +23,8 @@
#define EVENT_TYPE_USER_BASE 1000
typedef struct command_event {
- int type; /* must be EVENT_TYPE_COMMAND */
- char *cmd; /* command string */
+ int type; /* must be EVENT_TYPE_COMMAND */
+ char *cmd; /* command string */
} command_event;
typedef struct motion_event {
17 lib/button.c
View
@@ -16,6 +16,7 @@ struct button;
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
+#include <mtki18n.h>
#include "mtkstd.h"
#include "button.h"
#include "gfx_macros.h"
@@ -36,7 +37,8 @@ static struct userstate_services *userstate;
static struct messenger_services *msg;
struct button_data {
- char *text;
+ char *text; /* translated */
+ char *text_original; /* non translated */
s16 style;
s16 font_id;
s16 tx, ty; /* text position inside the button */
@@ -319,7 +321,7 @@ static void but_updatepos(BUTTON *b)
*/
static void but_free_data(BUTTON *b)
{
- if (b->bd->text) free(b->bd->text);
+ if (b->bd->text_original) free(b->bd->text_original);
}
@@ -349,13 +351,14 @@ static char *but_get_type(BUTTON *b)
static void but_set_text(BUTTON *b, char *new_txt)
{
- if (b->bd->text) {
- free(b->bd->text);
+ if (b->bd->text_original) {
+ free(b->bd->text_original);
}
if (new_txt != NULL)
- b->bd->text = strdup(new_txt);
+ b->bd->text_original = strdup(new_txt);
else
- b->bd->text = NULL;
+ b->bd->text_original = NULL;
+ b->bd->text = (char *)mtk_translate(b->bd->text_original);
/*
* If a button's size is completely free, a change
@@ -373,7 +376,7 @@ static void but_set_text(BUTTON *b, char *new_txt)
static char *but_get_text(BUTTON *b)
{
- return b->bd->text;
+ return b->bd->text_original;
}
46 lib/i18n.c
View
@@ -0,0 +1,46 @@
+/*
+ * MTK
+ * Copyright (C) 2011 Sebastien Bourdeauducq
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, version 3 of the License.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <string.h>
+#include <stdio.h>
+#include <mtki18n.h>
+
+static struct mtk_i18n_entry *current_table;
+
+void mtk_set_language(struct mtk_i18n_entry *table)
+{
+ current_table = table;
+ /* TODO: notify widgets */
+}
+
+const char *mtk_translate(char *original)
+{
+ int i;
+
+ if(original == NULL)
+ return NULL;
+ if(current_table == NULL)
+ return original;
+ i = 0;
+ while(current_table[i].original != NULL) {
+ if(strcmp(current_table[i].original, original) == 0)
+ return current_table[i].translated;
+ i++;
+ }
+ printf("Warning: no translation found for '%s'\n", original);
+ return original;
+}
18 lib/label.c
View
@@ -16,6 +16,7 @@ struct label;
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
+#include <mtki18n.h>
#include "mtkstd.h"
#include "gfx.h"
#include "widget_data.h"
@@ -32,9 +33,10 @@ static struct fontman_services *font;
static struct script_services *script;
struct label_data {
- char *text;
+ char *text; /* translated */
+ char *text_original; /* non-translated */
s32 font_id;
- s32 tx, ty; /* text position inside the label cell */
+ s32 tx, ty; /* text position inside the label cell */
VARIABLE *var;
};
@@ -108,7 +110,7 @@ static void lab_updatepos(LABEL *l)
*/
static void lab_free_data(LABEL *l)
{
- if (l->ld->text) free(l->ld->text);
+ if (l->ld->text_original) free(l->ld->text_original);
}
@@ -128,15 +130,16 @@ static char *lab_get_type(LABEL *l)
static void lab_set_text(LABEL *l, char *new_txt)
{
if ((!l) || (!l->ld)) return;
- if (l->ld->text) free(l->ld->text);
- l->ld->text = strdup(new_txt);
+ if (l->ld->text_original) free(l->ld->text_original);
+ l->ld->text_original = strdup(new_txt);
+ l->ld->text = (char *)mtk_translate(l->ld->text_original);
l->wd->update |= WID_UPDATE_MINMAX;
}
static char *lab_get_text(LABEL *l)
{
- return l->ld->text;
+ return l->ld->text_original;
}
@@ -234,7 +237,8 @@ static LABEL *create(void)
SET_WIDGET_DEFAULTS(new, struct label, &lab_methods);
/* set label specific attributes */
- new->ld->text = strdup("");
+ new->ld->text_original = strdup("");
+ new->ld->text = "";
update_text_pos(new);
gen_methods.update(new);
7 lib/libmtk-generic.mk
View
@@ -16,7 +16,7 @@ CFLAGS += -Wall -O9 -MMD -g
SRC_C += \
appman.c background.c button.c \
cache.c clipping.c container.c \
- conv_fnt.c mtkstd.c entry.c \
+ conv_fnt.c mtkstd.c entry.c \
fontman.c frame.c gfx.c \
gfx_img16.c gfx_img32.c \
widman.c conv_tff.c grid.c \
@@ -26,14 +26,13 @@ SRC_C += \
window.c screen.c script.c \
scrollbar.c tick.c \
tokenizer.c userstate.c variable.c \
- scope.c \
+ scope.c i18n.c clipboard.c \
bigmouse.c default_fnt.c main.c \
smallmouse.c mono_fnt.c title_fnt.c \
scrdrv.c eventmsg.c \
sharedmem.c gfx_scr16.c scheduler.c \
vera16_tff.c vera20_tff.c edit.c \
- separator.c pixmap.c list.c \
- clipboard.c
+ separator.c pixmap.c list.c
vpath % $(LIBMTK_DIR)
2  lib/window.c
View
@@ -384,7 +384,7 @@ static void win_set_app_id(WINDOW *w, s32 app_id)
winlayout->set_win_title(w->wind->elem, app_name);
/* register window title at screen */
- if (scr) scr->scr->set_title(scr, w, app_name);;
+ if (scr) scr->scr->set_title(scr, w, app_name);
}
if (orig_set_app_id) orig_set_app_id((WIDGET *)w, app_id);
Please sign in to comment.
Something went wrong with that request. Please try again.