i18n - How to manually set my locale

6,224 views
Skip to first unread message

Martin Sikora

unread,
Mar 25, 2011, 9:45:07 AM3/25/11
to Chromium-extensions
Hello,
I'm developing a Google Chrome extension and in options page I want to
let users to set which language do they prefer.
I mean default language is English for everyone but if you want to
switch to Spanish or Portuguese you can.

The problem is that Chrome by default sets my local according to
accept-languages. Is there a way how to set local manually?

Thanks, Martin

Michael Gundlach

unread,
Mar 25, 2011, 8:53:39 PM3/25/11
to Martin Sikora, Chromium-extensions
Pass the --lang=zh-CN (or whatever) flag to chrome.exe upon startup.


--
You received this message because you are subscribed to the Google Groups "Chromium-extensions" group.
To post to this group, send email to chromium-...@chromium.org.
To unsubscribe from this group, send email to chromium-extens...@chromium.org.
For more options, visit this group at http://groups.google.com/a/chromium.org/group/chromium-extensions/?hl=en.


Martin Sikora

unread,
Mar 26, 2011, 5:13:29 AM3/26/11
to Chromium-extensions
Thanks Michael,
I know that, but that's not what I was looking for. I wanted to let my
users to set their local in the option page.


On Mar 26, 1:53 am, Michael Gundlach <gundl...@gmail.com> wrote:
> Pass the --lang=zh-CN (or whatever) flag to chrome.exe upon startup.
>
> On Fri, Mar 25, 2011 at 9:45 AM, Martin Sikora
> <lazyeurop...@googlemail.com>wrote:
>
>
>
>
>
>
>
> > Hello,
> > I'm developing a Google Chrome extension and in options page I want to
> > let users to set which language do they prefer.
> > I mean default language is English for everyone but if you want to
> > switch to Spanish or Portuguese you can.
>
> > The problem is that Chrome by default sets my local according to
> > accept-languages. Is there a way how to set local manually?
>
> > Thanks, Martin
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "Chromium-extensions" group.
> > To post to this group, send email to chromium-extensi...@chromium.org.
> > To unsubscribe from this group, send email to
> > chromium-extensions+unsubscr...@chromium.org.

Michael Gundlach

unread,
Mar 26, 2011, 8:19:53 AM3/26/11
to Martin Sikora, Chromium-extensions
Whoops, pardon me for skimming your post.  I'm unaware of a way to change locale from within the browser (or else Google's instructions for testing multiple l10ns wouldn't involve starting chrome with --lang).

If you replaced chrome.i18n with your own i18n library, it could read the user's lang preference, then fetch strings from _locales/ on disk.  I did exactly that for AdBlock, so that i18n would work seamlessly in the Safari version.

See https://adblockforchrome.googlecode.com/svn/trunk/port.js 's chrome.i18n implementation (MIT licensed).  You could change it to read a localStorage option instead of navigator.language when deciding how to localize strings.

Hope this helps more than my last post,
Michael

To post to this group, send email to chromium-...@chromium.org.
To unsubscribe from this group, send email to chromium-extens...@chromium.org.

Martin Sikora

unread,
Mar 26, 2011, 3:30:38 PM3/26/11
to Chromium-extensions
Hi Michael,
I though the only way will be to rewrite chrome.i18n :-). Thanks for
the link.


On Mar 26, 1:19 pm, Michael Gundlach <gundl...@gmail.com> wrote:
> Whoops, pardon me for skimming your post.  I'm unaware of a way to change
> locale from within the browser (or else Google's instructions for testing
> multiple l10ns wouldn't involve starting chrome with --lang).
>
> If you replaced chrome.i18n with your own i18n library, it could read the
> user's lang preference, then fetch strings from _locales/ on disk.  I did
> exactly that for AdBlock, so that i18n would work seamlessly in the Safari
> version.
>
> Seehttps://adblockforchrome.googlecode.com/svn/trunk/port.js's chrome.i18n
> implementation (MIT licensed).  You could change it to read a localStorage
> option instead of navigator.language when deciding how to localize strings.
>
> Hope this helps more than my last post,
> Michael
>
> On Sat, Mar 26, 2011 at 5:13 AM, Martin Sikora
> <lazyeurop...@googlemail.com>wrote:

Michael Gundlach

unread,
Mar 26, 2011, 4:12:48 PM3/26/11
to Martin Sikora, Chromium-extensions
On Sat, Mar 26, 2011 at 3:30 PM, Martin Sikora <lazyeu...@googlemail.com> wrote:
Hi Michael,
I though the only way will be to rewrite chrome.i18n :-). Thanks for
the link.

 
Sure thing.  My rewrite should be encapsulated enough that you can just make a script that says

    chrome.i18n = {
       // paste in all of my chrome.i18n definition here,
       // with 'navigator.language' replaced with
       // 'localStorage.locale_pref || "en" ' throughout.
    };

and you should be good to go.  Be aware that if you want to use chrome.i18n.getMessage from within a content script (as opposed to the background/options pages/other pages), the content script will need to call

    chrome.extension.sendRequest("send-me-l10n-data", function(data) {
        chrome.i18n._setL10nData(data);
        // code from here on can use chrome.i18n.getMessage()
    });
    // code from here on cannot use chrome.i18n.getMessage()

and the background page would then need to run

    chrome.extension.onRequest(function(request, sender, sendResponse) {
        if (request == 'send-me-l10n-data')
            sendResponse(chrome.i18n._getL10nData();
    });


Drop me a line if you do end up using my code -- I'd be happy to know it's getting reused :)

Michael
Reply all
Reply to author
Forward
Message has been deleted
0 new messages