2009-11-22 Carles Pina i Estany <carles@pina.cat>

* conf/common.rmk: Add grub-gettext_lib target and updates
	  lib_DATA and CLEANFILES. Adds gettext.mod SOURCES, CFLAGS,
	  LDFLAGS.
	* gettext/gettext.c: New file. (Reads mo files).
	* include/grub/file.h (grub_file_pread): New prototype.
	* include/grub/i18n.h (_): New prototype.
	* include/grub/misc.h (grub_gettext_dummy, grub_gettext): New
	  prototypes.
	* kern/misc.c (grub_gettext_dummy): New function.
	* normal/menu_text.c: Include <grub/i18n.h>.
	* normal/menu_text.c (print_timeout): Gettexttize string.
	* normal/menu_text.c (print_message): Gettexttize string.
	* po/POTFILES: Add `normal/menu_text.c'.
	* po/ca.po: Add new translations.
	* util/grub.d/00_header.in: Define locale_dir and lang. insmod
	  gettext module and defines locale_dir and lang in grub.cfg.
	* NEWS: Add gettext support.
This commit is contained in:
Carles Pina i Estany 2009-11-23 21:14:30 +00:00
commit e44249de6d
13 changed files with 390 additions and 9 deletions

View file

@ -1,3 +1,23 @@
2009-11-22 Carles Pina i Estany <carles@pina.cat>
* conf/common.rmk: Add grub-gettext_lib target and updates
lib_DATA and CLEANFILES. Adds gettext.mod SOURCES, CFLAGS,
LDFLAGS.
* gettext/gettext.c: New file. (Reads mo files).
* include/grub/file.h (grub_file_pread): New prototype.
* include/grub/i18n.h (_): New prototype.
* include/grub/misc.h (grub_gettext_dummy, grub_gettext): New
prototypes.
* kern/misc.c (grub_gettext_dummy): New function.
* normal/menu_text.c: Include <grub/i18n.h>.
* normal/menu_text.c (print_timeout): Gettexttize string.
* normal/menu_text.c (print_message): Gettexttize string.
* po/POTFILES: Add `normal/menu_text.c'.
* po/ca.po: Add new translations.
* util/grub.d/00_header.in: Define locale_dir and lang. insmod
gettext module and defines locale_dir and lang in grub.cfg.
* NEWS: Add gettext support.
2009-11-23 Robert Millan <rmh.grub@aybabtu.com> 2009-11-23 Robert Millan <rmh.grub@aybabtu.com>
* util/hostdisk.c: Include `<grub/i18n.h>'. * util/hostdisk.c: Include `<grub/i18n.h>'.

2
NEWS
View file

@ -1,5 +1,7 @@
New in 1.97 - : New in 1.97 - :
* Add support for gettext.
* Add support for loading XNU (MacOS X kernel). * Add support for loading XNU (MacOS X kernel).
* ACPI override support. * ACPI override support.

View file

@ -150,6 +150,12 @@ update-grub_lib: util/update-grub_lib.in config.status
lib_SCRIPTS += update-grub_lib lib_SCRIPTS += update-grub_lib
CLEANFILES += update-grub_lib CLEANFILES += update-grub_lib
grub-gettext_lib: util/grub-gettext_lib.in config.status
./config.status --file=$@:$<
chmod +x $@
lib_DATA += grub-gettext_lib
CLEANFILES += grub-gettext_lib
%: util/grub.d/%.in config.status %: util/grub.d/%.in config.status
./config.status --file=$@:$< ./config.status --file=$@:$<
chmod +x $@ chmod +x $@
@ -597,6 +603,12 @@ bufio_mod_SOURCES = io/bufio.c
bufio_mod_CFLAGS = $(COMMON_CFLAGS) bufio_mod_CFLAGS = $(COMMON_CFLAGS)
bufio_mod_LDFLAGS = $(COMMON_LDFLAGS) bufio_mod_LDFLAGS = $(COMMON_LDFLAGS)
# For gettext.mod.
pkglib_MODULES += gettext.mod
gettext_mod_SOURCES = gettext/gettext.c
gettext_mod_CFLAGS = $(COMMON_CFLAGS)
gettext_mod_LDFLAGS = $(COMMON_LDFLAGS)
# Misc. # Misc.
pkglib_MODULES += xnu_uuid.mod pkglib_MODULES += xnu_uuid.mod

310
gettext/gettext.c Normal file
View file

@ -0,0 +1,310 @@
/* 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>
#include <grub/gzio.h>
/*
.mo file information from:
http://www.gnu.org/software/autoconf/manual/gettext/MO-Files.html .
*/
static grub_file_t fd_mo;
static int grub_gettext_offsetoriginal;
static int grub_gettext_max;
static const char *(*grub_gettext_original) (const char *s);
#define GETTEXT_MAGIC_NUMBER 0
#define GETTEXT_FILE_FORMAT 4
#define GETTEXT_NUMBER_OF_STRINGS 8
#define GETTEXT_OFFSET_ORIGINAL 12
#define GETTEXT_OFFSET_TRANSLATION 16
#define MO_MAGIC_NUMBER 0x950412de
static grub_ssize_t
grub_gettext_pread (grub_file_t file, void *buf, grub_size_t len,
grub_off_t offset)
{
if (grub_file_seek (file, offset) == (grub_off_t) - 1)
{
return -1;
}
return grub_file_read (file, buf, len);
}
static grub_uint32_t
grub_gettext_get_info (int offset)
{
grub_uint32_t value;
grub_gettext_pread (fd_mo, (char *) &value, 4, offset);
value = grub_cpu_to_le32 (value);
return value;
}
static void
grub_gettext_getstring_from_offset (grub_uint32_t offset,
grub_uint32_t length, char *translation)
{
grub_gettext_pread (fd_mo, translation, length, offset);
translation[length] = '\0';
}
static char *
grub_gettext_gettranslation_from_position (int position)
{
int offsettranslation;
int internal_position;
grub_uint32_t length, offset;
char *translation;
offsettranslation = grub_gettext_get_info (GETTEXT_OFFSET_TRANSLATION);
internal_position = offsettranslation + position * 8;
grub_gettext_pread (fd_mo, (char *) &length, 4, internal_position);
length = grub_cpu_to_le32 (length);
grub_gettext_pread (fd_mo, (char *) &offset, 4, internal_position + 4);
offset = grub_cpu_to_le32 (offset);
translation = grub_malloc (length + 1);
grub_gettext_getstring_from_offset (offset, length, translation);
return translation;
}
static char *
grub_gettext_getstring_from_position (int position)
{
int internal_position;
int length, offset;
char *original;
/* Get position for string i. */
internal_position = grub_gettext_offsetoriginal + (position * 8);
/* Get the length of the string i. */
grub_gettext_pread (fd_mo, (char *) &length, 4, internal_position);
/* Get the offset of the string i. */
grub_gettext_pread (fd_mo, (char *) &offset, 4, internal_position + 4);
/* Get the string i. */
original = grub_malloc (length + 1);
grub_gettext_getstring_from_offset (offset, length, original);
return original;
}
static const char *
grub_gettext_translate (const char *orig)
{
char *current_string;
char *ret;
int min, max, current;
if (fd_mo == 0)
return orig;
min = 0;
max = grub_gettext_max;
current = (max + min) / 2;
while (current != min && current != max)
{
current_string = grub_gettext_getstring_from_position (current);
/* Search by bisection. */
if (grub_strcmp (current_string, orig) < 0)
{
grub_free (current_string);
min = current;
}
else if (grub_strcmp (current_string, orig) > 0)
{
grub_free (current_string);
max = current;
}
else if (grub_strcmp (current_string, orig) == 0)
{
grub_free (current_string);
return grub_gettext_gettranslation_from_position (current);
}
current = (max + min) / 2;
}
ret = grub_malloc (grub_strlen (orig) + 1);
grub_strcpy (ret, orig);
return ret;
}
/* This is similar to grub_gzfile_open. */
static grub_file_t
grub_mofile_open (const char *filename)
{
int unsigned magic;
int version;
/* Using fd_mo and not another variable because
it's needed for grub_gettext_get_info. */
fd_mo = grub_gzfile_open (filename, 1);
grub_errno = GRUB_ERR_NONE;
if (!fd_mo)
{
grub_dprintf ("gettext", "Cannot read %s\n", filename);
return 0;
}
magic = grub_gettext_get_info (GETTEXT_MAGIC_NUMBER);
if (magic != MO_MAGIC_NUMBER)
{
grub_error (GRUB_ERR_BAD_FILE_TYPE, "mo: invalid mo file: %s",
filename);
grub_file_close (fd_mo);
fd_mo = 0;
return 0;
}
version = grub_gettext_get_info (GETTEXT_FILE_FORMAT);
if (version != 0)
{
grub_error (GRUB_ERR_BAD_FILE_TYPE,
"mo: invalid mo version in file: %s\n", filename);
fd_mo = 0;
return 0;
}
return fd_mo;
}
static void
grub_gettext_init_ext (const char *lang)
{
char *mo_file;
char *locale_dir;
locale_dir = grub_env_get ("locale_dir");
if (locale_dir == NULL)
{
grub_printf ("locale_dir variable is not set up.");
return;
}
fd_mo = NULL;
/* mo_file e.g.: /boot/grub/locale/ca.mo */
mo_file =
grub_malloc (grub_strlen (locale_dir) + grub_strlen ("/") +
grub_strlen (lang) + grub_strlen (".mo") + 1);
/* Warning: if changing some paths in the below line, change the grub_malloc
contents below. */
grub_sprintf (mo_file, "%s/%s.mo", locale_dir, lang);
fd_mo = grub_mofile_open (mo_file);
/* Will try adding .gz as well. */
if (fd_mo == NULL)
{
grub_sprintf (mo_file, "%s.gz", mo_file);
fd_mo = grub_mofile_open (mo_file);
}
if (fd_mo)
{
grub_gettext_offsetoriginal =
grub_gettext_get_info (GETTEXT_OFFSET_ORIGINAL);
grub_gettext_max = grub_gettext_get_info (GETTEXT_NUMBER_OF_STRINGS);
grub_gettext_original = grub_gettext;
grub_gettext = grub_gettext_translate;
}
}
static char *
grub_gettext_env_write_lang (struct grub_env_var *var
__attribute__ ((unused)), const char *val)
{
grub_gettext_init_ext (val);
return grub_strdup (val);
}
static grub_err_t
grub_cmd_translate (grub_command_t cmd __attribute__ ((unused)),
int argc, char **args)
{
if (argc != 1)
return grub_error (GRUB_ERR_BAD_ARGUMENT, "text to translate required");
const char *translation;
translation = grub_gettext_translate (args[0]);
grub_printf ("%s\n", translation);
return 0;
}
GRUB_MOD_INIT (gettext)
{
(void) mod; /* To stop warning. */
const char *lang;
lang = grub_env_get ("lang");
grub_gettext_init_ext (lang);
grub_register_command_p1 ("gettext", grub_cmd_translate,
"gettext STRING",
"Translates the string with the current settings.");
/* 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");
}
GRUB_MOD_FINI (gettext)
{
if (fd_mo != 0)
grub_file_close (fd_mo);
grub_gettext = grub_gettext_original;
}

View file

@ -24,7 +24,7 @@
# include <libintl.h> # include <libintl.h>
# define _(str) gettext(str) # define _(str) gettext(str)
#else #else
# define _(str) str # define _(str) grub_gettext(str)
#endif #endif
#define N_(str) str #define N_(str) str

View file

@ -1,7 +1,7 @@
/* misc.h - prototypes for misc functions */ /* misc.h - prototypes for misc functions */
/* /*
* GRUB -- GRand Unified Bootloader * GRUB -- GRand Unified Bootloader
* Copyright (C) 2002,2003,2005,2006,2007,2008,2009,2008 Free Software Foundation, Inc. * Copyright (C) 2002,2003,2005,2006,2007,2008,2009,2008,2009 Free Software Foundation, Inc.
* *
* GRUB is free software: you can redistribute it and/or modify * GRUB is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
@ -191,6 +191,9 @@ grub_ssize_t EXPORT_FUNC(grub_utf8_to_ucs4) (grub_uint32_t *dest,
grub_uint64_t EXPORT_FUNC(grub_divmod64) (grub_uint64_t n, grub_uint64_t EXPORT_FUNC(grub_divmod64) (grub_uint64_t n,
grub_uint32_t d, grub_uint32_t *r); grub_uint32_t d, grub_uint32_t *r);
const char *EXPORT_FUNC(grub_gettext_dummy) (const char *s);
extern const char *(*EXPORT_VAR(grub_gettext)) (const char *s);
#ifdef NEED_ENABLE_EXECUTE_STACK #ifdef NEED_ENABLE_EXECUTE_STACK
void EXPORT_FUNC(__enable_execute_stack) (void *addr); void EXPORT_FUNC(__enable_execute_stack) (void *addr);
#endif #endif

View file

@ -30,6 +30,8 @@ grub_iswordseparator (int c)
return (grub_isspace (c) || c == ',' || c == ';' || c == '|' || c == '&'); return (grub_isspace (c) || c == ',' || c == ';' || c == '|' || c == '&');
} }
const char* (*grub_gettext) (const char *s) = grub_gettext_dummy;
void * void *
grub_memmove (void *dest, const void *src, grub_size_t n) grub_memmove (void *dest, const void *src, grub_size_t n)
{ {
@ -984,6 +986,13 @@ grub_utf8_to_ucs4 (grub_uint32_t *dest, grub_size_t destsize,
return p - dest; return p - dest;
} }
/* grub_gettext_dummy is not translating anything. */
const char *
grub_gettext_dummy (const char *s)
{
return s;
}
/* Abort GRUB. This function does not return. */ /* Abort GRUB. This function does not return. */
void void
grub_abort (void) grub_abort (void)

View file

@ -29,6 +29,7 @@
#include <grub/reader.h> #include <grub/reader.h>
#include <grub/menu_viewer.h> #include <grub/menu_viewer.h>
#include <grub/auth.h> #include <grub/auth.h>
#include <grub/i18n.h>
#define GRUB_DEFAULT_HISTORY_SIZE 50 #define GRUB_DEFAULT_HISTORY_SIZE 50
@ -508,10 +509,10 @@ grub_normal_reader_init (void)
grub_normal_init_page (); grub_normal_init_page ();
grub_setcursor (1); grub_setcursor (1);
grub_printf ("\ grub_printf (_("\
[ Minimal BASH-like line editing is supported. For the first word, TAB\n\ [ Minimal BASH-like line editing is supported. For the first word, TAB\n\
lists possible command completions. Anywhere else TAB lists possible\n\ lists possible command completions. Anywhere else TAB lists possible\n\
device/file completions.%s ]\n\n", device/file completions.%s ]\n\n"),
reader_nested ? " ESC at any time exits." : ""); reader_nested ? " ESC at any time exits." : "");
return 0; return 0;

View file

@ -24,6 +24,7 @@
#include <grub/command.h> #include <grub/command.h>
#include <grub/parser.h> #include <grub/parser.h>
#include <grub/auth.h> #include <grub/auth.h>
#include <grub/i18n.h>
enum update_mode enum update_mode
{ {
@ -996,7 +997,7 @@ run (struct screen *screen)
} }
grub_cls (); grub_cls ();
grub_printf (" Booting a command list\n\n"); grub_printf (_(" Booting a command list\n\n"));
/* Execute the script, line for line. */ /* Execute the script, line for line. */
@ -1176,6 +1177,6 @@ grub_menu_entry_run (grub_menu_entry_t entry)
grub_cls (); grub_cls ();
grub_print_error (); grub_print_error ();
grub_errno = GRUB_ERR_NONE; grub_errno = GRUB_ERR_NONE;
grub_printf ("\nPress any key to continue..."); grub_printf (_("\nPress any key to continue..."));
(void) grub_getkey (); (void) grub_getkey ();
} }

View file

@ -25,6 +25,7 @@
#include <grub/time.h> #include <grub/time.h>
#include <grub/env.h> #include <grub/env.h>
#include <grub/menu_viewer.h> #include <grub/menu_viewer.h>
#include <grub/i18n.h>
/* Time to delay after displaying an error message about a default/fallback /* Time to delay after displaying an error message about a default/fallback
entry failing to boot. */ entry failing to boot. */
@ -93,8 +94,8 @@ print_message (int nested, int edit)
} }
else else
{ {
grub_printf ("\n\ grub_printf (_("\n\
Use the %C and %C keys to select which entry is highlighted.\n", Use the %C and %C keys to select which entry is highlighted.\n"),
(grub_uint32_t) GRUB_TERM_DISP_UP, (grub_uint32_t) GRUB_TERM_DISP_DOWN); (grub_uint32_t) GRUB_TERM_DISP_UP, (grub_uint32_t) GRUB_TERM_DISP_DOWN);
grub_printf ("\ grub_printf ("\
Press enter to boot the selected OS, \'e\' to edit the\n\ Press enter to boot the selected OS, \'e\' to edit the\n\

View file

@ -10,3 +10,5 @@ util/mkisofs/multi.c
util/mkisofs/rock.c util/mkisofs/rock.c
util/mkisofs/tree.c util/mkisofs/tree.c
util/mkisofs/write.c util/mkisofs/write.c
normal/menu_text.c

View file

@ -6,7 +6,7 @@ msgid ""
msgstr "" msgstr ""
"Project-Id-Version: GNU GRUB\n" "Project-Id-Version: GNU GRUB\n"
"Report-Msgid-Bugs-To: \n" "Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2009-11-23 14:14+0100\n" "POT-Creation-Date: 2009-11-22 11:45+0000\n"
"PO-Revision-Date: 2009-11-17 12:26+0100\n" "PO-Revision-Date: 2009-11-17 12:26+0100\n"
"Last-Translator: Robert Millan <rmh.grub@aybabtu.com>\n" "Last-Translator: Robert Millan <rmh.grub@aybabtu.com>\n"
"Language-Team: None <no-team-yet@li.org>\n" "Language-Team: None <no-team-yet@li.org>\n"
@ -996,6 +996,15 @@ msgstr ""
msgid "Path table size(bytes): %d\n" msgid "Path table size(bytes): %d\n"
msgstr "" msgstr ""
#: normal/menu_text.c:97
#, c-format
msgid ""
"\n"
" Use the %C and %C keys to select which entry is highlighted.\n"
msgstr ""
"\n"
" Utilitzeu les tecles %C i %C per a seleccionar l'entrada ressaltada.\n"
#: util/grub.d/10_kfreebsd.in:40 #: util/grub.d/10_kfreebsd.in:40
msgid "%s, with kFreeBSD %s" msgid "%s, with kFreeBSD %s"
msgstr "" msgstr ""

View file

@ -22,6 +22,8 @@ prefix=@prefix@
exec_prefix=@exec_prefix@ exec_prefix=@exec_prefix@
libdir=@libdir@ libdir=@libdir@
grub_prefix=`echo /boot/grub | sed ${transform}` grub_prefix=`echo /boot/grub | sed ${transform}`
locale_dir=`echo /boot/grub/locale | sed ${transform}`
grub_lang=`echo $LANG | cut -d _ -f 1`
. ${libdir}/grub/grub-mkconfig_lib . ${libdir}/grub/grub-mkconfig_lib
@ -100,6 +102,15 @@ EOF
;; ;;
esac esac
# Gettext variables and module
if [ "x${LANG}" != "xC" ] ; then
cat << EOF
set locale_dir=${locale_dir}
set lang=${grub_lang}
insmod gettext
EOF
fi
if [ "x${GRUB_HIDDEN_TIMEOUT}" != "x" ] ; then if [ "x${GRUB_HIDDEN_TIMEOUT}" != "x" ] ; then
if [ "x${GRUB_HIDDEN_TIMEOUT_QUIET}" = "xtrue" ] ; then if [ "x${GRUB_HIDDEN_TIMEOUT_QUIET}" = "xtrue" ] ; then
verbose= verbose=