Notebook translation tips

9 views
Skip to first unread message

Sergey Semerikov

unread,
Jul 22, 2009, 5:03:40 AM7/22/09
to sage-devel
I tried to translate the interface of the notebook using gettext and
faced with the need to make several changes to the source code:

1) List of files for internationalization:

- data/extcode/notebook/templates/ (may not be used?):
failed_login.template
login.template
register.template
yes_no.template

- devel/sage/sage/server/notebook/templates/ :
account_recovery.html
account_settings.html
banner.html
base.html
base_authenticated.html
docs.html
error_message.html
history.html
list_top.html
login.html
registration.html
search.html
source_code.html
template_error.html
top_bar.html
upload.html
user_management.html
worksheet_listing.html
yes_no.html

- devel/sage/sage/server/notebook/ :

avatars.py
cell.py
colorize.py
conf.py
js.py
mailsender.py
notebook.py
notebook_object.py
register.py
run_notebook.py
sage_email.py
sagetex.py
smtpsend.py
template.py
tutorial.py
twist.py
user.py
worksheet.py

2) At the beginning of each file in devel/sage/sage/server/notebook/
must specify the encoding UTF-8:

# -*- coding: utf-8 -*-

Config file for notebook, generated by function run() in file
run_notebook.py also must have such encoding:

config.write("""
# -*- coding: utf-8 -
*- ###
new addition
####################################################################

3) Function convert_seconds_to_meaningful_time_span() must be
simplified, because in different languages differently shaped end of
words that denote the seconds, minutes and hours, depending on the
numerical value. For example, in Russian we must have different ends
for 1, 2-4, 5-... (in English - only for 1, 2-...). The proposed
simplification:

def convert_seconds_to_meaningful_time_span(t):
if t < 60:
s = int(t)
return "%d second(s)"%s
if t < 3600:
m = int(t/60)
return "%d minute(s)"%m
if t < 3600*24:
h = int(t/3600)
return "%d hour(s)"%h
d = int(t/(3600*24))
return "%d day(s)"%d

4) TEMPLATE_PATH in template.py should contain an indication of the
language catalog, e.g.:
TEMPLATE_PATH = sage.misc.misc.SAGE_ROOT + '/devel/sage/sage/server/
notebook/en/templates'

5) Language selector (listbox) must be a part of the interface.

6) In local/notebook/javascript/tiny_mce/langs must be language packs
for TinyMCE. The TinyMCE configuration, generated in cell.py (lines
199-213), must have one more parameter:

language: "XX"

"Stupid" (without gettext, directly changed files) Russian translation
can be download from http://semerikov.googlepages.com/ru.tar.bz2
Unpack, copy, run 'sage -b' and try translated notebook. Screenshots:
http://semerikov.googlepages.com/russian_sage_login.png and
http://semerikov.googlepages.com/russian_sage_home.png
I think that making language packs for notebook interface (and docs
translation) will help to popularize Sage. I can also do the
translation into Ukrainian, but I need in a language pack template for
notebook (to easily add new languages as is done for the
documentation).

William Stein

unread,
Jul 23, 2009, 1:32:22 AM7/23/09
to sage-...@googlegroups.com
On Wed, Jul 22, 2009 at 2:03 AM, Sergey Semerikov<seme...@gmail.com> wrote:
>
> I tried to translate the interface of the notebook using gettext and
> faced with the need to make several changes to the source code:

Thanks for working on this!!

(1) What is gettext? I've never heard of it?

(2) Thanks for making the list below. Now we need to figure out how
to get this into Sage in an incremental way.

(3) Somebody else just popped up on the sage list a few days ago who
is moving all the html generation code to jinja templates. His code
is here:

http://trac.sagemath.org/sage_trac/ticket/6568

This will obviously I think be relevant to what you're doing.

I don't have a lot of time to work on the notebook now, but will in Sept - Dec.

William
--
William Stein
Associate Professor of Mathematics
University of Washington
http://wstein.org

Dan Drake

unread,
Jul 23, 2009, 8:19:19 AM7/23/09
to sage-...@googlegroups.com
On Wed, 22 Jul 2009 at 10:32PM -0700, William Stein wrote:
> On Wed, Jul 22, 2009 at 2:03 AM, Sergey Semerikov<seme...@gmail.com> wrote:
> >
> > I tried to translate the interface of the notebook using gettext and
> > faced with the need to make several changes to the source code:
>
> Thanks for working on this!!
>
> (1) What is gettext? I've never heard of it?

My (vastly incomplete) understanding is that gettext makes string
translations easy. You write your program so that instead of just
printing something, it calls a string-printing function. That function
knows what language should be used, so it looks up the right string and
prints that. It makes it easy for people to contribute translations; you
have a list of strings, probably in a "base" language (for us, that'd be
English) and it's easy for someone to get that list, translate
everything into some new language, and we just ship that along with our
code.

I would *love* to have better support for this.

Dan

--
--- Dan Drake <dr...@kaist.edu>
----- KAIST Department of Mathematical Sciences
------- http://mathsci.kaist.ac.kr/~drake

signature.asc

Sergey Semerikov

unread,
Jul 23, 2009, 6:04:38 PM7/23/09
to sage-devel
On Jul 23, 8:32 am, William Stein <wst...@gmail.com> wrote:

> (1) What is gettext?  I've never heard of it?

Good describe of gettext we can find in http://docs.python.org/library/gettext.html
(23.1.3. Internationalizing your programs and modules). The main
advantage of such
way is an ability to change languages on the fly.

> (2) Thanks for making the list below.  Now we need to figure out how
> to get this into Sage in an incremental way.

I did some steps:

1. First step: simply add

# -*- coding: utf-8 -*-

at first line of every .py-file. In run_notebook.py additionally
encoding
must be at lines 170-172:

config.write(_("""
# -*- coding: utf-8 -*-
####################################################################

Then I rewrite convert_seconds_to_meaningful_time_span (as described
in
first messages).

2. Next step: every string constant, which can be translated, was
converted
from
raise ValueError, 'invalid failure type'
to
raise ValueError, _('invalid failure type')
Before begin of the string add underscore and open bracket, after end
-
close bracket.

Changed files at step 1, 2, please, download from
http://semerikov.googlepages.com/notebook_modify.tar.bz2

3. Then we need to define function "_" as described in 23.1.3.1
in every module (I don't do it yet):

import gettext
t = gettext.translation('modulename', '/usr/local/sage/....../locale')
_ = t.lgettext

all.py (or some main module) must contain

import gettext
gettext.install('applicationname', '/usr/local/sage/....../locale',
unicode=1)

4. For every changed file running xgettext or Tools/i18n/pygettext.py:

python pygettext.py *.py

Resulting file (http://semerikov.googlepages.com/messages.pot) is an
template for any notebook translation. To do new translation you must
rename messages.pot at LANG_CODE.po (e.g., for Korean - kr.po) and
write translation in msgstr (empty in template):

#: avatars.py:65
msgid "invalid failure type"
msgstr ""


> (3) Somebody else just popped up on the sage list a few days ago who
> is moving all the html generation code to jinja templates.

Thanks, I don't touch html-templates.

> I don't have a lot of time to work on the notebook now, but will in Sept - Dec.

Steps 1-4 will produce only minor (cosmetic) code changes. I hope,
it
can be done quickly (the main reason - easy and fast translation: for
example,
try Korean/English notebook at http://math1.skku.ac.kr/).

Pavel Sutyrin

unread,
Jul 26, 2009, 12:48:43 AM7/26/09
to sage-devel
Sergei,

> I tried to translate the interface of the notebook using gettext [...]

That's great idea, somebody should already have started this :)

> 3) Function convert_seconds_to_meaningful_time_span() must be
> simplified [...] For example, in Russian we must have different ends
for 1, 2-4, 5-... (in English - only for 1, 2-...).

What if we generalize accurately this 'endings' rule?..

Personally, I've tired sick of robotic word-endings everywhere,
like "Dear Mr(Mrs) Helen", "1 file(s) copied", etc.
Can't refrain from pointing to a handwritten (!) Russian invite letter
containing
incorrect grammatical case (like 'Dear Mr. Helen') and 'generalized'
part '(with your husband/wife)':

http://img.artlebedev.ru/kovodstvo/sections/98/robo.gif
(originally: http://www.artlebedev.ru/kovodstvo/sections/98/)

So, I see it reasonable for language support in Sage to complement
numeral translations strings with some ending pattern,
which should be langauge-specific, say, encoded in another string.
As for Russian, this task is often offered to 1st year students
in programming ;) and breaks down to analyze last 1 and 2 digits of a
number.

All the best,

--Pavel.
Reply all
Reply to author
Forward
0 new messages