Fix the way we handle translated event names

Here is what Linus reported:

 I think you have made a mistake in trying to translate some of
 libdivecomputer.c

 Translating some of those things based on locale is *wrong*, because
 they are saved in the XML file.

 That covers at least the warnings: they'll get translated when you
 import them, and then saved to the XML file as that translation, but
 now if you start subsurface in another locale, they will not get
 translated back.

 So translating XML file contents is fundamentally buggy. It just
 shouldn't be done.

 So all the "translations" for the event handling are buggy, and
 generate crap. Please don't do that. Leave them as English.

And of course he is absolutely right. However, instead of not translating
them at all, this commit fixes things a better way - we now mark the
strings for translation but store the original English strings everywhere
(in the in-memory data structure as well as in the XML file). Only when we
actually display something on the screen (in a tooltip or in the filter
dialog) do we actually translate the strings into the native language.

This should address both Linus' issue and the desire to have localized
event texts.

Reported-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
This commit is contained in:
Dirk Hohndel 2012-10-21 11:34:11 -07:00
parent e47b52ecdb
commit 016365c5f1
3 changed files with 31 additions and 22 deletions

View file

@ -74,6 +74,8 @@ static int parse_gasmixes(device_data_t *devdata, struct dive *dive, dc_parser_t
static void handle_event(struct dive *dive, struct sample *sample, dc_sample_value_t value)
{
int type, time;
/* we mark these for translation here, but we store the untranslated strings
* and only translate them when they are displayed on screen */
static const char *events[] = {
N_("none"), N_("deco"), N_("rbt"), N_("ascent"), N_("ceiling"), N_("workload"),
N_("transmitter"), N_("violation"), N_("bookmark"), N_("surface"), N_("safety stop"),
@ -98,9 +100,9 @@ static void handle_event(struct dive *dive, struct sample *sample, dc_sample_val
* Other evens might be more interesting, but for now we just print them out.
*/
type = value.event.type;
name = _("invalid event number");
name = N_("invalid event number");
if (type < nr_events)
name = _(events[type]);
name = events[type];
time = value.event.time;
if (sample)

View file

@ -284,7 +284,8 @@ void evn_foreach(void (*callback)(const char *, int *, void *), void *data)
int i;
for (i = 0; i < evn_used; i++) {
callback(ev_namelist[i].ev_name, &ev_namelist[i].plot_ev, data);
/* here we display an event name on screen - so translate */
callback(_(ev_namelist[i].ev_name), &ev_namelist[i].plot_ev, data);
}
}
@ -349,7 +350,8 @@ static void plot_one_event(struct graphics_context *gc, struct plot_info *pi, st
cairo_move_to(gc->cr, x-9, y+4);
cairo_line_to(gc->cr, x-9, y+4);
cairo_stroke(gc->cr);
attach_tooltip(x-15, y-6, 12, 12, event->name);
/* we display the event on screen - so translate */
attach_tooltip(x-15, y-6, 12, 12, _(event->name));
}
static void plot_events(struct graphics_context *gc, struct plot_info *pi, struct dive *dive)

41
uemis.c
View file

@ -123,51 +123,56 @@ bail:
* These bits basically represent what is displayed on screen at sample time.
* Many of these 'warnings' are way hyper-active and seriously clutter the
* profile plot - so these are disabled by default
*
* we mark all the strings for translation, but we store the untranslated
* strings and only convert them when displaying them on screen - this way
* when we write them to the XML file we'll always have the English strings,
* regardless of locale
*/
void uemis_event(struct dive *dive, struct sample *sample, uemis_sample_t *u_sample)
{
uint8_t *flags = u_sample->flags;
if (flags[1] & 0x01)
add_event(dive, sample->time.seconds, 0, 0, 0, _("Safety Stop Violation"));
add_event(dive, sample->time.seconds, 0, 0, 0, N_("Safety Stop Violation"));
if (flags[1] & 0x08)
add_event(dive, sample->time.seconds, 0, 0, 0, _("Speed Alarm"));
add_event(dive, sample->time.seconds, 0, 0, 0, N_("Speed Alarm"));
#if WANT_CRAZY_WARNINGS
if (flags[1] & 0x06) /* both bits 1 and 2 are a warning */
add_event(dive, sample->time.seconds, 0, 0, 0, _("Speed Warning"));
add_event(dive, sample->time.seconds, 0, 0, 0, N_("Speed Warning"));
if (flags[1] & 0x10)
add_event(dive, sample->time.seconds, 0, 0, 0, _("PO2 Green Warning"));
add_event(dive, sample->time.seconds, 0, 0, 0, N_("PO2 Green Warning"));
#endif
if (flags[1] & 0x20)
add_event(dive, sample->time.seconds, 0, 0, 0, _("PO2 Ascend Warning"));
add_event(dive, sample->time.seconds, 0, 0, 0, N_("PO2 Ascend Warning"));
if (flags[1] & 0x40)
add_event(dive, sample->time.seconds, 0, 0, 0, _("PO2 Ascend Alarm"));
add_event(dive, sample->time.seconds, 0, 0, 0, N_("PO2 Ascend Alarm"));
/* flags[2] reflects the deco / time bar
* flags[3] reflects more display details on deco and pO2 */
if (flags[4] & 0x01)
add_event(dive, sample->time.seconds, 0, 0, 0, _("Tank Pressure Info"));
add_event(dive, sample->time.seconds, 0, 0, 0, N_("Tank Pressure Info"));
if (flags[4] & 0x04)
add_event(dive, sample->time.seconds, 0, 0, 0, _("RGT Warning"));
add_event(dive, sample->time.seconds, 0, 0, 0, N_("RGT Warning"));
if (flags[4] & 0x08)
add_event(dive, sample->time.seconds, 0, 0, 0, _("RGT Alert"));
add_event(dive, sample->time.seconds, 0, 0, 0, N_("RGT Alert"));
if (flags[4] & 0x40)
add_event(dive, sample->time.seconds, 0, 0, 0, _("Tank Change Suggested"));
add_event(dive, sample->time.seconds, 0, 0, 0, N_("Tank Change Suggested"));
if (flags[4] & 0x80)
add_event(dive, sample->time.seconds, 0, 0, 0, _("Depth Limit Exceeded"));
add_event(dive, sample->time.seconds, 0, 0, 0, N_("Depth Limit Exceeded"));
if (flags[5] & 0x01)
add_event(dive, sample->time.seconds, 0, 0, 0, _("Max Deco Time Warning"));
add_event(dive, sample->time.seconds, 0, 0, 0, N_("Max Deco Time Warning"));
if (flags[5] & 0x04)
add_event(dive, sample->time.seconds, 0, 0, 0, _("Dive Time Info"));
add_event(dive, sample->time.seconds, 0, 0, 0, N_("Dive Time Info"));
if (flags[5] & 0x08)
add_event(dive, sample->time.seconds, 0, 0, 0, _("Dive Time Alert"));
add_event(dive, sample->time.seconds, 0, 0, 0, N_("Dive Time Alert"));
if (flags[5] & 0x10)
add_event(dive, sample->time.seconds, 0, 0, 0, _("Marker"));
add_event(dive, sample->time.seconds, 0, 0, 0, N_("Marker"));
if (flags[6] & 0x02)
add_event(dive, sample->time.seconds, 0, 0, 0, _("No Tank Data"));
add_event(dive, sample->time.seconds, 0, 0, 0, N_("No Tank Data"));
if (flags[6] & 0x04)
add_event(dive, sample->time.seconds, 0, 0, 0, _("Low Battery Warning"));
add_event(dive, sample->time.seconds, 0, 0, 0, N_("Low Battery Warning"));
if (flags[6] & 0x08)
add_event(dive, sample->time.seconds, 0, 0, 0, _("Low Battery Alert"));
add_event(dive, sample->time.seconds, 0, 0, 0, N_("Low Battery Alert"));
/* flags[7] reflects the little on screen icons that remind of previous
* warnings / alerts - not useful for events */
}