mirror of
https://github.com/ankitects/anki.git
synced 2025-09-19 06:22:22 -04:00
expose StringsGroup enum in protobuf
This commit is contained in:
parent
61b9f70ab9
commit
cc99f221be
4 changed files with 38 additions and 33 deletions
|
@ -12,6 +12,13 @@ message BackendInit {
|
||||||
string locale_folder_path = 5;
|
string locale_folder_path = 5;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
enum StringsGroup {
|
||||||
|
OTHER = 0;
|
||||||
|
TEST = 1;
|
||||||
|
MEDIA_CHECK = 2;
|
||||||
|
CARD_TEMPLATES = 3;
|
||||||
|
}
|
||||||
|
|
||||||
// 1-15 reserved for future use; 2047 for errors
|
// 1-15 reserved for future use; 2047 for errors
|
||||||
|
|
||||||
message BackendInput {
|
message BackendInput {
|
||||||
|
|
|
@ -10,6 +10,7 @@ use unic_langid::LanguageIdentifier;
|
||||||
|
|
||||||
pub use fluent::fluent_args as tr_args;
|
pub use fluent::fluent_args as tr_args;
|
||||||
|
|
||||||
|
pub use crate::backend_proto::StringsGroup;
|
||||||
/// Helper for creating args with &strs
|
/// Helper for creating args with &strs
|
||||||
#[macro_export]
|
#[macro_export]
|
||||||
macro_rules! tr_strs {
|
macro_rules! tr_strs {
|
||||||
|
@ -34,13 +35,6 @@ pub enum LanguageDialect {
|
||||||
ChineseTaiwan,
|
ChineseTaiwan,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Clone, Copy)]
|
|
||||||
pub enum TranslationFile {
|
|
||||||
Test,
|
|
||||||
MediaCheck,
|
|
||||||
CardTemplates,
|
|
||||||
}
|
|
||||||
|
|
||||||
fn lang_dialect(lang: LanguageIdentifier) -> Option<LanguageDialect> {
|
fn lang_dialect(lang: LanguageIdentifier) -> Option<LanguageDialect> {
|
||||||
use LanguageDialect as L;
|
use LanguageDialect as L;
|
||||||
Some(match lang.get_language() {
|
Some(match lang.get_language() {
|
||||||
|
@ -61,24 +55,28 @@ fn dialect_file_locale(dialect: LanguageDialect) -> &'static str {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn data_for_fallback(file: TranslationFile) -> String {
|
fn ftl_fallback_for_group(group: StringsGroup) -> String {
|
||||||
match file {
|
match group {
|
||||||
TranslationFile::Test => include_str!("../../tests/support/test.ftl"),
|
StringsGroup::Other => "",
|
||||||
TranslationFile::MediaCheck => include_str!("media-check.ftl"),
|
StringsGroup::Test => include_str!("../../tests/support/test.ftl"),
|
||||||
TranslationFile::CardTemplates => include_str!("card-template-rendering.ftl"),
|
StringsGroup::MediaCheck => include_str!("media-check.ftl"),
|
||||||
|
StringsGroup::CardTemplates => include_str!("card-template-rendering.ftl"),
|
||||||
}
|
}
|
||||||
.to_string()
|
.to_string()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn data_for_lang_and_file(
|
fn localized_ftl_for_group(
|
||||||
dialect: LanguageDialect,
|
dialect: LanguageDialect,
|
||||||
file: TranslationFile,
|
group: StringsGroup,
|
||||||
locales: &Path,
|
locales: &Path,
|
||||||
) -> Option<String> {
|
) -> Option<String> {
|
||||||
let path = locales.join(dialect_file_locale(dialect)).join(match file {
|
let path = locales
|
||||||
TranslationFile::Test => "test.ftl",
|
.join(dialect_file_locale(dialect))
|
||||||
TranslationFile::MediaCheck => "media-check.ftl",
|
.join(match group {
|
||||||
TranslationFile::CardTemplates => "card-template-rendering.ftl",
|
StringsGroup::Other => "",
|
||||||
|
StringsGroup::Test => "test.ftl",
|
||||||
|
StringsGroup::MediaCheck => "media-check.ftl",
|
||||||
|
StringsGroup::CardTemplates => "card-template-rendering.ftl",
|
||||||
});
|
});
|
||||||
fs::read_to_string(&path)
|
fs::read_to_string(&path)
|
||||||
.map_err(|e| {
|
.map_err(|e| {
|
||||||
|
@ -139,8 +137,8 @@ impl I18n {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get(&self, file: TranslationFile) -> I18nCategory {
|
pub fn get(&self, group: StringsGroup) -> I18nCategory {
|
||||||
I18nCategory::new(&*self.langs, &*self.supported, file, &self.locale_folder)
|
I18nCategory::new(&*self.langs, &*self.supported, group, &self.locale_folder)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -154,24 +152,24 @@ impl I18nCategory {
|
||||||
pub fn new(
|
pub fn new(
|
||||||
langs: &[LanguageIdentifier],
|
langs: &[LanguageIdentifier],
|
||||||
preferred: &[LanguageDialect],
|
preferred: &[LanguageDialect],
|
||||||
file: TranslationFile,
|
group: StringsGroup,
|
||||||
locale_folder: &Path,
|
locale_folder: &Path,
|
||||||
) -> Self {
|
) -> Self {
|
||||||
let mut bundles = Vec::with_capacity(preferred.len() + 1);
|
let mut bundles = Vec::with_capacity(preferred.len() + 1);
|
||||||
for dialect in preferred {
|
for dialect in preferred {
|
||||||
if let Some(text) = data_for_lang_and_file(*dialect, file, locale_folder) {
|
if let Some(text) = localized_ftl_for_group(*dialect, group, locale_folder) {
|
||||||
if let Some(mut bundle) = get_bundle(text, langs) {
|
if let Some(mut bundle) = get_bundle(text, langs) {
|
||||||
if cfg!(test) {
|
if cfg!(test) {
|
||||||
bundle.set_use_isolating(false);
|
bundle.set_use_isolating(false);
|
||||||
}
|
}
|
||||||
bundles.push(bundle);
|
bundles.push(bundle);
|
||||||
} else {
|
} else {
|
||||||
error!("Failed to create bundle for {:?} {:?}", dialect, file);
|
error!("Failed to create bundle for {:?} {:?}", dialect, group);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut fallback_bundle = get_bundle(data_for_fallback(file), langs).unwrap();
|
let mut fallback_bundle = get_bundle(ftl_fallback_for_group(group), langs).unwrap();
|
||||||
if cfg!(test) {
|
if cfg!(test) {
|
||||||
fallback_bundle.set_use_isolating(false);
|
fallback_bundle.set_use_isolating(false);
|
||||||
}
|
}
|
||||||
|
@ -220,7 +218,7 @@ impl I18nCategory {
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod test {
|
mod test {
|
||||||
use crate::i18n::{dialect_file_locale, lang_dialect, TranslationFile};
|
use crate::i18n::{dialect_file_locale, lang_dialect, StringsGroup};
|
||||||
use crate::i18n::{tr_args, I18n, LanguageDialect};
|
use crate::i18n::{tr_args, I18n, LanguageDialect};
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
use unic_langid::LanguageIdentifier;
|
use unic_langid::LanguageIdentifier;
|
||||||
|
@ -246,7 +244,7 @@ mod test {
|
||||||
fn i18n() {
|
fn i18n() {
|
||||||
// English fallback
|
// English fallback
|
||||||
let i18n = I18n::new(&["zz"], "../../tests/support");
|
let i18n = I18n::new(&["zz"], "../../tests/support");
|
||||||
let cat = i18n.get(TranslationFile::Test);
|
let cat = i18n.get(StringsGroup::Test);
|
||||||
assert_eq!(cat.tr("valid-key"), "a valid key");
|
assert_eq!(cat.tr("valid-key"), "a valid key");
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
cat.tr("invalid-key"),
|
cat.tr("invalid-key"),
|
||||||
|
@ -271,7 +269,7 @@ mod test {
|
||||||
let mut d = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
|
let mut d = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
|
||||||
d.push("tests/support");
|
d.push("tests/support");
|
||||||
let i18n = I18n::new(&["ja_JP"], d);
|
let i18n = I18n::new(&["ja_JP"], d);
|
||||||
let cat = i18n.get(TranslationFile::Test);
|
let cat = i18n.get(StringsGroup::Test);
|
||||||
assert_eq!(cat.tr("valid-key"), "キー");
|
assert_eq!(cat.tr("valid-key"), "キー");
|
||||||
assert_eq!(cat.tr("only-in-english"), "not translated");
|
assert_eq!(cat.tr("only-in-english"), "not translated");
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
|
|
|
@ -3,7 +3,7 @@
|
||||||
|
|
||||||
use crate::cloze::expand_clozes_to_reveal_latex;
|
use crate::cloze::expand_clozes_to_reveal_latex;
|
||||||
use crate::err::{AnkiError, Result};
|
use crate::err::{AnkiError, Result};
|
||||||
use crate::i18n::{tr_args, tr_strs, I18n, TranslationFile};
|
use crate::i18n::{tr_args, tr_strs, I18n, StringsGroup};
|
||||||
use crate::latex::extract_latex;
|
use crate::latex::extract_latex;
|
||||||
use crate::media::col::{
|
use crate::media::col::{
|
||||||
for_every_note, get_note_types, mark_collection_modified, open_or_create_collection_db,
|
for_every_note, get_note_types, mark_collection_modified, open_or_create_collection_db,
|
||||||
|
@ -89,7 +89,7 @@ where
|
||||||
|
|
||||||
pub fn summarize_output(&self, output: &mut MediaCheckOutput) -> String {
|
pub fn summarize_output(&self, output: &mut MediaCheckOutput) -> String {
|
||||||
let mut buf = String::new();
|
let mut buf = String::new();
|
||||||
let cat = self.i18n.get(TranslationFile::MediaCheck);
|
let cat = self.i18n.get(StringsGroup::MediaCheck);
|
||||||
|
|
||||||
// top summary area
|
// top summary area
|
||||||
buf += &cat.trn("missing-count", tr_args!["count"=>output.missing.len()]);
|
buf += &cat.trn("missing-count", tr_args!["count"=>output.missing.len()]);
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
||||||
|
|
||||||
use crate::err::{AnkiError, Result, TemplateError};
|
use crate::err::{AnkiError, Result, TemplateError};
|
||||||
use crate::i18n::{tr_strs, I18n, I18nCategory, TranslationFile};
|
use crate::i18n::{tr_strs, I18n, I18nCategory, StringsGroup};
|
||||||
use crate::template_filters::apply_filters;
|
use crate::template_filters::apply_filters;
|
||||||
use lazy_static::lazy_static;
|
use lazy_static::lazy_static;
|
||||||
use nom;
|
use nom;
|
||||||
|
@ -196,7 +196,7 @@ fn parse_inner<'a, I: Iterator<Item = TemplateResult<Token<'a>>>>(
|
||||||
}
|
}
|
||||||
|
|
||||||
fn template_error_to_anki_error(err: TemplateError, q_side: bool, i18n: &I18n) -> AnkiError {
|
fn template_error_to_anki_error(err: TemplateError, q_side: bool, i18n: &I18n) -> AnkiError {
|
||||||
let cat = i18n.get(TranslationFile::CardTemplates);
|
let cat = i18n.get(StringsGroup::CardTemplates);
|
||||||
let header = cat.tr(if q_side {
|
let header = cat.tr(if q_side {
|
||||||
"front-side-problem"
|
"front-side-problem"
|
||||||
} else {
|
} else {
|
||||||
|
@ -508,7 +508,7 @@ pub fn render_card(
|
||||||
|
|
||||||
// check if the front side was empty
|
// check if the front side was empty
|
||||||
if !qtmpl.renders_with_fields(context.nonempty_fields) {
|
if !qtmpl.renders_with_fields(context.nonempty_fields) {
|
||||||
let cat = i18n.get(TranslationFile::CardTemplates);
|
let cat = i18n.get(StringsGroup::CardTemplates);
|
||||||
let info = format!(
|
let info = format!(
|
||||||
"{}<br><a href='{}'>{}</a>",
|
"{}<br><a href='{}'>{}</a>",
|
||||||
cat.tr("empty-front"),
|
cat.tr("empty-front"),
|
||||||
|
|
Loading…
Reference in a new issue