Table of contents
1.
Introduction
2.
Internationalization with T in web2py
2.1.
Determining the Language
2.2.
Translating Variables
3.
Pluralization in web2py
4.
Frequently Asked Questions
4.1.
What is web2py?
4.2.
What is Internationalisation in web2py?
4.3.
What is T in Internationalisation in web2py?
4.4.
What is Pluralization in web2py?
4.5.
What are the divisions of PS?
5.
Conclusion 
Last Updated: Mar 27, 2024
Easy

Internationalisation and Pluralization with T in web2py

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Web2py is a free, open-source internet framework for the rapid development of database-driven web applications. Python is used to write and program it. It is a full-stack framework that provides all of the necessary components for a developer to create fully functional web apps.

Internationalization and Pluralization with T in web2py is one of the most important topics/concepts in web2py. This blog will be your key to a thorough understanding of Internationalisation and Pluralization with T in web2py. Before starting Internationalisation and Pluralization with T in web2py, we request your full concentration, So let's start.

Internationalization and Pluralization with T in web2py

Internationalization with T in web2py

The language translator is the object T. It is a single global instance of the gluon. Language.translator web2py class. T should be used to denote all string constants (and only string constants), 

For example:

a = T("Coding Ninjas")
You can also try this code with Online Python Compiler
Run Code

 

The T object also allows interpolated variables and numerous similar syntaxes:

a = T("hello %s", ('Ninjas', ))
a = T("hello %(name)s", dict(name='Ninjas'))
a = T("hello %s") % ('Ninjas', )
a = T("hello %(name)s") % dict(name='Ninjas')
You can also try this code with Online Python Compiler
Run Code

 

The latter syntax is preferred since it facilitates translation. The first string is translated according to the language file specified, and the name variable is replaced regardless of Language.

Concatenating translated and normal strings are possible:

T("blah ") + name + T(" blah")
You can also try this code with Online Python Compiler
Run Code

 

The following code is also acceptable and generally preferred:

T("blah %(name)s blah", dict(name='Ninjas'))  OR  T("blah %(name)s blah") % dict(name='Ninjas')
You can also try this code with Online Python Compiler
Run Code

 

In all situations, the translation takes place before the variable name is entered into the " percent (names)" column. 

The following option should not be Applicable:

T("blah %(name)s blah" % dict(name='Ninjas'))
You can also try this code with Online Python Compiler
Run Code

 

Internationalisation with T in web2py

Determining the Language

The "Accept-Language" field in the HTTP header determines the requested Language, although this option can be changed programmatically by requesting a specific file, for example, T.force('it-it'), which reads the language file "languages/it-it.py" The administrative interface allows you to create and change language files.

You may also specify a language per string:

T("Hello World", language="it-it")
You can also try this code with Online Python Compiler
Run Code

 

You may totally disable translations by using Force Method:

T.force(None)
You can also try this code with Online Python Compiler
Run Code

 

The translator force method should not be invoked inside a view since string translation is normally evaluated lazily when the view is displayed.

It is possible to deactivate lazy evaluation by using the

T.lazy = False
You can also try this code with Online Python Compiler
Run Code

 

Strings are, therefore, immediately translated by the T operation based on the presently acceptable or forced Language.

Individual strings can also be disabled from the lazy evaluation:

T("Hello World", lazy=False)
You can also try this code with Online Python Compiler
Run Code

 

The following is a prevalent problem. The initial application was written in English. Assume there is a translation file (say, "it-it.py"), and the HTTP client indicates that it supports both English (en) and Italian (it-it) in that order. The following unfavorable scenario occurs: web2py is unaware that the default is written in English (en). As a result, because it only located the Italian translation file, it prefers translating everything into Italian (it-it). It would have utilized the default language strings if the "it-it.py" file had not been discovered (English).

There are two solutions to this problem: build a redundant and superfluous translation language for English, or specify web2py which languages should use the default language strings (the strings coded into the application). 

This is possible with:

T.set_language('en', 'en-en')
You can also try this code with Online Python Compiler
Run Code

 

It saves a list of languages that do not require translation in T.languages and requires a reload of the language files.

Notice that "it" and "it-it" are different languages from the point of view of web2py. To support both of them, one would need two translation files, always lowercase. The same is true for all other languages.

The currently used Language is saved in

T.accepted_language
You can also try this code with Online Python Compiler
Run Code

Translating Variables

T may translate not only texts but also values stored in variables:

a = "Coding Ninjas."
print T(a)
You can also try this code with Online Python Compiler
Run Code

 

The word "test" is translated in this instance, but if it is not discovered and the filesystem is writable, it will be added to the list of words to be translated in the language file.

Take note that this might result in a large amount of file IO, and you may wish to disable it:

T.is_writable = False
You can also try this code with Online Python Compiler
Run Code

 

T is not allowed to dynamically update language files.

Pluralization in web2py

Pluralization in web2py

Web2py has included a strong pluralization mechanism since version 2.0. (PS). This implies that if the text is designated for translation and it is dependent on a number variable, the content may be translated differently depending on the numeric value. 

In English, for example, we could write:

x pen(s)
You can also try this code with Online Python Compiler
Run Code

 

With

a pen (x==1)
10 pens (x==10)
You can also try this code with Online Python Compiler
Run Code

 

There is just one singular form and one plural form in English. The plural form is formed by appending a "-s" or "-es" or by employing an unusual form. web2py allows you to create pluralization rules and exceptions to the default rules for each Language. In reality, web2py already understands the pluralization rules for a wide range of languages. It is understood that Slovenian has one single form and three plural forms (for x==2, x==3, x==4, and x>4). These rules are stored in "gluon/contrib/plural rules/*.py" files, which can be expanded. The administrative interface is used to edit pluralization files to generate explicit pluralizations for terms.

Pluralization in web2py

The PS is off by default. It is activated by the T function's symbols parameter.

As an example:

T("You have %s %%{pen}", symbols=15)
You can also try this code with Online Python Compiler
Run Code

 

or, less verbosely:

T("You have %s %%{pen}", 15)
You can also try this code with Online Python Compiler
Run Code

 

The PS is now active for the word "pen" and the number 15. In English, the outcome will be: "You have fifteen pens." It's worth noting that "pen" has been pluralized into "pens."

The PS is divided into three sections:

  • To indicate words in the T input, use placeholders %%{}.
  • a rule that determines the word form to employ ("gluon/contrib/plural rules/*.py")
  • ("applications/app/languages/plural-*.py") dictionary includes word plural forms

 

Symbols can have a single variable, a list/tuple of variables, or a dictionary as their value.

With a single index, you may utilize several %%{} placeholders:

T("%%{this} %%{is} %s %%{pen}", var)
You can also try this code with Online Python Compiler
Run Code

 

Output:

1   this is 1 pen
 6   these are 6 pens
 13   these are 13 pens
You can also try this code with Online Python Compiler
Run Code

 

A dictionary can be passed to symbols:

T("You %(var1)s %(wordcnt)s %%{pen(wordcnt)}",
    dict(var1="have", wordcnt=10))
You can also try this code with Online Python Compiler
Run Code

 

Output:

You have 10 pens
You can also try this code with Online Python Compiler
Run Code

 

Can you replace "1" with any word you wish by this placeholder %%{?word?number}:

T("%%{this} %%{is} %%{?a?%s} %%{pen}", var)
You can also try this code with Online Python Compiler
Run Code

 

Output:

1   this is a pen
12   these are 12 pens
23   these are 23 pens
You can also try this code with Online Python Compiler
Run Code

Frequently Asked Questions

What is web2py?

Web2py is a free, open-source internet framework for the rapid development of database-driven web applications. Python is used to write and program it. It is a full-stack framework that provides all of the necessary components for a developer to create fully functional web apps.

What is Internationalisation in web2py?

Web2py is designed with Internationalisation in mind. T() should be used to wrap all strings used in templates, controllers, models, and menus.

What is T in Internationalisation in web2py?

The language translator is the object T. It is a single global instance of the gluon. Language.translator web2py class. T should be used to denote all string constants.

What is Pluralization in web2py?

Web2py has included a strong pluralization mechanism since version 2.0. (PS). This implies that if the text is designated for translation and it is dependent on a number variable, the content may be translated differently depending on the numeric value.

What are the divisions of PS?

PS is divided into three sections which are to indicate words in the T input use placeholders %%{}, a rule that determines the word form to employ ("gluon/contrib/plural rules/*.py"), ("applications/app/languages/plural-*.py") dictionary includes word plural forms.

Conclusion 

In this article, we learned about Internationalisation and Pluralization with T in web2py and Language determination in Internationalisation and Pluralization with T  in web2py.

After reading about Internationalisation, and Pluralization with T  in web2py, are you not feeling excited to read/explore more articles on the topic of Ruby? Don't worry; Coding Ninjas has you covered. To learn, see JCL Interview Questions, DNS Interview Questions, and Power BI interview questions Basic.

Refer to our Guided Path on Coding Ninjas Studio to upskill yourself in Data Structures and AlgorithmsCompetitive ProgrammingJavaScriptSystem Design, and many more! 

Do upvote our blogs if you find them helpful and engaging!

Live masterclass