mirror of
https://github.com/ankitects/anki.git
synced 2026-01-20 01:39:00 -05:00
Instead of generating a fluent.proto file with a giant enum, create a .json file representing the translations that downstream consumers can use for code generation. This enables the generation of a separate method for each translation, with a docstring that shows the actual text, and any required arguments listed in the function signature. The codebase is still using the old enum for now; updating it will need to come in future commits, and the old enum will need to be kept around, as add-ons are referencing it. Other changes: - move translation code into a separate crate - store the translations on a per-file/module basis, which will allow us to avoid sending 1000+ strings on each JS page load in the future - drop the undocumented support for external .ftl files, that we weren't using - duplicate strings in translation files are now checked for at build time - fix i18n test failing when run outside Bazel - drop slog dependency in i18n module
51 lines
1.7 KiB
Rust
51 lines
1.7 KiB
Rust
// Copyright: Ankitects Pty Ltd and contributors
|
|
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
|
|
|
use super::Backend;
|
|
use crate::{
|
|
backend_proto as pb,
|
|
prelude::*,
|
|
scheduler::timespan::{answer_button_time, time_span},
|
|
};
|
|
use fluent::FluentValue;
|
|
pub(super) use pb::i18n_service::Service as I18nService;
|
|
|
|
impl I18nService for Backend {
|
|
fn translate_string(&self, input: pb::TranslateStringIn) -> Result<pb::String> {
|
|
let key = input.key;
|
|
let map = input
|
|
.args
|
|
.iter()
|
|
.map(|(k, v)| (k.as_str(), translate_arg_to_fluent_val(&v)))
|
|
.collect();
|
|
|
|
Ok(self.i18n.trn2(key as usize, map).into())
|
|
}
|
|
|
|
fn format_timespan(&self, input: pb::FormatTimespanIn) -> Result<pb::String> {
|
|
use pb::format_timespan_in::Context;
|
|
Ok(match input.context() {
|
|
Context::Precise => time_span(input.seconds, &self.i18n, true),
|
|
Context::Intervals => time_span(input.seconds, &self.i18n, false),
|
|
Context::AnswerButtons => answer_button_time(input.seconds, &self.i18n),
|
|
}
|
|
.into())
|
|
}
|
|
|
|
fn i18n_resources(&self, _input: pb::Empty) -> Result<pb::Json> {
|
|
serde_json::to_vec(&self.i18n.resources_for_js())
|
|
.map(Into::into)
|
|
.map_err(Into::into)
|
|
}
|
|
}
|
|
|
|
fn translate_arg_to_fluent_val(arg: &pb::TranslateArgValue) -> FluentValue {
|
|
use pb::translate_arg_value::Value as V;
|
|
match &arg.value {
|
|
Some(val) => match val {
|
|
V::Str(s) => FluentValue::String(s.into()),
|
|
V::Number(f) => FluentValue::Number(f.into()),
|
|
},
|
|
None => FluentValue::String("".into()),
|
|
}
|
|
}
|