Skip to content

Automatically translates TS file format used by Qt Linguist to different languages via Google Translate API

License

Notifications You must be signed in to change notification settings

esutton/i18n-translate-qt-ts

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

56 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

i18n-translate-qt-ts

Automatically translates Qt Linguist TS file to different languages via Google Translate API.

Note: Use requires a commercial Google API key.

Usage:

i18n-translate-qt-ts <googleTranslateApiKey> <workingFolder> <inputLanguage>
  • <googleTranslateApiKey> - Your commercial Google Translate API key
  • <workingFolder> - Folder containing your Qt *.ts files
  • <sourceLanguage> - Source strings language abbreviation, for example, 'en'

Example:

i18n-translate-qt-ts myLongSecretApiKey app/i18n en

A C++ based cross-platform SDK supporting desktops, embedded, Android, and iOS.

Example

Before Translation

File: myproject_es.ts

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE TS>
<TS version="2.0" language="es">
  <context>
      <name>QPushButton</name>
      <message>
          <source>Hello</source>
          <translation type="unfinished"></translation>
      </message>
  </context>
</TS>
After Translation

File: myproject_es.ts

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE TS>
<TS version="2.0" language="es">
  <context>
      <name>QPushButton</name>
      <message>
          <source>Hello</source>
          <translation type="finished">Hola</translation>          
      </message>
  </context>
</TS>

Development - Getting Started

git clone https://github.com/esutton/i18n-translate-qt-ts.git i18n-translate-qt-ts/source
cd 18n-translate-qt-ts/source
yarn
npm install -g

# Check global installed packages
npm list -g --depth=0

# Use environment variable to store your API key
export API_KEY=AIzy0Vj...

# Generate German (de.ts) and Spanish (es.ts) translations 
# from English (*_en.ts) file 
# found in folder test/i18n..

node index.js ${API_KEY} test/simple/unfinished en

Working with Qt Translation

See: qt-translation-example

  1. Add languages you wish to support to your Qt project file (*.pro) and run qmake again
TRANSLATIONS += \
    languages/TranslationExample_en.ts  \
    languages/TranslationExample_de.ts
  1. Use Qt lupdate to auto-generate your initial ts files.

    • From Qt Creator menu select:
      • Tools > External > Linguist > Update Translations (lupdate)
  2. For each <message> found in each TSL file translate the <source> text and paste into <translation>

    • Manually change translation/@type attribute from "unfinished" to "finished".
    • Or delete translation/@type attribute after translation.
    • When you run lupdate again, it will removed all translation/@type=finished attributes
      • Missing type attribute is same as "finished"
      • Y
    • For small one-by-word projects, use free web browser Google Translate.
    • I made this node.js script that requires a commercial Google Translate API key:
  3. Use Qt lrelease to generate compressed *.qm files from translated *.ts files

    • From Qt Creator menu select:
      • Tools > External > Linguist > Release Translations (lrelease)
  4. Copy *.qm files to the embedded resource folder ./res/translation

    • ToDo: Add code to *.pro file to copy *.qm to ./res/translation

Automated Translation Using Google Translate

I created this i18n-translate-qt-ts as a node.js command line utility.

I was inspired by another github project i18n-translate-json used to auto-translate a react native app

Since i18n-translate-json was so helpful, I felt obligated to share i18n-translate-qt-ts.

  • Ulterior motive: I was hoping someone in the community might help improve on it.

Use requires a commercial Google Translate API key:

To add translations to all ./languages/*.ts files in the qt-translation-example project:

# Use environment variable to store your API key
export API_KEY=AIzy0Vj...AIzy0VjQ
node index.js ${API_KEY} languages en
  • Seems to work best if you process in small batches of one or two *.ts files.
    • Too many async queries active?
  • If errors occur, you may need to run multiple times until all strings have translations.
    • Perhaps throttling the API calls might help this.
  • If needed, send auto-translations to a native speaker for improvements.
    • i18n-translate-qt-ts will not update a manual translation unless translation/@type=unfinished or source string is non-empty and <translation> is empty.

About

Automatically translates TS file format used by Qt Linguist to different languages via Google Translate API

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published