Merge lp:~mterry/indicator-datetime/lc-time-fixes into lp:indicator-datetime/0.3

Proposed by Michael Terry
Status: Merged
Merged at revision: 29
Proposed branch: lp:~mterry/indicator-datetime/lc-time-fixes
Merge into: lp:indicator-datetime/0.3
Diff against target: 183 lines (+76/-26)
3 files modified
data/org.ayatana.indicator.datetime.gschema.xml (+1/-1)
po/Makevars (+7/-0)
src/indicator-datetime.c (+68/-25)
To merge this branch: bzr merge lp:~mterry/indicator-datetime/lc-time-fixes
Reviewer Review Type Date Requested Status
Ted Gould (community) Approve
Review via email: mp+43793@code.launchpad.net

Description of the change

This branch does several locale/time-related things:

 * Uses N_ on DEFAULT_TIME_12_FORMAT and DEFAULT_TIME_24_FORMAT so they will appear in the pot file
 * Adds a is_locale_12h function so that we don't have to rely on translators to determine if the locale is 12 or 24 hours
 * Adds a T_ function to translate a string according to the locale specified by LC_TIME, not the one specified by LC_MESSAGES. This means that when the user sets their preferred time locale in gnome-language-selector (for example), indicator-datetime will respect that when looking up time-related translations.
 * Added a po/Makevars to pass --keyword=T_ to xgettext
 * Fixes a minor typo in the gsettings schema

To post a comment you must log in.
Revision history for this message
Ted Gould (ted) wrote :

Wow, very cool. Only comment is that g_setenv's third parameter should really be TRUE/FALSE instead of an int. I'll fix that on merge.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'data/org.ayatana.indicator.datetime.gschema.xml'
2--- data/org.ayatana.indicator.datetime.gschema.xml 2010-07-15 20:02:17 +0000
3+++ data/org.ayatana.indicator.datetime.gschema.xml 2010-12-15 17:02:56 +0000
4@@ -13,7 +13,7 @@
5 Controls the time format that is displayed in the indicator. For almost
6 all users this should be the default for their locale. If you think the
7 setting is wrong for your locale please join or talk to the translation
8- team for your langauge. If you just want something different you can
9+ team for your language. If you just want something different you can
10 adjust this to be either 12 or 24 time. Or, you can use a custom format
11 string and set the custom-time-format setting.
12 </description>
13
14=== added file 'po/Makevars'
15--- po/Makevars 1970-01-01 00:00:00 +0000
16+++ po/Makevars 2010-12-15 17:02:56 +0000
17@@ -0,0 +1,7 @@
18+DOMAIN = $(PACKAGE)
19+subdir = po
20+top_builddir = ..
21+XGETTEXT_OPTIONS = --keyword=_ --keyword=N_ --keyword=C_:1c,2 --keyword=T_ --from-code=UTF-8
22+COPYRIGHT_HOLDER = Canonical Ltd.
23+MSGID_BUGS_ADDRESS = ted@canonical.com
24+EXTRA_LOCALE_CATEGORIES =
25
26=== modified file 'src/indicator-datetime.c'
27--- src/indicator-datetime.c 2010-09-30 13:52:20 +0000
28+++ src/indicator-datetime.c 2010-12-15 17:02:56 +0000
29@@ -23,6 +23,10 @@
30 #include "config.h"
31 #endif
32
33+#include <locale.h>
34+#include <langinfo.h>
35+#include <string.h>
36+
37 /* GStuff */
38 #include <glib.h>
39 #include <glib-object.h>
40@@ -119,8 +123,14 @@
41 SETTINGS_TIME_CUSTOM = 3
42 };
43
44-#define DEFAULT_TIME_12_FORMAT "%l:%M %p"
45-#define DEFAULT_TIME_24_FORMAT "%H:%M"
46+/* TRANSLATORS: A format string for the strftime function for
47+ a clock showing 12-hour time without seconds. */
48+#define DEFAULT_TIME_12_FORMAT N_("%l:%M %p")
49+
50+/* TRANSLATORS: A format string for the strftime function for
51+ a clock showing 24-hour time without seconds. */
52+#define DEFAULT_TIME_24_FORMAT N_("%H:%M")
53+
54 #define DEFAULT_TIME_FORMAT DEFAULT_TIME_12_FORMAT
55
56 #define INDICATOR_DATETIME_GET_PRIVATE(o) \
57@@ -840,6 +850,53 @@
58 return;
59 }
60
61+/* Translate msg according to the locale specified by LC_TIME */
62+static char *
63+T_(const char *msg)
64+{
65+ /* General strategy here is to make sure LANGUAGE is empty (since that
66+ trumps all LC_* vars) and then to temporarily swap LC_TIME and
67+ LC_MESSAGES. Then have gettext translate msg.
68+
69+ We strdup the strings because the setlocale & *env functions do not
70+ guarantee anything about the storage used for the string, and thus
71+ the string may not be portably safe after multiple calls.
72+
73+ Note that while you might think g_dcgettext would do the trick here,
74+ that actually looks in /usr/share/locale/XX/LC_TIME, not the
75+ LC_MESSAGES directory, so we won't find any translation there.
76+ */
77+ char *message_locale = g_strdup(setlocale(LC_MESSAGES, NULL));
78+ char *time_locale = g_strdup(setlocale(LC_TIME, NULL));
79+ char *language = g_strdup(g_getenv("LANGUAGE"));
80+ char *rv;
81+ g_unsetenv("LANGUAGE");
82+ setlocale(LC_MESSAGES, time_locale);
83+
84+ /* Get the LC_TIME version */
85+ rv = _(msg);
86+
87+ /* Put everything back the way it was */
88+ setlocale(LC_MESSAGES, message_locale);
89+ g_setenv("LANGUAGE", language, 1);
90+ g_free(message_locale);
91+ g_free(time_locale);
92+ g_free(language);
93+ return rv;
94+}
95+
96+static gboolean
97+is_locale_12h()
98+{
99+ static const char *formats_24h[] = {"%H", "%R", "%T", "%OH", "%k", NULL};
100+ const char *t_fmt = nl_langinfo(T_FMT);
101+ int i;
102+ for (i = 0; formats_24h[i]; ++i)
103+ if (strstr(t_fmt, formats_24h[i]))
104+ return FALSE;
105+ return TRUE;
106+}
107+
108 /* Tries to figure out what our format string should be. Lots
109 of translator comments in here. */
110 static gchar *
111@@ -852,17 +909,7 @@
112 gboolean twelvehour = TRUE;
113
114 if (self->priv->time_mode == SETTINGS_TIME_LOCALE) {
115- /* TRANSLATORS: This string is used to determine the default
116- clock style for your locale. If it is the string '12' then
117- the default will be a 12-hour clock using AM/PM string. If
118- it is '24' then it will be a 24-hour clock. Users may over
119- ride this setting so it's still important to translate the
120- other strings no matter how this is set. */
121- const gchar * locale_default = _("12");
122-
123- if (g_strcmp0(locale_default, "24") == 0) {
124- twelvehour = FALSE;
125- }
126+ twelvehour = is_locale_12h();
127 } else if (self->priv->time_mode == SETTINGS_TIME_24_HOUR) {
128 twelvehour = FALSE;
129 }
130@@ -872,21 +919,17 @@
131 if (self->priv->show_seconds) {
132 /* TRANSLATORS: A format string for the strftime function for
133 a clock showing 12-hour time with seconds. */
134- time_string = _("%l:%M:%S %p");
135+ time_string = T_("%l:%M:%S %p");
136 } else {
137- /* TRANSLATORS: A format string for the strftime function for
138- a clock showing 12-hour time. */
139- time_string = _(DEFAULT_TIME_12_FORMAT);
140+ time_string = T_(DEFAULT_TIME_12_FORMAT);
141 }
142 } else {
143 if (self->priv->show_seconds) {
144 /* TRANSLATORS: A format string for the strftime function for
145 a clock showing 24-hour time with seconds. */
146- time_string = _("%H:%M:%S");
147+ time_string = T_("%H:%M:%S");
148 } else {
149- /* TRANSLATORS: A format string for the strftime function for
150- a clock showing 24-hour time. */
151- time_string = _(DEFAULT_TIME_24_FORMAT);
152+ time_string = T_(DEFAULT_TIME_24_FORMAT);
153 }
154 }
155
156@@ -903,15 +946,15 @@
157 if (self->priv->show_date && self->priv->show_day) {
158 /* TRANSLATORS: This is a format string passed to strftime to represent
159 the day of the week, the month and the day of the month. */
160- date_string = _("%a %b %e");
161+ date_string = T_("%a %b %e");
162 } else if (self->priv->show_date) {
163 /* TRANSLATORS: This is a format string passed to strftime to represent
164 the month and the day of the month. */
165- date_string = _("%b %e");
166+ date_string = T_("%b %e");
167 } else if (self->priv->show_day) {
168 /* TRANSLATORS: This is a format string passed to strftime to represent
169 the day of the week. */
170- date_string = _("%a");
171+ date_string = T_("%a");
172 }
173
174 /* Check point, we should have a date string */
175@@ -920,7 +963,7 @@
176 /* TRANSLATORS: This is a format string passed to strftime to combine the
177 date and the time. The value of "%s, %s" would result in a string like
178 this in US English 12-hour time: 'Fri Jul 16, 11:50 AM' */
179- return g_strdup_printf(_("%s, %s"), date_string, time_string);
180+ return g_strdup_printf(T_("%s, %s"), date_string, time_string);
181 }
182
183 static gboolean

Subscribers

People subscribed via source and target branches