Edgewall Software
Modify

Opened 16 years ago

Closed 14 years ago

Last modified 8 years ago

#8117 closed enhancement (fixed)

Default language setting in trac.ini

Reported by: alexey.rudy@… Owned by: Christian Boos
Priority: low Milestone: 0.12.1
Component: web frontend Version: 0.12dev
Severity: normal Keywords: i18n language settings review
Cc: alexey.rudy@…, nzoltan Branch:
Release Notes:
API Changes:
Internal Changes:

Description

I haven't found the way to enforce default language. Now language selection based on Accept-Language header from browser, but sometimes that's not convinient.

I suggest to add extra setting to trac.ini:

[trac]
lang = ru

This setting will have higher priority than headers data. Patch is attached: patch_from_r7937.diff

Attachments (5)

patch_from_r7937.diff (582 bytes ) - added by alexey.rudy@… 16 years ago.
patch for setting defaulf language in trac.ini
patch_from_r7937.2.diff (692 bytes ) - added by alexey.rudy@… 16 years ago.
patch for setting defaulf language in trac.ini, version 2
patch_from_r7937.2.2.diff (1.5 KB ) - added by Christian Boos 14 years ago.
enable a default language to be configured in TracIni (based on r10044)
8117-trac-default_language.patch (1.6 KB ) - added by Christian Boos 14 years ago.
reworked patch_from_r7937.2.2.diff in order to properly support values with undescores (e.g. 'zh_CN')
8117-trac-admin-default_language.patch (1.9 KB ) - added by Christian Boos 14 years ago.
trac-admin uses the default_language configured for the environment, if any; patch on top of 8117-trac-default_language.patch

Download all attachments as: .zip

Change History (25)

by alexey.rudy@…, 16 years ago

Attachment: patch_from_r7937.diff added

patch for setting defaulf language in trac.ini

comment:1 by Christian Boos, 16 years ago

Any reason why you can't simply select the language you want from the users preferences? That will also take precedence over the Accept-Language headers.

in reply to:  1 comment:2 by alexey.rudy@…, 16 years ago

Replying to cboos:

Any reason why you can't simply select the language you want from the users preferences? That will also take precedence over the Accept-Language headers.

Yes, I can :) But I want all other users to see the site in particular language by default. They are just users and they don't need to do extra actions.

For example - when I do public wiki in Russian not for hardcore programmers :) That looks strange when site content is in Russian and the interface is in English. And I need to explain everyone that they need to change their language in preferences. It's better to switch always to Russian in such case.

My first suggested implementation is wrong - the setting in trac.ini will override session settings too. It would be right to override only browser headres data. Sorry :))

by alexey.rudy@…, 16 years ago

Attachment: patch_from_r7937.2.diff added

patch for setting defaulf language in trac.ini, version 2

comment:3 by Christian Boos, 15 years ago

Priority: normallow

Ok, but the change should be rewritten so that the lookup in config could be done only if there's nothing stored in the session.

comment:4 by Christian Boos, 15 years ago

Milestone: 0.120.12.1

Can wait for 0.12.1.

comment:5 by nzoltan, 14 years ago

Cc: nzoltan added

I also would like to see this feature, this would be very useful.

comment:6 by Christian Boos, 14 years ago

Milestone: 0.12.10.13
Owner: set to Christian Boos
Status: newassigned

No new enhancements for 0.12.x. This one shouldn't be hard to get in shape for trunk (comment:3), so moving to 0.13 directly.

comment:7 by Christian Boos, 14 years ago

Milestone: 0.130.12.1

I'd like to make an exception for this feature, and get it in 0.12.1 nevertheless.

Updated patch follows.

by Christian Boos, 14 years ago

Attachment: patch_from_r7937.2.2.diff added

enable a default language to be configured in TracIni (based on r10044)

comment:8 by Christian Boos, 14 years ago

Keywords: i18n added

in reply to:  7 ; comment:9 by Remy Blank, 14 years ago

Replying to cboos:

I'd like to make an exception for this feature, and get it in 0.12.1 nevertheless.

He he, changed your mind? :)

Any chance of using the same setting to set the language used by trac-admin? Currently, with my custom locale en_CH, trac-admin outputs text in German for some mysterious reason, even though German is mentioned nowhere in the locale. Begin able to set it to "en" and having trac-admin talk to me in English would be awesome.

in reply to:  9 comment:10 by Christian Boos, 14 years ago

Replying to rblank:

Replying to cboos:

I'd like to make an exception for this feature, and get it in 0.12.1 nevertheless.

He he, changed your mind? :)

Personal reason ;-) But I'd really like that it remains an exception…

Any chance of using the same setting to set the language used by trac-admin? Currently, with my custom locale en_CH, trac-admin outputs text in German for some mysterious reason, even though German is mentioned nowhere in the locale.

That's because of Babel, the following:

>>> import babel
>>> babel.Locale.default()

must give you 'de'.

Being able to set it to "en" and having trac-admin talk to me in English would be awesome.

Yup, see 8117-trac-admin-default_language.patch.

by Christian Boos, 14 years ago

reworked patch_from_r7937.2.2.diff in order to properly support values with undescores (e.g. 'zh_CN')

by Christian Boos, 14 years ago

trac-admin uses the default_language configured for the environment, if any; patch on top of 8117-trac-default_language.patch

comment:11 by Christian Boos, 14 years ago

Keywords: review added

Ok to apply?

in reply to:  11 comment:12 by osimons, 14 years ago

Replying to cboos:

Ok to apply?

Haven't actually tried the patches, but they read well… I'd like to see this committed.

in reply to:  11 comment:13 by Remy Blank, 14 years ago

Replying to cboos:

Ok to apply?

I haven't had a chance to test it yet either, but it reads well here, too. So yes, please go ahead.

comment:14 by Christian Boos, 14 years ago

Resolution: fixed
Status: assignedclosed

Reworked patches applied in [10052:10053], should work as expected, but testing always welcome.

comment:15 by Christian Boos, 14 years ago

Resolution: fixed
Status: closedreopened

Well, when documenting the feature, r10053 feels a bit wrong, as we don't have a way to enforce a user preference anymore (e.g. LANG=en trac-admin ... or LANG=fr trac-admin ...).

Curiously, the above didn't work either before r10053, and this is because the precedence order in babel.Locale.default() is 'LANGUAGE', 'LC_ALL', 'LC_CTYPE', 'LANG' ([Babel:source:tags/0.9.5/babel/core.py@#L630 babel.core]).

This is somehow in accordance with http://www.gnu.org/software/gettext/manual/gettext.html#Users, however that documentation also refers to LANG as the normal environment variable for specifying a locale.

Babel has anyway other problems with the default, as it expects things like fr_FR and won't work with a simple LANG=fr.

Therefore I'd propose the following addition:

  • trac/admin/console.py

     
    168168        self.__env = env = Environment(self.envname)
    169169        # fixup language according to env settings
    170170        if has_babel:
    171             preferred = env.config.get('trac', 'default_language', '')
    172             if preferred:
    173                 translation.activate(get_negotiated_locale([preferred]))
     171            lang = os.environ.get('LANG')
     172            default = env.config.get('trac', 'default_language', '')
     173            negotiated = get_negotiated_locale([lang, default])
     174            if negotiated:
     175                translation.activate(negotiated)
    174176
    175177    ##
    176178    ## Utility methods

in reply to:  15 ; comment:16 by Remy Blank, 14 years ago

Replying to cboos:

Therefore I'd propose the following addition:

There's still a little glitch here, as the locale is only set when opening the environment. However, the welcome banner is already printed at that point, so I get a banner in German and the rest in English. Of course, we cannot get the value of [trac] default_language before opening the environment, but we can at least use LANG.

How about the following addition:

  • trac/admin/console.py

    diff --git a/trac/admin/console.py b/trac/admin/console.py
    a b  
    524526    if has_babel:
    525527        import babel
    526528        try:
    527             locale = babel.Locale.default()
     529            locale = get_negotiated_locale([os.environ.get('LANG')]) \
     530                     or babel.Locale.default()
    528531        except babel.UnknownLocaleError:
    529532            pass
    530533        translation.activate(locale)

in reply to:  16 comment:17 by Christian Boos, 14 years ago

Resolution: fixed
Status: reopenedclosed

Replying to rblank:

How about the following addition: …

Thanks! Both patches integrated in r10060.

comment:18 by Ryan J Ollos, 8 years ago

In r10052, was it intended to have get_negotiate_locale have a different signature when Babel is present as compared to the non-Babel fallback?

This change would make the signatures the same:

  • trac/util/translation.py

    diff --git a/trac/util/translation.py b/trac/util/translation.py
    index bb49e259a..0574333f7 100644
    a b except ImportError: # fall back on 0.11 behavior, i18n functions are no-ops  
    402402    def get_available_locales():
    403403        return []
    404404
    405     def get_negotiated_locale(preferred=None, default=None):
     405    def get_negotiated_locale(preferred_locales):
    406406        return None

comment:19 by Christian Boos, 8 years ago

Sounds good.

comment:20 by Ryan J Ollos, 8 years ago

Thanks, committed to 1.0-stable in r15403, merged in r15404 and r15405.

Modify Ticket

Change Properties
Set your email in Preferences
Action
as closed The owner will remain Christian Boos.
The resolution will be deleted. Next status will be 'reopened'.
to The owner will be changed from Christian Boos to the specified user.

Add Comment


E-mail address and name can be saved in the Preferences .
 
Note: See TracTickets for help on using tickets.