Localization
Frappe Framework includes a powerful built-in tool called "Translation" that allows users to manage and contribute to the localization of your application directly from the user interface. This makes it easy for non-developers to help make the system available in multiple languages.
Understanding the "Translation" DocType?
The "Translation" DocType is a standard Frappe document that stores all translatable text strings for your application. It acts as a bridge between the application's code and the translated phrases users see.
Accessing the "Translation" Doctype
- Log in to your Frappe site as a System Manager or Administrator.
- Click the search bar (Awesome Bar) or press
Ctrl + G/Cmd + G. - Type "Translation" and select it from the dropdown list.
- You will be directed to the Translation list view.
Adding a New Translation
To add a new translation:
- Click on the Add Translation button.
- Fill out the form fields:
- Language: Select the target language for the translation.
- Source Text: Enter the exact text as it appears in the application.
- Translated Text: Enter the translated version of the text.
- Context (optional): Provide context to explain the use case or location of the text.
- Click Save to apply the translation.
For Developers: Marking Strings for Translation
As a developer, it's crucial to mark all user-facing text strings in your code so that they can be identified and translated. In this project, we primarily use two distinct functions for this purpose, depending on the programming language: _() for Python and __() for JavaScript.
Using _() in Python Code
Use the _() function to mark strings for translation in all your Python files (e.g., in DocType server scripts, API methods, or custom reports).
- Purpose: Primarily for translating singular text strings.
- Context in Python: For strings that have the same text but different meanings depending on where they are used (e.g., "Open" as a verb vs. "Open" as an adjective), pass a
contextargument to the_()function.
Usage Examples:
Basic String:
import frappe
def sayhello():
frappe.msgprint(("Welcome to our application!"))
With Placeholders (Formatting): Use Python's .format() method for dynamic values.
import frappe
def get_greeting(user_name):
frappe.msgprint(_("Hello, {0}!").format(user_name))
Using __() in JavaScript Code
Use the __() function for all strings that need translation in your client-side JavaScript files (e.g., DocType client scripts, custom web pages, or UI components).
- Purpose: Translating singular text strings and supporting string formatting with placeholders.
- Context in JavaScript: For strings with specific contexts, combine the context and the string using the
Context::Stringformat within the__()call.
Usage Examples:
Basic String:
frappe.ui.form.on('MyDocType', {
onload: function(frm) {
frappe.show_alert({
message: __("Data loaded successfully."),
indicator: 'green"
});
}
});
With Placeholders (Formatting): You can use either array-based indexed placeholders or object-based named placeholders.
let item_count = 3;
frappe.msgprint(__("{0} items found.", [item_count]));
let user = { name: "Alice", role: "Admin" };
frappe.msgprint(__("User {name} with role {role} logged in.", user));