Crates.io | qtranslation |
lib.rs | qtranslation |
version | 0.1.7 |
source | src |
created_at | 2022-09-02 10:25:39.833506 |
updated_at | 2022-09-08 15:44:36.096228 |
description | A minimalist translation system based on json for qmetaobject |
homepage | |
repository | https://github.com/cyrgarden/qtranslation |
max_upload_size | |
id | 657308 |
size | 17,131 |
This crate is a very small translation system based on Json for QMetaOjbect. It was originally created for the translation of the Grace software(by Cairn Devices).
Allow simple translation of variables for interfaces that use the qmetaobject crate.
Create a "lang" folder in your "src" folder. In this "lang" folder create a json file for every languages you want to switch to.
For example in "fr_FR.json"
{
"water": "eau",
"battery": "Batterie",
"trains":"age",
"global_infos": "Informations générales",
}
By default, the library will determine the language of your computer using "sys-local::get_local". If the language is not present in the "lang" folder, it will automatically load an "en_GB.json". So please provide an "en_GB.json" file to avoid any crash of your application.
//For example: in my main.rs
//We start with import*
use qmetaobject::prelude::*;
use qtranslation::QTranslater;
fn main(){
/*
Some code...
*/
qml_ressources();
qml_register_type::<QTranslater>(cstr!("QTranslater"), 1, 0, cstr!("QTranslater")); //Register the QTranslater type
let mut engine = QmlEngine::new();
engine.load_file("qrc:/main/qml/main_window.qml".into()); //"main_window" is just my qml first window.But it can be any other page.
engine.exec();
}
QTranslater init() function takes one argument "folder_path" a String. This argument determines the file in which the Qtranslater will look for json files. There are three options :
//Import the QTranslater object
import QTranslater 1.0
ApplicationWindow{
id:my_window
visible:true
width : 800
height:800
QTranslater{
id: lang //Assign an ID you want
Component.onCompleted: {
init(installed_path); // it can be "dev_path" or a custom path like the one in the example below
//init(dev_path);
//init("/home/username/Desktop/random_folder/")
}
}
//Some code...
ColumnLayout{
Text{
text: lang.dict["global_informations"]
}
Text{
text: lang.dict["water"]
}
Text{
text: lang.dict["battery"] + ": 23
font.bold: true
}
Text{
textFormat: Text.RichText
text: "<html><span style='font-weight: bold '>I love</span><span>" +lang.dict.["trains"] +"</span><\html>"
color: "red"
font.family: ubuntu_regular.name
}
Button{
text: "Change to French"
onClicked: lang.change_lang("fr_FR") // To change language
}
}
}