Skip to content

Commit e0916c3

Browse files
behdadBehdad Esfahbod
authored andcommitted
Bug 503071 – Application direction changes to right to left even if
2008-06-11 Behdad Esfahbod <[email protected]> Bug 503071 – Application direction changes to right to left even if theres no translation * glib/gi18n-lib.h: * glib/glib.symbols: * glib/gstrfuncs.h: * glib/gstrfuncs.c: Add new functions g_dgettext() and g_dngettext(). * glib/gutils.c (glib_gettext): * glib/gfileutils.c (g_format_size_for_display): * glib/goption.c (dgettext_swapped): Use the new functions. svn path=/trunk/; revision=7020
1 parent 8882576 commit e0916c3

File tree

10 files changed

+171
-9
lines changed

10 files changed

+171
-9
lines changed

ChangeLog

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
2008-06-11 Behdad Esfahbod <[email protected]>
2+
3+
Bug 503071 – Application direction changes to right to left even if
4+
theres no translation
5+
6+
* glib/gi18n-lib.h:
7+
* glib/glib.symbols:
8+
* glib/gstrfuncs.h:
9+
* glib/gstrfuncs.c:
10+
Add new functions g_dgettext() and g_dngettext().
11+
12+
* glib/gutils.c (glib_gettext):
13+
* glib/gfileutils.c (g_format_size_for_display):
14+
* glib/goption.c (dgettext_swapped):
15+
Use the new functions.
16+
117
2008-06-11 Matthias Clasen <[email protected]>
218

319
Bug 502511 – g_assert_cmphex prints invalid message

docs/reference/ChangeLog

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
2008-06-11 Behdad Esfahbod <[email protected]>
2+
3+
* glib/glib-sections.txt: Add g_dgettext() and g_dngettext().
4+
15
2008-06-11 Matthias Clasen <[email protected]>
26

37
Bug 535418 – Please document which glib version defines goffset

docs/reference/glib/glib-sections.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2498,8 +2498,10 @@ g_unichar_to_utf8
24982498
Q_
24992499
C_
25002500
N_
2501-
g_strip_context
2501+
g_dgettext
2502+
g_dngettext
25022503
g_dpgettext
2504+
g_strip_context
25032505
<SUBSECTION>
25042506
g_get_language_names
25052507
</SECTION>

glib/gfileutils.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1823,7 +1823,7 @@ char *
18231823
g_format_size_for_display (goffset size)
18241824
{
18251825
if (size < (goffset) KILOBYTE_FACTOR)
1826-
return g_strdup_printf (dngettext(GETTEXT_PACKAGE, "%u byte", "%u bytes",(guint) size), (guint) size);
1826+
return g_strdup_printf (g_dngettext(GETTEXT_PACKAGE, "%u byte", "%u bytes",(guint) size), (guint) size);
18271827
else
18281828
{
18291829
gdouble displayed_size;

glib/gi18n-lib.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
#error You must define GETTEXT_PACKAGE before including gi18n-lib.h.
2929
#endif
3030

31-
#define _(String) dgettext (GETTEXT_PACKAGE, String)
31+
#define _(String) ((char *) g_dgettext (GETTEXT_PACKAGE, String))
3232
#define Q_(String) g_dpgettext (GETTEXT_PACKAGE, String, 0)
3333
#define N_(String) (String)
3434
#define C_(Context,String) g_dpgettext (GETTEXT_PACKAGE, Context "\004" String, strlen (Context) + 1)

glib/glib.symbols

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1148,6 +1148,8 @@ g_strdown
11481148
#endif
11491149
g_strv_length
11501150
g_strip_context
1151+
g_dgettext
1152+
g_dngettext
11511153
g_dpgettext
11521154
#endif
11531155
#endif

glib/goption.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2058,7 +2058,7 @@ static gchar *
20582058
dgettext_swapped (const gchar *msgid,
20592059
const gchar *domainname)
20602060
{
2061-
return dgettext (domainname, msgid);
2061+
return g_dgettext (domainname, msgid);
20622062
}
20632063

20642064
/**

glib/gstrfuncs.c

Lines changed: 135 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2853,30 +2853,33 @@ g_strv_length (gchar **str_array)
28532853
* by a \004 character
28542854
* @msgidoffset: the offset of the message id in @msgctxid
28552855
*
2856-
* This function is a variant of dgettext() which supports
2856+
* This function is a variant of g_dgettext() which supports
28572857
* a disambiguating message context. GNU gettext uses the
28582858
* '\004' character to separate the message context and
28592859
* message id in @msgctxtid.
28602860
* If 0 is passed as @msgidoffset, this function will fall back to
28612861
* trying to use the deprecated convention of using "|" as a separation
28622862
* character.
28632863
*
2864+
* This uses g_dgettext() internally. See that functions for differences
2865+
* with dgettext() proper.
2866+
*
28642867
* Applications should normally not use this function directly,
28652868
* but use the C_() macro for translations with context.
28662869
*
28672870
* Returns: The translated string
28682871
*
28692872
* Since: 2.16
28702873
*/
2871-
const gchar *
2874+
G_CONST_RETURN gchar *
28722875
g_dpgettext (const gchar *domain,
28732876
const gchar *msgctxtid,
28742877
gsize msgidoffset)
28752878
{
28762879
const gchar *translation;
28772880
gchar *sep;
28782881

2879-
translation = dgettext (domain, msgctxtid);
2882+
translation = g_dgettext (domain, msgctxtid);
28802883

28812884
if (translation == msgctxtid)
28822885
{
@@ -2894,7 +2897,7 @@ g_dpgettext (const gchar *domain,
28942897
strcpy (tmp, msgctxtid);
28952898
tmp[sep - msgctxtid] = '\004';
28962899

2897-
translation = dgettext (domain, tmp);
2900+
translation = g_dgettext (domain, tmp);
28982901

28992902
if (translation == tmp)
29002903
return sep + 1;
@@ -2904,6 +2907,134 @@ g_dpgettext (const gchar *domain,
29042907
return translation;
29052908
}
29062909

2910+
static gboolean
2911+
_g_dgettext_should_translate (void)
2912+
{
2913+
static gsize translate = 0;
2914+
enum {
2915+
SHOULD_TRANSLATE = 1,
2916+
SHOULD_NOT_TRANSLATE = 2
2917+
};
2918+
2919+
if (G_UNLIKELY (g_once_init_enter (&translate)))
2920+
{
2921+
gboolean should_translate = TRUE;
2922+
2923+
const char *default_domain = textdomain (NULL);
2924+
const char *translator_comment = gettext ("");
2925+
const char *translate_locale = setlocale (LC_MESSAGES, NULL);
2926+
2927+
/* We should NOT translate only if all the following hold:
2928+
* - user has called textdomain() and set textdomain to non-default
2929+
* - default domain has no translations
2930+
* - locale does not start with "en_" and is not "C"
2931+
*
2932+
* Rationale:
2933+
* - If text domain is still the default domain, maybe user calls
2934+
* it later. Continue with old behavior of translating.
2935+
* - If locale starts with "en_", we can continue using the
2936+
* translations even if the app doesn't have translations for
2937+
* this locale. That is, en_UK and en_CA for example.
2938+
* - If locale is "C", maybe user calls setlocale(LC_ALL,"") later.
2939+
* Continue with old behavior of translating.
2940+
*/
2941+
if (0 != strcmp (default_domain, "messages") &&
2942+
'\0' == *translator_comment &&
2943+
0 != strncmp (translate_locale, "en_", 3) &&
2944+
0 != strcmp (translate_locale, "C"))
2945+
should_translate = FALSE;
2946+
2947+
g_once_init_leave (&translate,
2948+
should_translate ?
2949+
SHOULD_TRANSLATE :
2950+
SHOULD_NOT_TRANSLATE);
2951+
}
2952+
2953+
return translate == SHOULD_TRANSLATE;
2954+
}
2955+
2956+
/**
2957+
* g_dgettext:
2958+
* @domain: the translation domain to use, or %NULL to use
2959+
* the domain set with textdomain()
2960+
* @msgid: message to translate
2961+
*
2962+
* This function is a wrapper of dgettext() which does not translate
2963+
* the message if the default domain as set with textdomain() has no
2964+
* translations for the current locale.
2965+
*
2966+
* The advantage of using this function over dgettext() proper is that
2967+
* libraries using this function (like GTK+) will not use translations
2968+
* if the application using the library does not have translations for
2969+
* the current locale. This results in a consistent English-only
2970+
* interface instead of one having partial translations. For this
2971+
* feature to work, the call to textdomain() and setlocale() should
2972+
* precede any g_dgettext() invocations. For GTK+, it means calling
2973+
* textdomain() before gtk_init or its variants.
2974+
*
2975+
* This function disables translations if and only if upon its first
2976+
* call all the following conditions hold:
2977+
* <itemizedlist>
2978+
* <listitem>@domain is not %NULL</listitem>
2979+
* <listitem>textdomain() has been called to set a default text domain</listitem>
2980+
* <listitem>there is no translations available for the default text domain
2981+
* and the current locale</listitem>
2982+
* <listitem>current locale is not "C" or any English locales (those
2983+
* starting with "en_")</listitem>
2984+
* </itemizedlist>
2985+
*
2986+
* Note that this behavior may not be desired for example if an application
2987+
* has its untranslated messages in a language other than English. In those
2988+
* cases the application should call textdomain() after initializing GTK+.
2989+
*
2990+
* Applications should normally not use this function directly,
2991+
* but use the _() macro for translations.
2992+
*
2993+
* Returns: The translated string
2994+
*
2995+
* Since: 2.18
2996+
*/
2997+
G_CONST_RETURN gchar *
2998+
g_dgettext (const gchar *domain,
2999+
const gchar *msgid)
3000+
{
3001+
if (domain && G_UNLIKELY (!_g_dgettext_should_translate ()))
3002+
return msgid;
3003+
3004+
return dgettext (domain, msgid);
3005+
}
3006+
3007+
/**
3008+
* g_dngettext:
3009+
* @domain: the translation domain to use, or %NULL to use
3010+
* the domain set with textdomain()
3011+
* @msgid: message to translate
3012+
* @msgid_plural: plural form of the message
3013+
* @n: the quantity for which translation is needed
3014+
*
3015+
* This function is a wrapper of dngettext() which does not translate
3016+
* the message if the default domain as set with textdomain() has no
3017+
* translations for the current locale.
3018+
*
3019+
* See g_dgettext() for details of how this differs from dngettext()
3020+
* proper.
3021+
*
3022+
* Returns: The translated string
3023+
*
3024+
* Since: 2.18
3025+
*/
3026+
G_CONST_RETURN gchar *
3027+
g_dngettext (const gchar *domain,
3028+
const gchar *msgid,
3029+
const gchar *msgid_plural,
3030+
gulong n)
3031+
{
3032+
if (domain && G_UNLIKELY (!_g_dgettext_should_translate ()))
3033+
return n == 1 ? msgid : msgid_plural;
3034+
3035+
return dngettext (domain, msgid, msgid_plural, n);
3036+
}
3037+
29073038

29083039
#define __G_STRFUNCS_C__
29093040
#include "galiasdef.c"

glib/gstrfuncs.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,13 @@ gchar* g_stpcpy (gchar *dest,
247247
G_CONST_RETURN gchar *g_strip_context (const gchar *msgid,
248248
const gchar *msgval);
249249

250+
G_CONST_RETURN gchar *g_dgettext (const gchar *domain,
251+
const gchar *msgid);
252+
253+
G_CONST_RETURN gchar *g_dngettext (const gchar *domain,
254+
const gchar *msgid,
255+
const gchar *msgid_plural,
256+
gulong n);
250257
G_CONST_RETURN gchar *g_dpgettext (const gchar *domain,
251258
const gchar *msgctxtid,
252259
gsize msgidoffset);

glib/gutils.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3283,7 +3283,7 @@ glib_gettext (const gchar *str)
32833283
_glib_gettext_initialized = TRUE;
32843284
}
32853285

3286-
return dgettext (GETTEXT_PACKAGE, str);
3286+
return g_dgettext (GETTEXT_PACKAGE, str);
32873287
}
32883288

32893289
#ifdef G_OS_WIN32

0 commit comments

Comments
 (0)