ki18n-rs/README.md

72 lines
2.5 KiB
Markdown
Raw Normal View History

# KI18n Crate for Rust
[![Crates.io](https://img.shields.io/crates/v/ki18n)](https://crates.io/crates/ki18n)
[![Documentation](https://docs.rs/ki18n/badge.svg)](https://docs.rs/ki18n/)
2022-01-04 13:44:43 +00:00
## Introduction
KI18n is a cross-platform internationalization framework used by KDE applications. This crate
is meant to allow using KI18n with Rust and [qmetaobject-rs](https://github.com/woboq/qmetaobject-rs) crate.
2022-01-04 13:44:43 +00:00
## Motivation
I love KDE. I have been using it as my primary DE for quite a while. I also like Rust Programming Language.
Currently, it is nearly impossible to use any KDE frameworks from Rust without some C++ FFI. While FFI is
never easy, complexity with C++ is exponentially more than plain C due to the Object-Oriented nature of C++,
which is entirely different from Rust's somewhat functional design.
2021-12-14 16:16:12 +00:00
2022-01-04 13:44:43 +00:00
If possible, I would like the Tier-1 KDE Frameworks to be usable from pragmatic Rust in a somewhat natural
fashion. I am currently interested in KDE frameworks that are usable with QML.
2021-12-14 16:16:12 +00:00
2022-01-04 13:44:43 +00:00
## Requirements
2021-12-01 14:09:36 +00:00
This crate requires KF5I18n to be installed or at least present in the system.
2022-01-04 13:44:43 +00:00
### Ubuntu
2021-12-01 14:09:36 +00:00
``` shell
sudo apt install libkf5i18n-dev
2022-01-04 13:44:43 +00:00
```rust
2021-12-01 14:09:36 +00:00
## Arch Linux
``` shell
sudo pacman -S ki18n
```
2022-01-04 13:44:43 +00:00
## Custom Location for KF5I18n
The crate searches from KF5I18n using either the environment variables (`KF_VERSION`,
`KF_INCLUDE_PATH` and `KF_LIBRARY_PATH`) if they are set or uses `kf5-config` to find the paths.
2021-12-01 14:09:36 +00:00
2022-01-04 13:44:43 +00:00
## Example
```rust
use cstr::cstr;
use qmetaobject::prelude::*;
2022-01-04 13:58:00 +00:00
use ki18n::klocalizedcontext::KLocalizedContext;
fn main() {
let mut engine = QmlEngine::new();
KLocalizedContext::init_from_engine(&engine);
engine.load_data(r#"
import QtQuick 2.6
import QtQuick.Controls 2.0 as Controls
import QtQuick.Layouts 1.2
import org.kde.kirigami 2.13 as Kirigami
2022-01-04 13:44:43 +00:00
// Base element, provides basic features needed for all kirigami applications
Kirigami.ApplicationWindow {
// ID provides unique identifier to reference this element
id: root
2022-01-04 13:44:43 +00:00
// Window title
// i18nc is useful for adding context for translators, also lets strings be changed for different languages
title: i18nc("@title:window", "Hello World")
2022-01-04 13:44:43 +00:00
// Initial page to be loaded on app load
pageStack.initialPage: Kirigami.Page {
2022-01-04 13:44:43 +00:00
Controls.Label {
// Center label horizontally and vertically within parent element
anchors.centerIn: parent
text: i18n("Hello World!")
}
}
}
"#.into());
engine.exec();
}
```