ki18n/
klocalizedstring.rs

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