Skip to main content

vI18n

vI18n is a set of Qt (c++) classes for internationalization, translation of cpp application.
It intended to be a replacement of qt lupdate with a more simple and friendly approach similar to id based properties files in Java world. Code and translation are totally separated, All values are dynamically loaded, no need to compil any translation files.

  • Simple, only consist of 1 class to add to your project
  • Id based, no source parsing, no compilation
  • Strings are loaded dynamically on request; qt,js,qml you can render value from dynamic key too
  • You can of course select language at run time
  • You still can use lupdate alongside vI18n while migrating your sources
Some example
C++ / QT
Notification n;
n.setPreviewBody(VI18N.get("file.remove.error").arg(error_code) );
n.setHintValue("x-nemo-icon", "image://theme/icon-s-high-importance");
n.publish();
Qml
// get current language as a string
vi18n.get("main.current.lng").arg(vi18n.languageStr)

// dynamically change value of a string, overriding language
 Label {
    id: str_ex
    text: vi18n.get("hello")
}

function says_hello_in(lng) {
    str_ex.text = vi18n.get("hello", lng);
}
Install

Install is really simple and only require to add the lib to your project. Steps assume you use the default names and locations

  1. Copy the src/vi18n directory into your src directory
  2. Edit the project's .pro file and include v18n.pri reference
  3. into /src/vi18n/i18n directory, add translation for your project

From Github or inside the .tgz you will find a full qtcreator project.

Download

You can get vI18n from the github repository or directly as tgz archives :

 

 

Editing .pro file

Here the .pro file for the sample application

TARGET = harbour-testvi18n

CONFIG += sailfishapp

include(src/vi18n/vi18n.pri)

OTHER_FILES += \
    qml/pages/*.qml \
    qml/cover/*.qml \
    rpm/harbour-testvi18n.changes.in \
    rpm/harbour-testvi18n.spec \
    rpm/harbour-testvi18n.yaml \
    harbour-testvi18n.desktop

SAILFISHAPP_ICONS = 86x86 108x108 128x128 256x256

DISTFILES += \
    qml/harbour-testvi18n.qml \
    qml/pages/*.qml \
    qml/cover/*.qml
.yaml sample

Edit the File section according to this example - assuming you're using the default location.

Files:
  - '%{_bindir}'
  - '%{_datadir}/%{name}'
  - '%{_datadir}/%{name}/i18n'
Properties files name convention

.properties file follow the convention :

{base name of your project}-{2 chars code language}.properties

it also expect a default file without any code as English.

harbour_testvi18n-fi.properties                # translation for Finn
harbour_testvi18n.properties                   # this is the default / fall back file; English too
Properties files format

Properties files are plain text similar to java's one, but with utf-8 encoding instead. Define one key for each line. If a key is the same, You don't need to redefine in on each language, just put it in default file (type english).
One difference with lupudate, you have to add keys yourself. No change either on properties or source files will automatically be done.

# this is a comment
# IMPORTANT ! ALL properties files MUST be in utf8
title=Vi18n Sample

hello = Hello !

# this is a multiline key - end with \. also note the \n for carriage return
main.description=Use the drop down menu below to change the label. \n\
This text too but only in english, french and german (fall back to english).

main.info2=This text only translate in English
main.currentis=Current language is : %1
main.see.more=In all available language

list.title=Hello all
list.description='hello' in each language

menu.title=Select new language
menu.en=english
menu.fr=français
menu.de=deutsch
menu.ru=русский
menu.es=español
menu.fi=suomi

And override in Finn. hello will be translated, the rest stay in english (indeed as defined into the default language config file, english by convention).

hello=hei !
you can download vi18n directly from this link or from github
  • Wen Sep 5 2018- V 1.6.2
    • correct typo on translation prefix
    • removed unused function from header
  • Fri Aug 31 2018- V 1.6
    • added VI18N macro for shorter cpp code
    • can now reload while app running
    • directory name and translation file prefix not static anymore
    • can list available languages
    • get method is not static anymore