1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
use cpp::{cpp, cpp_class};
use qttypes::{QByteArray, QString, QStringList};
use std::ffi::CStr;
cpp! {{
#include <KLocalizedString>
#include <QtCore/QByteArray>
}}
cpp_class!(
/// Wrapper around [`KLocalizedString`][class] class
///
/// [class]: https://api.kde.org/frameworks/ki18n/html/classKLocalizedString.html
#[derive(Default, Clone)]
pub unsafe struct KLocalizedString as "KLocalizedString"
);
impl KLocalizedString {
/// Set Application Domain
/// # Example
/// ```
/// use std::ffi::CString;
/// use ki18n::prelude::KLocalizedString;
///
/// let domain = CString::new("Application_Domain").unwrap();
/// KLocalizedString::set_application_domain(&domain);
/// ```
pub fn set_application_domain(domain: &CStr) {
let domain_ptr = domain.as_ptr();
cpp!(unsafe [domain_ptr as "char*"] {
KLocalizedString::setApplicationDomain(domain_ptr);
});
}
/// Get Application Domain
pub fn application_domain() -> QByteArray {
cpp!(unsafe [] -> QByteArray as "QByteArray" {
return KLocalizedString::applicationDomain();
})
}
/// Get the languages for which translations will be made.
/// Returned languages are ordered with decreasing priority.
pub fn languages() -> QStringList {
cpp!(unsafe [] -> QStringList as "QStringList" {
return KLocalizedString::languages();
})
}
/// Set the languages for which translations will be made.
/// This overrides the languages provided by the locale. Languages should be ordered with decreasing priority.
/// TODO: Add Test
pub fn set_languages(languages: QStringList) {
cpp!(unsafe [languages as "QStringList"] {
KLocalizedString::setLanguages(languages);
});
}
/// Clear override languages.
/// This clears the override languages, going back to those provided by the locale.
/// TODO: Add Test.
pub fn clear_languages() {
cpp!(unsafe [] {
KLocalizedString::clearLanguages();
})
}
/// Load locales for a domain from a specific location.
/// This is useful for resources which have their translation files outside of the usual $XDG_DATA_DIRS/locales location.
pub fn add_domain_locale_dir(domain: QByteArray, path: QString) {
cpp!(unsafe [domain as "QByteArray", path as "QString"] {
KLocalizedString::addDomainLocaleDir(domain, path);
})
}
/// Check whether the translation catalog file in the given language for the set application translation domain exists.
pub fn is_application_translated_into(language: QString) -> bool {
cpp!(unsafe [language as "QString"] -> bool as "bool" {
return KLocalizedString::isApplicationTranslatedInto(language);
})
}
/// Find a path to the localized file for the given original path.
/// This is intended mainly for non-text resources (images, sounds, etc). Text resources should be handled in more specific ways.
pub fn localized_file_path(file_path: QString) -> QString {
cpp!(unsafe [file_path as "QString"] -> QString as "QString" {
return KLocalizedString::localizedFilePath(file_path);
})
}
/// Remove accelerator marker from a UI text label.
pub fn remove_accelerator_marker(label: QString) -> QString {
cpp!(unsafe [label as "QString"] -> QString as "QString" {
return KLocalizedString::removeAcceleratorMarker(label);
})
}
/// Finalize the translation.
/// Creates translated QString, with placeholders substituted by arguments given by KLocalizedString::subs methods.
pub fn to_qstring(&self) -> QString {
cpp!(unsafe [self as "const KLocalizedString *"] -> QString as "QString" {
return self->toString();
})
}
/// Indicate to look for translation in the given domain.
/// TODO: Add Test
pub fn with_domain(&self, domain: &CStr) -> KLocalizedString {
let domain_ptr = domain.as_ptr();
cpp!(unsafe [self as "const KLocalizedString *", domain_ptr as "char*"] -> KLocalizedString as "KLocalizedString" {
return self->withDomain(domain_ptr);
})
}
/// Indicate to look for translation only in given languages.
/// TODO: Add Test
pub fn with_languages(&self, languages: QStringList) -> KLocalizedString {
cpp!(unsafe [self as "const KLocalizedString *", languages as "QStringList"] -> KLocalizedString as "KLocalizedString" {
return self->withLanguages(languages);
})
}
}