2009-11-13 20:39:33 +00:00
|
|
|
/* gettext.c - gettext module */
|
|
|
|
/*
|
|
|
|
* GRUB -- GRand Unified Bootloader
|
|
|
|
* Copyright (C) 2009 Free Software Foundation, Inc.
|
|
|
|
*
|
|
|
|
* GRUB 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.
|
|
|
|
*
|
|
|
|
* GRUB 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 GRUB. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <grub/types.h>
|
|
|
|
#include <grub/misc.h>
|
|
|
|
#include <grub/mm.h>
|
|
|
|
#include <grub/err.h>
|
|
|
|
#include <grub/dl.h>
|
|
|
|
#include <grub/normal.h>
|
|
|
|
#include <grub/file.h>
|
|
|
|
#include <grub/kernel.h>
|
2009-11-24 21:42:14 +00:00
|
|
|
#include <grub/i18n.h>
|
2009-11-13 20:39:33 +00:00
|
|
|
|
2011-04-11 21:01:51 +00:00
|
|
|
GRUB_MOD_LICENSE ("GPLv3+");
|
|
|
|
|
2009-12-23 16:41:32 +00:00
|
|
|
/*
|
|
|
|
.mo file information from:
|
2009-11-13 20:39:33 +00:00
|
|
|
http://www.gnu.org/software/autoconf/manual/gettext/MO-Files.html .
|
|
|
|
*/
|
|
|
|
|
2009-11-22 11:49:54 +00:00
|
|
|
static const char *(*grub_gettext_original) (const char *s);
|
2009-11-13 20:39:33 +00:00
|
|
|
|
2009-12-05 19:03:27 +00:00
|
|
|
struct grub_gettext_msg
|
|
|
|
{
|
2012-02-04 12:35:17 +00:00
|
|
|
char *name;
|
|
|
|
char *translated;
|
2009-12-05 19:03:27 +00:00
|
|
|
};
|
|
|
|
|
2012-02-04 12:35:17 +00:00
|
|
|
struct header
|
|
|
|
{
|
|
|
|
grub_uint32_t magic;
|
|
|
|
grub_uint32_t version;
|
|
|
|
grub_uint32_t number_of_strings;
|
|
|
|
grub_uint32_t offset_original;
|
|
|
|
grub_uint32_t offset_translation;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct string_descriptor
|
|
|
|
{
|
|
|
|
grub_uint32_t length;
|
|
|
|
grub_uint32_t offset;
|
|
|
|
};
|
2009-11-13 20:39:33 +00:00
|
|
|
|
2012-03-10 22:25:34 +00:00
|
|
|
struct grub_gettext_context
|
|
|
|
{
|
|
|
|
grub_file_t fd_mo;
|
|
|
|
grub_off_t grub_gettext_offset_original;
|
|
|
|
grub_off_t grub_gettext_offset_translation;
|
|
|
|
grub_size_t grub_gettext_max;
|
|
|
|
int grub_gettext_max_log;
|
|
|
|
struct grub_gettext_msg *grub_gettext_msg_list;
|
|
|
|
};
|
|
|
|
|
2013-11-07 00:01:47 +00:00
|
|
|
static struct grub_gettext_context main_context, secondary_context;
|
|
|
|
|
2009-11-22 11:49:54 +00:00
|
|
|
#define MO_MAGIC_NUMBER 0x950412de
|
2009-11-13 20:39:33 +00:00
|
|
|
|
2012-02-04 12:35:17 +00:00
|
|
|
static grub_err_t
|
2009-11-22 12:50:46 +00:00
|
|
|
grub_gettext_pread (grub_file_t file, void *buf, grub_size_t len,
|
|
|
|
grub_off_t offset)
|
|
|
|
{
|
2012-04-04 12:41:55 +00:00
|
|
|
if (len == 0)
|
|
|
|
return GRUB_ERR_NONE;
|
2009-11-22 12:50:46 +00:00
|
|
|
if (grub_file_seek (file, offset) == (grub_off_t) - 1)
|
2012-02-04 12:35:17 +00:00
|
|
|
return grub_errno;
|
|
|
|
if (grub_file_read (file, buf, len) != (grub_ssize_t) len)
|
2009-11-22 12:50:46 +00:00
|
|
|
{
|
2012-02-04 12:35:17 +00:00
|
|
|
if (!grub_errno)
|
|
|
|
grub_error (GRUB_ERR_READ_ERROR, N_("premature end of file"));
|
|
|
|
return grub_errno;
|
2009-11-22 12:50:46 +00:00
|
|
|
}
|
2012-02-04 12:35:17 +00:00
|
|
|
return GRUB_ERR_NONE;
|
2009-11-22 12:50:46 +00:00
|
|
|
}
|
|
|
|
|
2012-02-04 12:35:17 +00:00
|
|
|
static char *
|
2012-03-10 22:25:34 +00:00
|
|
|
grub_gettext_getstr_from_position (struct grub_gettext_context *ctx,
|
|
|
|
grub_off_t off,
|
2012-02-04 12:35:17 +00:00
|
|
|
grub_size_t position)
|
2009-11-13 20:39:33 +00:00
|
|
|
{
|
2012-02-04 12:35:17 +00:00
|
|
|
grub_off_t internal_position;
|
|
|
|
grub_size_t length;
|
|
|
|
grub_off_t offset;
|
2009-11-13 20:39:33 +00:00
|
|
|
char *translation;
|
2012-02-04 12:35:17 +00:00
|
|
|
struct string_descriptor desc;
|
|
|
|
grub_err_t err;
|
2009-11-13 20:39:33 +00:00
|
|
|
|
2012-02-04 12:35:17 +00:00
|
|
|
internal_position = (off + position * sizeof (desc));
|
2009-11-13 20:39:33 +00:00
|
|
|
|
2012-03-10 22:25:34 +00:00
|
|
|
err = grub_gettext_pread (ctx->fd_mo, (char *) &desc,
|
2012-02-04 12:35:17 +00:00
|
|
|
sizeof (desc), internal_position);
|
|
|
|
if (err)
|
|
|
|
return NULL;
|
|
|
|
length = grub_cpu_to_le32 (desc.length);
|
|
|
|
offset = grub_cpu_to_le32 (desc.offset);
|
2009-11-13 20:39:33 +00:00
|
|
|
|
2009-11-22 11:49:54 +00:00
|
|
|
translation = grub_malloc (length + 1);
|
2012-02-04 12:35:17 +00:00
|
|
|
if (!translation)
|
|
|
|
return NULL;
|
|
|
|
|
2012-03-10 22:25:34 +00:00
|
|
|
err = grub_gettext_pread (ctx->fd_mo, translation, length, offset);
|
2012-02-04 12:35:17 +00:00
|
|
|
if (err)
|
|
|
|
{
|
|
|
|
grub_free (translation);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
translation[length] = '\0';
|
2009-11-13 20:39:33 +00:00
|
|
|
|
|
|
|
return translation;
|
|
|
|
}
|
|
|
|
|
2012-02-04 12:35:17 +00:00
|
|
|
static const char *
|
2012-03-10 22:25:34 +00:00
|
|
|
grub_gettext_gettranslation_from_position (struct grub_gettext_context *ctx,
|
|
|
|
grub_size_t position)
|
2009-11-13 20:39:33 +00:00
|
|
|
{
|
2012-03-10 22:25:34 +00:00
|
|
|
if (!ctx->grub_gettext_msg_list[position].translated)
|
|
|
|
ctx->grub_gettext_msg_list[position].translated
|
|
|
|
= grub_gettext_getstr_from_position (ctx,
|
|
|
|
ctx->grub_gettext_offset_translation,
|
2012-02-04 12:35:17 +00:00
|
|
|
position);
|
2012-03-10 22:25:34 +00:00
|
|
|
return ctx->grub_gettext_msg_list[position].translated;
|
2012-02-04 12:35:17 +00:00
|
|
|
}
|
2009-11-13 20:39:33 +00:00
|
|
|
|
2012-02-04 12:35:17 +00:00
|
|
|
static const char *
|
2012-03-10 22:25:34 +00:00
|
|
|
grub_gettext_getstring_from_position (struct grub_gettext_context *ctx,
|
|
|
|
grub_size_t position)
|
2012-02-04 12:35:17 +00:00
|
|
|
{
|
2012-03-10 22:25:34 +00:00
|
|
|
if (!ctx->grub_gettext_msg_list[position].name)
|
|
|
|
ctx->grub_gettext_msg_list[position].name
|
|
|
|
= grub_gettext_getstr_from_position (ctx,
|
|
|
|
ctx->grub_gettext_offset_original,
|
2012-02-04 12:35:17 +00:00
|
|
|
position);
|
2012-03-10 22:25:34 +00:00
|
|
|
return ctx->grub_gettext_msg_list[position].name;
|
2009-11-13 20:39:33 +00:00
|
|
|
}
|
|
|
|
|
2009-11-22 11:49:54 +00:00
|
|
|
static const char *
|
2012-03-10 22:25:34 +00:00
|
|
|
grub_gettext_translate_real (struct grub_gettext_context *ctx,
|
|
|
|
const char *orig)
|
2009-11-13 20:39:33 +00:00
|
|
|
{
|
2012-02-04 12:35:17 +00:00
|
|
|
grub_size_t current = 0;
|
|
|
|
int i;
|
|
|
|
const char *current_string;
|
|
|
|
static int depth = 0;
|
2009-11-13 20:39:33 +00:00
|
|
|
|
2012-03-10 22:25:34 +00:00
|
|
|
if (!ctx->grub_gettext_msg_list || !ctx->fd_mo)
|
|
|
|
return NULL;
|
2009-12-05 19:03:27 +00:00
|
|
|
|
2012-02-04 12:35:17 +00:00
|
|
|
/* Shouldn't happen. Just a precaution if our own code
|
|
|
|
calls gettext somehow. */
|
|
|
|
if (depth > 2)
|
2012-03-10 22:25:34 +00:00
|
|
|
return NULL;
|
2012-02-04 12:35:17 +00:00
|
|
|
depth++;
|
2009-12-05 19:03:27 +00:00
|
|
|
|
2010-01-14 22:31:06 +00:00
|
|
|
/* Make sure we can use grub_gettext_translate for error messages. Push
|
|
|
|
active error message to error stack and reset error message. */
|
|
|
|
grub_error_push ();
|
|
|
|
|
2012-03-10 22:25:34 +00:00
|
|
|
for (i = ctx->grub_gettext_max_log; i >= 0; i--)
|
2010-01-14 22:31:06 +00:00
|
|
|
{
|
2012-02-04 12:35:17 +00:00
|
|
|
grub_size_t test;
|
|
|
|
int cmp;
|
2009-11-13 20:39:33 +00:00
|
|
|
|
2012-02-04 12:35:17 +00:00
|
|
|
test = current | (1 << i);
|
2012-03-10 22:25:34 +00:00
|
|
|
if (test >= ctx->grub_gettext_max)
|
2012-02-04 12:35:17 +00:00
|
|
|
continue;
|
2009-11-13 20:39:33 +00:00
|
|
|
|
2012-03-10 22:25:34 +00:00
|
|
|
current_string = grub_gettext_getstring_from_position (ctx, test);
|
2009-11-13 20:39:33 +00:00
|
|
|
|
2012-02-04 12:35:17 +00:00
|
|
|
if (!current_string)
|
2009-11-22 11:49:54 +00:00
|
|
|
{
|
2012-02-04 12:35:17 +00:00
|
|
|
grub_errno = GRUB_ERR_NONE;
|
|
|
|
grub_error_pop ();
|
|
|
|
depth--;
|
2012-03-10 22:25:34 +00:00
|
|
|
return NULL;
|
2009-11-22 11:49:54 +00:00
|
|
|
}
|
2009-12-05 19:03:27 +00:00
|
|
|
|
2012-02-04 12:35:17 +00:00
|
|
|
/* Search by bisection. */
|
|
|
|
cmp = grub_strcmp (current_string, orig);
|
|
|
|
if (cmp <= 0)
|
|
|
|
current = test;
|
|
|
|
if (cmp == 0)
|
2009-12-05 19:03:27 +00:00
|
|
|
{
|
2012-02-04 12:35:17 +00:00
|
|
|
const char *ret = 0;
|
2012-03-10 22:25:34 +00:00
|
|
|
ret = grub_gettext_gettranslation_from_position (ctx, current);
|
2012-02-04 12:35:17 +00:00
|
|
|
if (!ret)
|
2009-12-05 19:03:27 +00:00
|
|
|
{
|
2012-02-04 12:35:17 +00:00
|
|
|
grub_errno = GRUB_ERR_NONE;
|
|
|
|
grub_error_pop ();
|
|
|
|
depth--;
|
2012-03-10 22:25:34 +00:00
|
|
|
return NULL;
|
2009-12-05 19:03:27 +00:00
|
|
|
}
|
2012-02-04 12:35:17 +00:00
|
|
|
grub_error_pop ();
|
|
|
|
depth--;
|
|
|
|
return ret;
|
2009-12-05 19:03:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-04 12:41:55 +00:00
|
|
|
if (current == 0 && ctx->grub_gettext_max != 0)
|
|
|
|
{
|
|
|
|
current_string = grub_gettext_getstring_from_position (ctx, 0);
|
|
|
|
|
|
|
|
if (!current_string)
|
|
|
|
{
|
|
|
|
grub_errno = GRUB_ERR_NONE;
|
|
|
|
grub_error_pop ();
|
|
|
|
depth--;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (grub_strcmp (current_string, orig) == 0)
|
|
|
|
{
|
|
|
|
const char *ret = 0;
|
|
|
|
ret = grub_gettext_gettranslation_from_position (ctx, current);
|
|
|
|
if (!ret)
|
|
|
|
{
|
|
|
|
grub_errno = GRUB_ERR_NONE;
|
|
|
|
grub_error_pop ();
|
|
|
|
depth--;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
grub_error_pop ();
|
|
|
|
depth--;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-01-14 22:31:06 +00:00
|
|
|
grub_error_pop ();
|
2012-02-04 12:35:17 +00:00
|
|
|
depth--;
|
2012-03-10 22:25:34 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char *
|
|
|
|
grub_gettext_translate (const char *orig)
|
|
|
|
{
|
|
|
|
const char *ret;
|
2012-04-04 12:41:55 +00:00
|
|
|
if (orig[0] == 0)
|
|
|
|
return orig;
|
|
|
|
|
2012-03-10 22:25:34 +00:00
|
|
|
ret = grub_gettext_translate_real (&main_context, orig);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
ret = grub_gettext_translate_real (&secondary_context, orig);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
return orig;
|
2012-02-04 12:35:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2012-03-10 22:25:34 +00:00
|
|
|
grub_gettext_delete_list (struct grub_gettext_context *ctx)
|
2012-02-04 12:35:17 +00:00
|
|
|
{
|
2012-03-10 22:25:34 +00:00
|
|
|
struct grub_gettext_msg *l = ctx->grub_gettext_msg_list;
|
2012-02-04 12:35:17 +00:00
|
|
|
grub_size_t i;
|
|
|
|
|
|
|
|
if (!l)
|
|
|
|
return;
|
2012-03-10 22:25:34 +00:00
|
|
|
ctx->grub_gettext_msg_list = 0;
|
|
|
|
for (i = 0; i < ctx->grub_gettext_max; i++)
|
2012-02-04 12:35:17 +00:00
|
|
|
grub_free (l[i].name);
|
|
|
|
/* Don't delete the translated message because could be in use. */
|
|
|
|
grub_free (l);
|
2012-03-10 22:25:34 +00:00
|
|
|
if (ctx->fd_mo)
|
|
|
|
grub_file_close (ctx->fd_mo);
|
|
|
|
ctx->fd_mo = 0;
|
|
|
|
grub_memset (ctx, 0, sizeof (*ctx));
|
2009-11-13 20:39:33 +00:00
|
|
|
}
|
|
|
|
|
2010-09-05 11:05:36 +00:00
|
|
|
/* This is similar to grub_file_open. */
|
2012-02-04 12:35:17 +00:00
|
|
|
static grub_err_t
|
2012-03-10 22:25:34 +00:00
|
|
|
grub_mofile_open (struct grub_gettext_context *ctx,
|
|
|
|
const char *filename)
|
2009-11-13 20:39:33 +00:00
|
|
|
{
|
2012-02-04 12:35:17 +00:00
|
|
|
struct header head;
|
|
|
|
grub_err_t err;
|
|
|
|
grub_file_t fd;
|
2009-11-13 20:39:33 +00:00
|
|
|
|
|
|
|
/* Using fd_mo and not another variable because
|
|
|
|
it's needed for grub_gettext_get_info. */
|
|
|
|
|
2012-02-04 12:35:17 +00:00
|
|
|
fd = grub_file_open (filename);
|
|
|
|
|
|
|
|
if (!fd)
|
|
|
|
return grub_errno;
|
2009-11-22 11:49:54 +00:00
|
|
|
|
2012-02-04 12:35:17 +00:00
|
|
|
err = grub_gettext_pread (fd, &head, sizeof (head), 0);
|
|
|
|
if (err)
|
2009-11-13 20:39:33 +00:00
|
|
|
{
|
2012-02-04 12:35:17 +00:00
|
|
|
grub_file_close (fd);
|
|
|
|
return err;
|
2009-11-13 20:39:33 +00:00
|
|
|
}
|
|
|
|
|
2012-02-04 12:35:17 +00:00
|
|
|
if (head.magic != grub_cpu_to_le32_compile_time (MO_MAGIC_NUMBER))
|
|
|
|
{
|
|
|
|
grub_file_close (fd);
|
|
|
|
return grub_error (GRUB_ERR_BAD_FILE_TYPE,
|
|
|
|
"mo: invalid mo magic in file: %s", filename);
|
|
|
|
}
|
2009-11-13 20:39:33 +00:00
|
|
|
|
2012-02-04 12:35:17 +00:00
|
|
|
if (head.version != 0)
|
2009-11-13 20:39:33 +00:00
|
|
|
{
|
2012-02-04 12:35:17 +00:00
|
|
|
grub_file_close (fd);
|
|
|
|
return grub_error (GRUB_ERR_BAD_FILE_TYPE,
|
|
|
|
"mo: invalid mo version in file: %s", filename);
|
2009-11-13 20:39:33 +00:00
|
|
|
}
|
2009-11-22 11:49:54 +00:00
|
|
|
|
2012-03-10 22:25:34 +00:00
|
|
|
ctx->grub_gettext_offset_original = grub_le_to_cpu32 (head.offset_original);
|
|
|
|
ctx->grub_gettext_offset_translation = grub_le_to_cpu32 (head.offset_translation);
|
|
|
|
ctx->grub_gettext_max = grub_le_to_cpu32 (head.number_of_strings);
|
|
|
|
for (ctx->grub_gettext_max_log = 0; ctx->grub_gettext_max >> ctx->grub_gettext_max_log;
|
|
|
|
ctx->grub_gettext_max_log++);
|
2009-11-13 20:39:33 +00:00
|
|
|
|
2012-03-10 22:25:34 +00:00
|
|
|
ctx->grub_gettext_msg_list = grub_zalloc (ctx->grub_gettext_max
|
|
|
|
* sizeof (ctx->grub_gettext_msg_list[0]));
|
|
|
|
if (!ctx->grub_gettext_msg_list)
|
2009-11-13 20:39:33 +00:00
|
|
|
{
|
2012-02-04 12:35:17 +00:00
|
|
|
grub_file_close (fd);
|
|
|
|
return grub_errno;
|
2009-11-13 20:39:33 +00:00
|
|
|
}
|
2012-03-10 22:25:34 +00:00
|
|
|
ctx->fd_mo = fd;
|
2012-02-04 12:35:17 +00:00
|
|
|
if (grub_gettext != grub_gettext_translate)
|
|
|
|
{
|
|
|
|
grub_gettext_original = grub_gettext;
|
|
|
|
grub_gettext = grub_gettext_translate;
|
|
|
|
}
|
|
|
|
return 0;
|
2009-11-13 20:39:33 +00:00
|
|
|
}
|
|
|
|
|
2010-12-10 11:45:08 +00:00
|
|
|
/* Returning grub_file_t would be more natural, but grub_mofile_open assigns
|
|
|
|
to fd_mo anyway ... */
|
2012-02-04 12:35:17 +00:00
|
|
|
static grub_err_t
|
2012-03-10 22:25:34 +00:00
|
|
|
grub_mofile_open_lang (struct grub_gettext_context *ctx,
|
|
|
|
const char *part1, const char *part2, const char *locale)
|
2009-11-13 20:39:33 +00:00
|
|
|
{
|
|
|
|
char *mo_file;
|
2012-02-04 12:35:17 +00:00
|
|
|
grub_err_t err;
|
2009-11-22 11:49:54 +00:00
|
|
|
|
2009-11-21 23:11:14 +00:00
|
|
|
/* mo_file e.g.: /boot/grub/locale/ca.mo */
|
2009-11-13 20:39:33 +00:00
|
|
|
|
2012-02-04 20:47:29 +00:00
|
|
|
mo_file = grub_xasprintf ("%s%s/%s.mo", part1, part2, locale);
|
2009-12-29 09:04:06 +00:00
|
|
|
if (!mo_file)
|
2012-02-04 12:35:17 +00:00
|
|
|
return grub_errno;
|
2009-11-21 21:05:15 +00:00
|
|
|
|
2012-03-10 22:25:34 +00:00
|
|
|
err = grub_mofile_open (ctx, mo_file);
|
2013-05-04 11:40:52 +00:00
|
|
|
grub_free (mo_file);
|
2009-11-13 20:39:33 +00:00
|
|
|
|
2009-11-22 11:49:54 +00:00
|
|
|
/* Will try adding .gz as well. */
|
2012-02-04 12:35:17 +00:00
|
|
|
if (err)
|
2009-11-22 11:49:54 +00:00
|
|
|
{
|
2012-02-04 12:35:17 +00:00
|
|
|
grub_errno = GRUB_ERR_NONE;
|
2013-05-04 11:40:52 +00:00
|
|
|
mo_file = grub_xasprintf ("%s%s/%s.mo.gz", part1, part2, locale);
|
2009-12-29 09:04:06 +00:00
|
|
|
if (!mo_file)
|
2012-02-04 12:35:17 +00:00
|
|
|
return grub_errno;
|
2012-03-10 22:25:34 +00:00
|
|
|
err = grub_mofile_open (ctx, mo_file);
|
2013-05-04 11:40:52 +00:00
|
|
|
grub_free (mo_file);
|
2009-11-22 11:49:54 +00:00
|
|
|
}
|
2013-05-04 11:40:52 +00:00
|
|
|
|
|
|
|
/* Will try adding .gmo as well. */
|
|
|
|
if (err)
|
|
|
|
{
|
|
|
|
grub_errno = GRUB_ERR_NONE;
|
|
|
|
mo_file = grub_xasprintf ("%s%s/%s.gmo", part1, part2, locale);
|
|
|
|
if (!mo_file)
|
|
|
|
return grub_errno;
|
|
|
|
err = grub_mofile_open (ctx, mo_file);
|
|
|
|
grub_free (mo_file);
|
|
|
|
}
|
|
|
|
|
2012-02-04 12:35:17 +00:00
|
|
|
return err;
|
2010-12-10 11:45:08 +00:00
|
|
|
}
|
|
|
|
|
2012-02-04 12:35:17 +00:00
|
|
|
static grub_err_t
|
2012-03-10 22:25:34 +00:00
|
|
|
grub_gettext_init_ext (struct grub_gettext_context *ctx,
|
|
|
|
const char *locale,
|
|
|
|
const char *locale_dir, const char *prefix)
|
2010-12-10 11:45:08 +00:00
|
|
|
{
|
2012-02-04 20:47:29 +00:00
|
|
|
const char *part1, *part2;
|
2012-02-04 12:35:17 +00:00
|
|
|
grub_err_t err;
|
2010-12-10 11:45:08 +00:00
|
|
|
|
2012-03-10 22:25:34 +00:00
|
|
|
grub_gettext_delete_list (ctx);
|
|
|
|
|
|
|
|
if (!locale || locale[0] == 0)
|
2012-02-04 12:35:17 +00:00
|
|
|
return 0;
|
2011-11-03 15:04:26 +00:00
|
|
|
|
2012-03-10 22:25:34 +00:00
|
|
|
part1 = locale_dir;
|
2012-02-04 20:47:29 +00:00
|
|
|
part2 = "";
|
2012-03-10 22:25:34 +00:00
|
|
|
if (!part1 || part1[0] == 0)
|
2010-12-10 11:45:08 +00:00
|
|
|
{
|
2012-03-10 22:25:34 +00:00
|
|
|
part1 = prefix;
|
2012-02-04 20:47:29 +00:00
|
|
|
part2 = "/locale";
|
2010-12-10 11:45:08 +00:00
|
|
|
}
|
|
|
|
|
2012-03-10 22:25:34 +00:00
|
|
|
if (!part1 || part1[0] == 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
err = grub_mofile_open_lang (ctx, part1, part2, locale);
|
2010-12-10 11:45:08 +00:00
|
|
|
|
|
|
|
/* ll_CC didn't work, so try ll. */
|
2012-02-04 12:35:17 +00:00
|
|
|
if (err)
|
2010-12-10 11:45:08 +00:00
|
|
|
{
|
|
|
|
char *lang = grub_strdup (locale);
|
2012-02-04 12:35:17 +00:00
|
|
|
char *underscore = lang ? grub_strchr (lang, '_') : 0;
|
2010-12-10 11:45:08 +00:00
|
|
|
|
|
|
|
if (underscore)
|
|
|
|
{
|
|
|
|
*underscore = '\0';
|
2012-02-04 12:35:17 +00:00
|
|
|
grub_errno = GRUB_ERR_NONE;
|
2012-03-10 22:25:34 +00:00
|
|
|
err = grub_mofile_open_lang (ctx, part1, part2, lang);
|
2010-12-10 11:45:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
grub_free (lang);
|
|
|
|
}
|
2013-11-03 04:42:02 +00:00
|
|
|
|
|
|
|
if (locale[0] == 'e' && locale[1] == 'n'
|
|
|
|
&& (locale[2] == '\0' || locale[2] == '_'))
|
|
|
|
grub_errno = err = GRUB_ERR_NONE;
|
2012-02-04 12:35:17 +00:00
|
|
|
return err;
|
2009-12-05 19:03:27 +00:00
|
|
|
}
|
|
|
|
|
2009-11-22 11:49:54 +00:00
|
|
|
static char *
|
|
|
|
grub_gettext_env_write_lang (struct grub_env_var *var
|
|
|
|
__attribute__ ((unused)), const char *val)
|
2009-11-13 20:39:33 +00:00
|
|
|
{
|
2012-02-04 12:35:17 +00:00
|
|
|
grub_err_t err;
|
2012-03-10 22:25:34 +00:00
|
|
|
err = grub_gettext_init_ext (&main_context, val, grub_env_get ("locale_dir"),
|
|
|
|
grub_env_get ("prefix"));
|
2012-02-04 12:35:17 +00:00
|
|
|
if (err)
|
2012-03-10 22:25:34 +00:00
|
|
|
grub_print_error ();
|
|
|
|
|
|
|
|
err = grub_gettext_init_ext (&secondary_context, val,
|
|
|
|
grub_env_get ("secondary_locale_dir"), 0);
|
|
|
|
if (err)
|
|
|
|
grub_print_error ();
|
|
|
|
|
|
|
|
return grub_strdup (val);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
grub_gettext_reread_prefix (const char *val)
|
|
|
|
{
|
|
|
|
grub_err_t err;
|
|
|
|
err = grub_gettext_init_ext (&main_context, grub_env_get ("lang"),
|
|
|
|
grub_env_get ("locale_dir"),
|
|
|
|
val);
|
|
|
|
if (err)
|
|
|
|
grub_print_error ();
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *
|
|
|
|
read_main (struct grub_env_var *var
|
|
|
|
__attribute__ ((unused)), const char *val)
|
|
|
|
{
|
|
|
|
grub_err_t err;
|
|
|
|
err = grub_gettext_init_ext (&main_context, grub_env_get ("lang"), val,
|
|
|
|
grub_env_get ("prefix"));
|
|
|
|
if (err)
|
|
|
|
grub_print_error ();
|
|
|
|
return grub_strdup (val);
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *
|
|
|
|
read_secondary (struct grub_env_var *var
|
|
|
|
__attribute__ ((unused)), const char *val)
|
|
|
|
{
|
|
|
|
grub_err_t err;
|
|
|
|
err = grub_gettext_init_ext (&secondary_context, grub_env_get ("lang"), val,
|
|
|
|
0);
|
|
|
|
if (err)
|
|
|
|
grub_print_error ();
|
2009-12-05 19:03:27 +00:00
|
|
|
|
2009-11-13 20:39:33 +00:00
|
|
|
return grub_strdup (val);
|
|
|
|
}
|
|
|
|
|
2009-11-22 18:58:19 +00:00
|
|
|
static grub_err_t
|
|
|
|
grub_cmd_translate (grub_command_t cmd __attribute__ ((unused)),
|
|
|
|
int argc, char **args)
|
|
|
|
{
|
|
|
|
if (argc != 1)
|
2012-02-12 14:25:25 +00:00
|
|
|
return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("one argument expected"));
|
2009-11-22 18:58:19 +00:00
|
|
|
|
|
|
|
const char *translation;
|
|
|
|
translation = grub_gettext_translate (args[0]);
|
|
|
|
grub_printf ("%s\n", translation);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-11-22 11:49:54 +00:00
|
|
|
GRUB_MOD_INIT (gettext)
|
2009-11-13 20:39:33 +00:00
|
|
|
{
|
|
|
|
const char *lang;
|
2012-03-26 14:07:42 +00:00
|
|
|
grub_err_t err;
|
2009-11-13 20:39:33 +00:00
|
|
|
|
2009-11-22 11:49:54 +00:00
|
|
|
lang = grub_env_get ("lang");
|
2009-11-13 20:39:33 +00:00
|
|
|
|
2012-03-26 14:07:42 +00:00
|
|
|
err = grub_gettext_init_ext (&main_context, lang, grub_env_get ("locale_dir"),
|
|
|
|
grub_env_get ("prefix"));
|
|
|
|
if (err)
|
|
|
|
grub_print_error ();
|
|
|
|
err = grub_gettext_init_ext (&secondary_context, lang,
|
|
|
|
grub_env_get ("secondary_locale_dir"), 0);
|
|
|
|
if (err)
|
|
|
|
grub_print_error ();
|
2012-03-10 22:25:34 +00:00
|
|
|
|
|
|
|
grub_register_variable_hook ("locale_dir", NULL, read_main);
|
|
|
|
grub_register_variable_hook ("secondary_locale_dir", NULL, read_secondary);
|
2009-11-13 20:39:33 +00:00
|
|
|
|
2009-11-22 18:58:19 +00:00
|
|
|
grub_register_command_p1 ("gettext", grub_cmd_translate,
|
2010-01-09 20:20:27 +00:00
|
|
|
N_("STRING"),
|
2012-02-26 16:28:05 +00:00
|
|
|
/* TRANSLATORS: It refers to passing the string through gettext.
|
|
|
|
So it's "translate" in the same meaning as in what you're
|
|
|
|
doing now.
|
|
|
|
*/
|
2010-01-09 20:20:27 +00:00
|
|
|
N_("Translates the string with the current settings."));
|
2009-11-22 18:58:19 +00:00
|
|
|
|
2009-11-13 20:39:33 +00:00
|
|
|
/* Reload .mo file information if lang changes. */
|
|
|
|
grub_register_variable_hook ("lang", NULL, grub_gettext_env_write_lang);
|
|
|
|
|
|
|
|
/* Preserve hooks after context changes. */
|
|
|
|
grub_env_export ("lang");
|
2012-03-10 22:25:34 +00:00
|
|
|
grub_env_export ("locale_dir");
|
|
|
|
grub_env_export ("secondary_locale_dir");
|
2009-11-13 20:39:33 +00:00
|
|
|
}
|
|
|
|
|
2009-11-22 11:49:54 +00:00
|
|
|
GRUB_MOD_FINI (gettext)
|
2009-11-13 20:39:33 +00:00
|
|
|
{
|
2012-03-10 22:25:34 +00:00
|
|
|
grub_gettext_delete_list (&main_context);
|
|
|
|
grub_gettext_delete_list (&secondary_context);
|
2009-12-05 19:03:27 +00:00
|
|
|
|
2009-11-13 20:39:33 +00:00
|
|
|
grub_gettext = grub_gettext_original;
|
|
|
|
}
|