Edgewall Software
Modify

Opened 10 years ago

Closed 8 years ago

Last modified 23 months 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
Release Notes:
API 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@… 10 years ago.
patch for setting defaulf language in trac.ini
patch_from_r7937.2.diff (692 bytes ) - added by alexey.rudy@… 10 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 8 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 8 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 8 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)

Changed 10 years ago by alexey.rudy@…

Attachment: patch_from_r7937.diff added

patch for setting defaulf language in trac.ini

comment:1 Changed 10 years ago by Christian Boos

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.

comment:2 in reply to:  1 Changed 10 years ago by alexey.rudy@…

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 :))

Changed 10 years ago by alexey.rudy@…

Attachment: patch_from_r7937.2.diff added

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

comment:3 Changed 9 years ago by Christian Boos

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 Changed 9 years ago by Christian Boos

Milestone: 0.120.12.1

Can wait for 0.12.1.

comment:5 Changed 9 years ago by nzoltan

Cc: nzoltan added

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

comment:6 Changed 9 years ago by Christian Boos

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 Changed 8 years ago by Christian Boos

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.

Changed 8 years ago by Christian Boos

Attachment: patch_from_r7937.2.2.diff added

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

comment:8 Changed 8 years ago by Christian Boos

Keywords: i18n added

comment:9 in reply to:  7 ; Changed 8 years ago by Remy Blank

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.

comment:10 in reply to:  9 Changed 8 years ago by Christian Boos

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.

Changed 8 years ago by Christian Boos

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

Changed 8 years ago by Christian Boos

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

comment:11 Changed 8 years ago by Christian Boos

Keywords: review added

Ok to apply?

comment:12 in reply to:  11 Changed 8 years ago by osimons

Replying to cboos:

Ok to apply?

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

comment:13 in reply to:  11 Changed 8 years ago by Remy Blank

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 Changed 8 years ago by Christian Boos

Resolution: fixed
Status: assignedclosed

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

comment:15 Changed 8 years ago by Christian Boos

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.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

comment:16 in reply to:  15 ; Changed 8 years ago by Remy Blank

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)

comment:17 in reply to:  16 Changed 8 years ago by Christian Boos

Resolution: fixed
Status: reopenedclosed

Replying to rblank:

How about the following addition: …

Thanks! Both patches integrated in r10060.

comment:18 Changed 23 months ago by Ryan J Ollos

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 Changed 23 months ago by Christian Boos

Sounds good.

comment:20 Changed 23 months ago by Ryan J Ollos

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.
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.