2013-06-18 23:39:50 +00:00
|
|
|
/*
|
|
|
|
* Rufus: The Reliable USB Formatting Utility
|
|
|
|
* Localization functions, a.k.a. "Everybody is doing it wrong but me!"
|
|
|
|
* Copyright © 2013 Pete Batard <pete@akeo.ie>
|
|
|
|
*
|
|
|
|
* 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, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Memory leaks detection - define _CRTDBG_MAP_ALLOC as preprocessor macro */
|
|
|
|
#ifdef _CRTDBG_MAP_ALLOC
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <crtdbg.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <windows.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <wchar.h>
|
|
|
|
#include <string.h>
|
2013-06-28 01:17:27 +00:00
|
|
|
#include <stddef.h>
|
2013-06-18 23:39:50 +00:00
|
|
|
|
|
|
|
#include "rufus.h"
|
2013-06-21 00:41:56 +00:00
|
|
|
#include "resource.h"
|
2013-06-18 23:39:50 +00:00
|
|
|
#include "msapi_utf8.h"
|
2013-06-21 23:40:48 +00:00
|
|
|
#include "localization.h"
|
2013-06-28 13:09:26 +00:00
|
|
|
#include "localization_data.h"
|
2013-06-21 00:41:56 +00:00
|
|
|
|
2013-07-07 01:12:05 +00:00
|
|
|
/* c control ID (no space, no quotes), s: quoted string, i: 32 bit signed integer, u: 32 bit unsigned CSV list */
|
2013-06-29 17:45:09 +00:00
|
|
|
// Remember to update the size of the array in localization.h when adding/removing elements
|
2013-07-07 01:12:05 +00:00
|
|
|
const loc_parse parse_cmd[9] = {
|
|
|
|
// Translation name and Windows LCIDs it should apply to
|
|
|
|
{ 'l', LC_LOCALE, "su" }, // l "English (US)" 0x0009,0x1009
|
|
|
|
// Base translation to add on top of (eg. "English (UK)" can be used to build on top of "English (US)"
|
|
|
|
{ 'b', LC_BASE, "s" }, // b "English (US)" // TODO: NOT IMPLEMENTED YET
|
|
|
|
// Version to use for the localization commandset and API
|
|
|
|
{ 'v', LC_VERSION, "ii" }, // v 1.0 // TODO: NOT IMPLEMENTED YET
|
|
|
|
// Translate the text control associated with an ID
|
|
|
|
{ 't', LC_TEXT, "cs" }, // t IDC_CONTROL "Translation"
|
|
|
|
// Set the parent dialog to which the next commands should apply.
|
|
|
|
// Use 'p NONE' for text data that is not bound to a dialog
|
|
|
|
{ 'p', LC_PARENT, "c" }, // p IDD_DIALOG
|
|
|
|
// Resize a dialog (dx dy pixel increment)
|
|
|
|
{ 'r', LC_RESIZE, "cii" }, // t IDC_CONTROL +10 +10
|
|
|
|
// Move a dialog (dx dy pixed displacement)
|
|
|
|
{ 'm', LC_MOVE, "cii" }, // t IDC_CONTROL -5 0
|
|
|
|
// Set the font to use for the text controls that follow
|
|
|
|
// Use f "Default" 0 to reset the font
|
|
|
|
{ 'f', LC_FONT, "si" }, // f "MS Dialog" 10
|
|
|
|
// Set the direction to use for the text controls that follow
|
|
|
|
// 0 = Left to right, 1 = Right to left
|
|
|
|
{ 'd', LC_DIRECTION, "i" }, // d 1 // TODO: NOT IMPLEMENTED YET
|
2013-06-18 23:39:50 +00:00
|
|
|
};
|
2013-07-07 01:12:05 +00:00
|
|
|
int loc_line_nr;
|
2013-06-21 00:41:56 +00:00
|
|
|
char loc_filename[32];
|
2013-07-27 22:16:11 +00:00
|
|
|
struct list_head locale_list = {NULL, NULL};
|
2013-06-18 23:39:50 +00:00
|
|
|
|
|
|
|
void free_loc_cmd(loc_cmd* lcmd)
|
|
|
|
{
|
|
|
|
if (lcmd == NULL)
|
|
|
|
return;
|
2013-07-07 01:12:05 +00:00
|
|
|
safe_free(lcmd->txt[0]);
|
|
|
|
safe_free(lcmd->txt[1]);
|
|
|
|
safe_free(lcmd->unum);
|
2013-06-18 23:39:50 +00:00
|
|
|
free(lcmd);
|
|
|
|
}
|
|
|
|
|
2013-06-28 01:17:27 +00:00
|
|
|
void loc_dlg_add(int index, loc_cmd* lcmd)
|
|
|
|
{
|
|
|
|
if ((lcmd == NULL) || (index < 0) || (index >= ARRAYSIZE(loc_dlg))) {
|
|
|
|
uprintf("loc_dlg_add: invalid parameter\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
list_add(&lcmd->list, &loc_dlg[index].list);
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: rename this to something_localization()
|
|
|
|
void free_loc_dlg(void)
|
|
|
|
{
|
|
|
|
size_t i = 0;
|
|
|
|
loc_cmd *lcmd, *next;
|
|
|
|
|
|
|
|
for (i=0; i<ARRAYSIZE(loc_dlg); i++) {
|
|
|
|
if (list_empty(&loc_dlg[i].list))
|
|
|
|
continue;
|
2013-07-27 22:16:11 +00:00
|
|
|
list_for_each_entry_safe(lcmd, next, &loc_dlg[i].list, loc_cmd, list) {
|
2013-06-28 01:17:27 +00:00
|
|
|
list_del(&lcmd->list);
|
|
|
|
free_loc_cmd(lcmd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-27 22:16:11 +00:00
|
|
|
void free_locale_list(void)
|
|
|
|
{
|
|
|
|
loc_cmd *lcmd, *next;
|
|
|
|
|
|
|
|
list_for_each_entry_safe(lcmd, next, &locale_list, loc_cmd, list) {
|
|
|
|
list_del(&lcmd->list);
|
|
|
|
free_loc_cmd(lcmd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-28 01:17:27 +00:00
|
|
|
/*
|
|
|
|
* We need to initialize the command lists
|
|
|
|
*/
|
|
|
|
void init_localization(void) {
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
for (i=0; i<ARRAYSIZE(loc_dlg); i++)
|
|
|
|
list_init(&loc_dlg[i].list);
|
2013-07-27 22:16:11 +00:00
|
|
|
list_init(&locale_list);
|
|
|
|
}
|
|
|
|
|
|
|
|
void exit_localization(void) {
|
|
|
|
free_loc_dlg();
|
|
|
|
free_locale_list();
|
2013-06-28 01:17:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Yada. Should be called during init
|
|
|
|
* if hDlg is NULL, we try to apply the commands against an active Window
|
|
|
|
* if dlg_id is negative, we try to apply all
|
|
|
|
*/
|
|
|
|
void apply_localization(int dlg_id, HWND hDlg)
|
|
|
|
{
|
|
|
|
loc_cmd* lcmd;
|
|
|
|
HWND hCtrl = NULL;
|
2013-06-28 13:09:26 +00:00
|
|
|
int id_start = IDD_DIALOG, id_end = IDD_DIALOG + ARRAYSIZE(loc_dlg);
|
2013-07-27 22:16:11 +00:00
|
|
|
LONG_PTR style;
|
|
|
|
BOOL left_to_right = FALSE;
|
|
|
|
|
2013-06-28 13:09:26 +00:00
|
|
|
|
|
|
|
if ((dlg_id >= id_start) && (dlg_id < id_end)) {
|
|
|
|
// If we have a valid dialog_id, just process that one dialog
|
|
|
|
id_start = dlg_id;
|
|
|
|
id_end = dlg_id + 1;
|
|
|
|
if (hDlg != NULL) {
|
|
|
|
loc_dlg[dlg_id-IDD_DIALOG].hDlg = hDlg;
|
2013-06-28 01:17:27 +00:00
|
|
|
}
|
2013-06-28 13:09:26 +00:00
|
|
|
}
|
2013-06-28 01:17:27 +00:00
|
|
|
|
2013-06-28 13:09:26 +00:00
|
|
|
for (dlg_id = id_start; dlg_id < id_end; dlg_id++) {
|
|
|
|
hDlg = loc_dlg[dlg_id-IDD_DIALOG].hDlg;
|
|
|
|
if ((!IsWindow(hDlg)) || (list_empty(&loc_dlg[dlg_id-IDD_DIALOG].list)))
|
|
|
|
continue;
|
|
|
|
|
2013-07-27 22:16:11 +00:00
|
|
|
// TODO: storing the messages in an array indexed on the message ID - 3000 would be faster
|
|
|
|
list_for_each_entry(lcmd, &loc_dlg[dlg_id-IDD_DIALOG].list, loc_cmd, list) {
|
2013-06-28 13:09:26 +00:00
|
|
|
if (lcmd->command <= LC_TEXT) { // TODO: should always be the case
|
|
|
|
if (lcmd->ctrl_id == dlg_id) {
|
2013-07-27 22:16:11 +00:00
|
|
|
if ((dlg_id == IDD_DIALOG) && (lcmd->txt[1] != NULL) && (lcmd->txt[1][0] != 0)) {
|
2013-07-07 01:12:05 +00:00
|
|
|
loc_line_nr = lcmd->line_nr;
|
2013-06-29 17:45:09 +00:00
|
|
|
luprint("operation forbidden (main dialog title cannot be changed)");
|
2013-06-28 13:09:26 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
hCtrl = hDlg;
|
|
|
|
} else {
|
|
|
|
hCtrl = GetDlgItem(hDlg, lcmd->ctrl_id);
|
|
|
|
}
|
|
|
|
if (hCtrl == NULL) {
|
|
|
|
loc_line_nr = lcmd->line_nr;
|
|
|
|
luprintf("control '%s' is not part of dialog '%s'\n",
|
2013-07-07 01:12:05 +00:00
|
|
|
lcmd->txt[0], control_id[dlg_id-IDD_DIALOG].name);
|
2013-06-28 13:09:26 +00:00
|
|
|
}
|
2013-06-28 01:17:27 +00:00
|
|
|
}
|
2013-06-28 13:09:26 +00:00
|
|
|
|
|
|
|
switch(lcmd->command) {
|
|
|
|
// NB: For commands that take an ID, ctrl_id is always a valid index at this stage
|
|
|
|
case LC_TEXT:
|
|
|
|
if (hCtrl != NULL) {
|
2013-07-27 22:16:11 +00:00
|
|
|
if ((lcmd->txt[1] != NULL) && (lcmd->txt[1][0] != 0))
|
|
|
|
SetWindowTextU(hCtrl, lcmd->txt[1]);
|
|
|
|
if (left_to_right) {
|
|
|
|
style = GetWindowLongPtr(hCtrl, GWL_EXSTYLE);
|
|
|
|
style |= WS_EX_LAYOUTRTL; // WS_EX_RIGHT | WS_EX_RTLREADING
|
|
|
|
SetWindowLongPtr(hCtrl, GWL_EXSTYLE, style);
|
|
|
|
InvalidateRect(hCtrl, NULL, TRUE);
|
|
|
|
}
|
2013-06-28 13:09:26 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case LC_MOVE:
|
|
|
|
if (hCtrl != NULL) {
|
|
|
|
ResizeMoveCtrl(hDlg, hCtrl, lcmd->num[0], lcmd->num[1], 0, 0);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case LC_RESIZE:
|
|
|
|
if (hCtrl != NULL) {
|
|
|
|
ResizeMoveCtrl(hDlg, hCtrl, 0, 0, lcmd->num[0], lcmd->num[1]);
|
|
|
|
}
|
|
|
|
break;
|
2013-06-28 01:17:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Can't use isWindow() against our existing HWND to avoid this call
|
|
|
|
// as handles are recycled.
|
|
|
|
void reset_localization(int dlg_id)
|
|
|
|
{
|
|
|
|
loc_dlg[dlg_id-IDD_DIALOG].hDlg = NULL;
|
|
|
|
}
|
|
|
|
|
2013-07-27 22:16:11 +00:00
|
|
|
// TODO: Do we need to store a revert for every action we execute here,
|
2013-06-21 00:41:56 +00:00
|
|
|
// or do we want to reinstantiate the dialogs?
|
2013-06-28 01:17:27 +00:00
|
|
|
BOOL dispatch_loc_cmd(loc_cmd* lcmd)
|
2013-06-18 23:39:50 +00:00
|
|
|
{
|
2013-06-21 00:41:56 +00:00
|
|
|
size_t i;
|
2013-06-28 01:17:27 +00:00
|
|
|
static int dlg_index = 0;
|
2013-06-21 00:41:56 +00:00
|
|
|
|
2013-06-18 23:39:50 +00:00
|
|
|
if (lcmd == NULL)
|
|
|
|
return FALSE;
|
2013-06-21 00:41:56 +00:00
|
|
|
|
|
|
|
if (lcmd->command <= LC_TEXT) {
|
2013-06-28 01:17:27 +00:00
|
|
|
// Any command up to LC_TEXT takes a control ID in text[0]
|
2013-06-21 00:41:56 +00:00
|
|
|
for (i=0; i<ARRAYSIZE(control_id); i++) {
|
2013-07-07 01:12:05 +00:00
|
|
|
if (safe_strcmp(lcmd->txt[0], control_id[i].name) == 0) {
|
2013-06-28 01:17:27 +00:00
|
|
|
lcmd->ctrl_id = control_id[i].id;
|
2013-06-21 00:41:56 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2013-06-28 01:17:27 +00:00
|
|
|
if (lcmd->ctrl_id < 0) {
|
2013-07-07 01:12:05 +00:00
|
|
|
luprintf("unknown control '%s'\n", lcmd->txt[0]);
|
2013-06-28 01:17:27 +00:00
|
|
|
goto err;
|
2013-06-21 00:41:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch(lcmd->command) {
|
2013-07-07 01:12:05 +00:00
|
|
|
// NB: For commands that take an ID, ctrl_id is always a valid index at this stage
|
2013-06-21 00:41:56 +00:00
|
|
|
case LC_TEXT:
|
|
|
|
case LC_MOVE:
|
|
|
|
case LC_RESIZE:
|
2013-06-28 01:17:27 +00:00
|
|
|
loc_dlg_add(dlg_index, lcmd);
|
2013-06-21 00:41:56 +00:00
|
|
|
break;
|
|
|
|
case LC_PARENT:
|
2013-06-28 01:17:27 +00:00
|
|
|
if ((lcmd->ctrl_id-IDD_DIALOG) > ARRAYSIZE(loc_dlg)) {
|
2013-07-07 01:12:05 +00:00
|
|
|
luprintf("'%s' is not a dialog ID\n", lcmd->txt[0]);
|
2013-06-28 01:17:27 +00:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
dlg_index = lcmd->ctrl_id - IDD_DIALOG;
|
|
|
|
free_loc_cmd(lcmd);
|
|
|
|
break;
|
2013-07-07 01:12:05 +00:00
|
|
|
case LC_VERSION:
|
|
|
|
luprintf("GOT VERSION: %d.%d\n", lcmd->num[0], lcmd->num[1]);
|
|
|
|
free_loc_cmd(lcmd);
|
|
|
|
break;
|
2013-06-28 01:17:27 +00:00
|
|
|
default:
|
|
|
|
free_loc_cmd(lcmd);
|
2013-06-21 00:41:56 +00:00
|
|
|
break;
|
|
|
|
}
|
2013-06-18 23:39:50 +00:00
|
|
|
return TRUE;
|
2013-06-21 00:41:56 +00:00
|
|
|
|
2013-06-28 01:17:27 +00:00
|
|
|
err:
|
|
|
|
free_loc_cmd(lcmd);
|
|
|
|
return FALSE;
|
2013-06-18 23:39:50 +00:00
|
|
|
}
|
2013-07-27 22:16:11 +00:00
|
|
|
|
|
|
|
char* get_loc_msg(int msg_id)
|
|
|
|
{
|
|
|
|
loc_cmd* lcmd;
|
|
|
|
|
|
|
|
list_for_each_entry(lcmd, &loc_dlg[IDD_MESSAGES-IDD_DIALOG].list, loc_cmd, list) {
|
|
|
|
if ((lcmd->command == LC_TEXT) && (lcmd->ctrl_id == msg_id) && (lcmd->txt[1] != NULL)) {
|
|
|
|
return lcmd->txt[1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// TODO: print the message ID or something
|
|
|
|
return "UNTRANSLATED MESSAGE";
|
|
|
|
}
|
|
|
|
|
|
|
|
loc_cmd* get_locale_from_lcid(int lcid)
|
|
|
|
{
|
|
|
|
loc_cmd* lcmd = NULL;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (list_empty(&locale_list)) {
|
|
|
|
uprintf("localization: the locale list is empty!\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
list_for_each_entry(lcmd, &locale_list, loc_cmd, list) {
|
|
|
|
for (i=0; i<lcmd->unum_size; i++) {
|
|
|
|
if (lcmd->unum[i] == lcid) {
|
|
|
|
return lcmd;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
lcmd = list_entry(locale_list.next, loc_cmd, list);
|
|
|
|
// If we couldn't find a supported locale, just pick the first one (usually English)
|
|
|
|
uprintf("localization: could not find locale for LCID: 0x%04X. Will default to '%s'\n", lcid, lcmd->txt[0]);
|
|
|
|
return lcmd;
|
|
|
|
}
|
|
|
|
|
|
|
|
loc_cmd* get_locale_from_name(char* locale_name)
|
|
|
|
{
|
|
|
|
loc_cmd* lcmd = NULL;
|
|
|
|
|
|
|
|
if (list_empty(&locale_list)) {
|
|
|
|
uprintf("localization: the locale list is empty!\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
list_for_each_entry(lcmd, &locale_list, loc_cmd, list) {
|
|
|
|
if (safe_strcmp(lcmd->txt[0], locale_name) == 0)
|
|
|
|
return lcmd;
|
|
|
|
}
|
|
|
|
|
|
|
|
lcmd = list_entry(locale_list.next, loc_cmd, list);
|
|
|
|
uprintf("localization: could not find locale for name '%s'. Will default to '%s'\n", locale_name, lcmd->txt[0]);
|
|
|
|
return lcmd;
|
|
|
|
}
|