Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
E
edx-platform
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
edx
edx-platform
Commits
bb58e4b3
Commit
bb58e4b3
authored
Oct 01, 2014
by
Christina Roberts
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #5458 from edx/andya/lms-underscore-i18n
Translate strings in LMS Underscore templates
parents
db82d456
191aba96
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
52 additions
and
21 deletions
+52
-21
conf/locale/babel_underscore.cfg
+2
-0
docs/en_us/developers/source/i18n.rst
+50
-21
No files found.
conf/locale/babel_underscore.cfg
View file @
bb58e4b3
...
@@ -3,6 +3,8 @@
...
@@ -3,6 +3,8 @@
input_encoding = utf-8
input_encoding = utf-8
[underscore: common/templates/**.underscore]
[underscore: common/templates/**.underscore]
input_encoding = utf-8
input_encoding = utf-8
[underscore: lms/templates/**.underscore]
input_encoding = utf-8
[extractors]
[extractors]
underscore = django_babel_underscore:extract
underscore = django_babel_underscore:extract
docs/en_us/developers/source/i18n.rst
View file @
bb58e4b3
...
@@ -50,7 +50,7 @@ See the detailed Style Guidelines at the end for details.
...
@@ -50,7 +50,7 @@ See the detailed Style Guidelines at the end for details.
Editing source files
Editing source files
********************
********************
While editing source files (including Python, Java
s
cript, or HTML template
While editing source files (including Python, Java
S
cript, or HTML template
files), use the appropriate conventions. There are a few things to know how to
files), use the appropriate conventions. There are a few things to know how to
do:
do:
...
@@ -70,13 +70,14 @@ The code samples below show how to do each of these things for:
...
@@ -70,13 +70,14 @@ The code samples below show how to do each of these things for:
* `Python source code`_
* `Python source code`_
* `Django template files`_
* `Django template files`_
* `Mako template files`_
* `Mako template files`_
* `Java
s
cript files`_
* `Java
S
cript files`_
* `Coffeescript files`_
* `Coffeescript files`_
* `Underscore template files`_
* `Other kinds of code`_
* `Other kinds of code`_
Note that you have to take into account not just the programming language involved,
Note that you have to take into account not just the programming language involved,
but the type of file: Java
s
cript embedded in an HTML Mako template is treated differently
but the type of file: Java
S
cript embedded in an HTML Mako template is treated differently
than Java
s
cript in a pure .js file.
than Java
S
cript in a pure .js file.
Python source code
Python source code
==================
==================
...
@@ -139,36 +140,36 @@ functions first. Here's a Mako template example::
...
@@ -139,36 +140,36 @@ functions first. Here's a Mako template example::
## Translators: message to the translator
## Translators: message to the translator
${_("Welcome!")}
${_("Welcome!")}
Java
s
cript files
Java
S
cript files
================
================
.. highlight:: javascript
.. highlight:: javascript
In order to internationalize Java
script, first the html
template (base.html)
In order to internationalize Java
Script, first the HTML
template (base.html)
must load a special Java
s
cript library (and Django must be configured to serve
must load a special Java
S
cript library (and Django must be configured to serve
it)::
it)::
<script type="text/javascript" src="jsi18n/"></script>
<script type="text/javascript" src="jsi18n/"></script>
Then, in Java
s
cript files (`*.js`)::
Then, in Java
S
cript files (`*.js`)::
// Translators: this will help the translator.
// Translators: this will help the translator.
var message = gettext('Welcome!');
var message = gettext('Welcome!');
Note that Java
s
cript embedded in HTML in a Mako template file is handled
Note that Java
S
cript embedded in HTML in a Mako template file is handled
differently. There, you use the Mako syntax even within the Java
s
cript.
differently. There, you use the Mako syntax even within the Java
S
cript.
Coffeescript files
Coffeescript files
==================
==================
.. highlight:: coffeescript
.. highlight:: coffeescript
Coffeescript files are compiled to Java
s
cript files, so it works mostly like
Coffeescript files are compiled to Java
S
cript files, so it works mostly like
Java
s
cript::
Java
S
cript::
`// Translators: this will help the translator.`
`// Translators: this will help the translator.`
message = gettext('Hey there!')
message = gettext('Hey there!')
# Interpolation has to be done in Java
s
cript, not Coffeescript:
# Interpolation has to be done in Java
S
cript, not Coffeescript:
message = gettext("Error getting student progress url for '<%= student_id %>'.")
message = gettext("Error getting student progress url for '<%= student_id %>'.")
full_message = _.template(message, {student_id: unique_student_identifier})
full_message = _.template(message, {student_id: unique_student_identifier})
...
@@ -179,7 +180,7 @@ native Coffeescript features that break the extraction from the .js files:
...
@@ -179,7 +180,7 @@ native Coffeescript features that break the extraction from the .js files:
concatenation in the .js file, so string extraction won't work.
concatenation in the .js file, so string extraction won't work.
2. You cannot use Coffeescript comments for translator comments, since they are
2. You cannot use Coffeescript comments for translator comments, since they are
not passed through to the Java
s
cript file.
not passed through to the Java
S
cript file.
::
::
...
@@ -198,6 +199,36 @@ native Coffeescript features that break the extraction from the .js files:
...
@@ -198,6 +199,36 @@ native Coffeescript features that break the extraction from the .js files:
.. highlight:: python
.. highlight:: python
Underscore template files
=========================
.. highlight:: underscore
Underscore template files are used in conjunction with JavaScript, and so the
same techniques are used for localization. Ensure that the i18n JavaScript
library has already been loaded, and then use the regular i18n functions
such as ``gettext`` and ``interpolate`` from your template.
For example::
<div class="group-visibility-label">
<% if (group.group_name) { %>
<%-
interpolate(
gettext('This post is visible only to %(group_name)s.'),
{group_name: group.group_name},
true
)
%>
<% } else { %>
<%- gettext('This post is visible to everyone.') %>
<% } %>
</div>
Note: it is recommended that you use ``<%-`` for all translated strings
as this will HTML escape the string before including it in the page. This
ensures that translations are free to use non-HTML characters.
Other kinds of code
Other kinds of code
===================
===================
...
@@ -206,8 +237,6 @@ We have not yet established guidelines for internationalizing the following.
...
@@ -206,8 +237,6 @@ We have not yet established guidelines for internationalizing the following.
* Course content (such as subtitles for videos)
* Course content (such as subtitles for videos)
* Documentation (written for Sphinx as .rst files)
* Documentation (written for Sphinx as .rst files)
* Client-side templates written using Underscore.
Building and testing your code
Building and testing your code
...
@@ -278,7 +307,7 @@ This dummy text is distinguished by extra accent characters. If you see plain
...
@@ -278,7 +307,7 @@ This dummy text is distinguished by extra accent characters. If you see plain
English instead (without these accents), it most likely means the string has
English instead (without these accents), it most likely means the string has
not been externalized yet. To fix this:
not been externalized yet. To fix this:
* Find the string in the source tree (either in Python, Java
s
cript, or HTML
* Find the string in the source tree (either in Python, Java
S
cript, or HTML
template code).
template code).
* Refer to the above coding guidelines to make sure it has been externalized
* Refer to the above coding guidelines to make sure it has been externalized
...
@@ -413,7 +442,7 @@ Be aware of nested syntax
...
@@ -413,7 +442,7 @@ Be aware of nested syntax
=========================
=========================
When translating strings in templated files, you have to be careful of nested
When translating strings in templated files, you have to be careful of nested
syntax. For example, consider this Java
s
cript fragment in a Mako template::
syntax. For example, consider this Java
S
cript fragment in a Mako template::
<script>
<script>
var feeling = '${_("I love you.")';
var feeling = '${_("I love you.")';
...
@@ -425,9 +454,9 @@ When rendered for a French speaker, it will produce this::
...
@@ -425,9 +454,9 @@ When rendered for a French speaker, it will produce this::
var feeling = 'Je t'aime.';
var feeling = 'Je t'aime.';
</script>
</script>
which is now invalid Java
s
cript. This can be avoided by using double-quotes
which is now invalid Java
S
cript. This can be avoided by using double-quotes
for the Java
s
cript string. The better solution is to use a filtering function
for the Java
S
cript string. The better solution is to use a filtering function
that properly escapes the string for Java
s
cript use::
that properly escapes the string for Java
S
cript use::
<%!
<%!
from django.utils.translation import ugettext as _
from django.utils.translation import ugettext as _
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment