Skip to content

Contributing Translations

Alan edited this page Nov 11, 2023 · 10 revisions

If you want to use Frappe Books in a different language and would like to contribute to adding support for this language please read this.

To contribute translations, you will:

  1. Have to set up a development instance of Frappe Books: ref.
  2. Need a GitHub account to share your contributions: ref.
  3. Create a Pull Request once you have made the updates: ref.

There are two parts to translations:

  1. Ensuring that the translate function is being called on the string.
  2. Ensuring that the translated string is available.

I have explained both in sufficient detail below, if something isn't clear, feel free to create an issue.

Is the translate function is being called?

In Frappe Books user-facing strings are passed through the translate function. If some strings are not being translated in the language you are using then these strings may not be passed through this function.

This is what a translate string looks like:

import { t } from 'fyo';

const translatedString = t`This string will be translated.`;

To translate a string you need to use the above syntax, this is called a Tagged Template. This allows for variables to be easily interpolated into the string being translated:

import { t } from 'fyo';

const interpolatedString = t`Are you sure you want to cancel ${doctype} ${name}?`;

If the string in question is not being translated, first search for the string, you will find some normal string such as this:

const someString = 'Make a payment?';

You'll have to change this to:

import { t } from 'fyo';

const someString = t`Make a payment?`;

Note: don't forget to import t, else there will be errors.

Is the translated string available?

All translated strings are stored in books/translations as .csv files.

Generating the Language File

If you want to generate a new file for your translations then run the following command:

yarn script:translate -l [language_code]

For example, if you want to generate a file for Russian:

yarn script:translate -l ru

Here ru is the ISO 639-1 code for Russian.

If there are locale-wise variations in your language, you can also use locale codes such as en-US which is the code for American English.

Adding Translations

In the translation file, each line will have a different translation, example line:

"${0} ${1} already exists.",,

You will have to add the translation after the comma, example for French:

"${0} ${1} already exists.","${0} ${1} existe déjà",

The ${0} and ${1} are variables that will be inserted when the app is running. You can switch the indices depending on the grammar of your language.

Update languageCodeMap in src/utils/language.ts

Once you add the file you will have to update src/utils/language.ts with the Language and the language code.

Updating the Language File

If you have passed new strings through the translate function and would like to add the translations for them, you will have to re-run the script:

yarn script:translate -l ru

This will add the missing lines to ru.csv (replace ru with your language's code)

Note: running the script without the -l flag will update all the files.

For more info on the script you can run:

yarn script:translate -h

Editing the Translations

You can import the translations to google sheets, edit them and export them as CSV.

I have updated the file, now what?

If you have executed all the earlier steps correctly and have set up a develop instance, on running

yarn electron:serve

you'll find that your strings have been translated.

Now you just have to create a Pull Request. Once your pull request has been merged, your installed version of Frappe Books will receive the updated files. You don't have to wait for a release.


Don'ts of using t

Do not nest translations strings

// VERY BAD
t`Don't ${ t`do` } this.`

Do not try to use the translation function as a function

// VERY BAD
t("Don't do this.")

Do not use a backtick anywhere inside the string

// VERY BAD
t`Don't do \`this.\``

Cans of using t

You can wrap a translation string

t`This is a wrapped string. This is allowed
  because long lines suck. You can also
  insert ${variables} like normal.`

Shameless blog plug: Enabling Translations