update remaining TR references in rslib

This commit is contained in:
Damien Elmes 2021-03-27 11:07:36 +10:00
parent c45ab78b73
commit f485efce16
8 changed files with 111 additions and 133 deletions

View File

@ -9,13 +9,16 @@ from re import Match
import stringcase import stringcase
TR_REF = re.compile(r'\.trn\(\s*TR::([^,) ]+),\s+tr_(?:strs|args)!\[((?:"[A-z0-9_]+"\s*=>[^=]+?)+)]\s*,?\s*\)') TR_REF = re.compile(
r'\.trn\(\s*TR::([^,) ]+),\s+tr_(?:strs|args)!\[((?:"[A-z0-9_]+"\s*=>[^=]+?)+)]\s*,?\s*\)'
)
TR_ARG_INNER = re.compile(r'"([A-z0-9_]+)"\s*=>(?!=>)+,?\s*') TR_ARG_INNER = re.compile(r'"([A-z0-9_]+)"\s*=>(?!=>)+,?\s*')
# eg "count"=>output.trash_count, "megs"=>megs # eg "count"=>output.trash_count, "megs"=>megs
def rewrite_tr_args(args: str) -> str: def rewrite_tr_args(args: str) -> str:
return TR_ARG_INNER.sub("", args) return TR_ARG_INNER.sub("", args)
def repl(m: Match) -> str: def repl(m: Match) -> str:
name = stringcase.snakecase(m.group(1)) name = stringcase.snakecase(m.group(1))
args = rewrite_tr_args(m.group(2)) args = rewrite_tr_args(m.group(2))

View File

@ -7,7 +7,7 @@ use std::sync::{Arc, Mutex};
use crate::{ use crate::{
backend_proto as pb, backend_proto as pb,
dbcheck::DatabaseCheckProgress, dbcheck::DatabaseCheckProgress,
i18n::{tr_args, I18n, TR}, i18n::I18n,
media::sync::MediaSyncProgress, media::sync::MediaSyncProgress,
sync::{FullSyncProgress, NormalSyncProgress, SyncStage}, sync::{FullSyncProgress, NormalSyncProgress, SyncStage},
}; };
@ -70,16 +70,12 @@ pub(super) fn progress_to_proto(progress: Option<Progress>, i18n: &I18n) -> pb::
SyncStage::Finalizing => i18n.sync_checking(), SyncStage::Finalizing => i18n.sync_checking(),
} }
.to_string(); .to_string();
let added = i18n.trn( let added = i18n
TR::SyncAddedUpdatedCount, .sync_added_updated_count(p.local_update, p.remote_update)
tr_args![ .into();
"up"=>p.local_update, "down"=>p.remote_update], let removed = i18n
); .sync_media_removed_count(p.local_remove, p.remote_remove)
let removed = i18n.trn( .into();
TR::SyncMediaRemovedCount,
tr_args![
"up"=>p.local_remove, "down"=>p.remote_remove],
);
pb::progress::Value::NormalSync(pb::progress::NormalSync { pb::progress::Value::NormalSync(pb::progress::NormalSync {
stage, stage,
added, added,

View File

@ -139,17 +139,18 @@ impl AnkiError {
SearchErrorKind::UnclosedQuote => i18n.search_unclosed_quote(), SearchErrorKind::UnclosedQuote => i18n.search_unclosed_quote(),
SearchErrorKind::MissingKey => i18n.search_missing_key(), SearchErrorKind::MissingKey => i18n.search_missing_key(),
SearchErrorKind::UnknownEscape(ctx) => { SearchErrorKind::UnknownEscape(ctx) => {
i18n.search_unknown_escape(ctx.replace('`', "'")).into() i18n.search_unknown_escape(ctx.replace('`', "'"))
} }
SearchErrorKind::InvalidState(state) => i18n SearchErrorKind::InvalidState(state) => {
.search_invalid_argument("is:", state.replace('`', "'")) i18n.search_invalid_argument("is:", state.replace('`', "'"))
.into(), }
SearchErrorKind::InvalidFlag => i18n.search_invalid_flag(), SearchErrorKind::InvalidFlag => i18n.search_invalid_flag(),
SearchErrorKind::InvalidPropProperty(prop) => i18n SearchErrorKind::InvalidPropProperty(prop) => {
.search_invalid_argument("prop:", prop.replace('`', "'")) i18n.search_invalid_argument("prop:", prop.replace('`', "'"))
.into(), }
SearchErrorKind::InvalidPropOperator(ctx) => { SearchErrorKind::InvalidPropOperator(ctx) => {
i18n.search_invalid_prop_operator(ctx.as_str()).into() i18n.search_invalid_prop_operator(ctx.as_str())
} }
SearchErrorKind::Regex(text) => { SearchErrorKind::Regex(text) => {
format!("<pre>`{}`</pre>", text.replace('`', "'")).into() format!("<pre>`{}`</pre>", text.replace('`', "'")).into()
@ -160,32 +161,31 @@ impl AnkiError {
.search_invalid_number( .search_invalid_number(
context.replace('`', "'"), context.replace('`', "'"),
provided.replace('`', "'"), provided.replace('`', "'"),
) ),
.into(),
SearchErrorKind::InvalidWholeNumber { provided, context } => i18n SearchErrorKind::InvalidWholeNumber { provided, context } => i18n
.search_invalid_whole_number( .search_invalid_whole_number(
context.replace('`', "'"), context.replace('`', "'"),
provided.replace('`', "'"), provided.replace('`', "'"),
) ),
.into(),
SearchErrorKind::InvalidPositiveWholeNumber { provided, context } => i18n SearchErrorKind::InvalidPositiveWholeNumber { provided, context } => i18n
.search_invalid_positive_whole_number( .search_invalid_positive_whole_number(
context.replace('`', "'"), context.replace('`', "'"),
provided.replace('`', "'"), provided.replace('`', "'"),
) ),
.into(),
SearchErrorKind::InvalidNegativeWholeNumber { provided, context } => i18n SearchErrorKind::InvalidNegativeWholeNumber { provided, context } => i18n
.search_invalid_negative_whole_number( .search_invalid_negative_whole_number(
context.replace('`', "'"), context.replace('`', "'"),
provided.replace('`', "'"), provided.replace('`', "'"),
) ),
.into(),
SearchErrorKind::InvalidAnswerButton { provided, context } => i18n SearchErrorKind::InvalidAnswerButton { provided, context } => i18n
.search_invalid_answer_button( .search_invalid_answer_button(
context.replace('`', "'"), context.replace('`', "'"),
provided.replace('`', "'"), provided.replace('`', "'"),
) ),
.into(),
}; };
i18n.search_invalid_search(reason).into() i18n.search_invalid_search(reason).into()
} }

View File

@ -4,13 +4,7 @@
use super::{ use super::{
cardgen::group_generated_cards_by_note, CardGenContext, NoteType, NoteTypeID, NoteTypeKind, cardgen::group_generated_cards_by_note, CardGenContext, NoteType, NoteTypeID, NoteTypeKind,
}; };
use crate::{ use crate::{card::CardID, collection::Collection, err::Result, notes::NoteID};
card::CardID,
collection::Collection,
err::Result,
i18n::{tr_args, TR},
notes::NoteID,
};
use std::collections::HashSet; use std::collections::HashSet;
use std::fmt::Write; use std::fmt::Write;
@ -119,13 +113,10 @@ impl Collection {
"<li class={}>[anki:nid:{}] {}</li>", "<li class={}>[anki:nid:{}] {}</li>",
class, class,
note.nid, note.nid,
self.i18n.trn( self.i18n.empty_cards_count_line(
TR::EmptyCardsCountLine, note.empty.len(),
tr_args![ note.current_count,
"empty_count"=>note.empty.len(), templates
"existing_count"=>note.current_count,
"template_names"=>templates
],
) )
) )
.unwrap(); .unwrap();

View File

@ -37,38 +37,37 @@ pub enum Op {
impl Op { impl Op {
pub fn describe(self, i18n: &I18n) -> String { pub fn describe(self, i18n: &I18n) -> String {
let key = match self { match self {
Op::AddDeck => TR::UndoAddDeck, Op::AddDeck => i18n.undo_add_deck(),
Op::AddNote => TR::UndoAddNote, Op::AddNote => i18n.undo_add_note(),
Op::AnswerCard => TR::UndoAnswerCard, Op::AnswerCard => i18n.undo_answer_card(),
Op::Bury => TR::StudyingBury, Op::Bury => i18n.studying_bury(),
Op::RemoveDeck => TR::DecksDeleteDeck, Op::RemoveDeck => i18n.decks_delete_deck(),
Op::RemoveNote => TR::StudyingDeleteNote, Op::RemoveNote => i18n.studying_delete_note(),
Op::RenameDeck => TR::ActionsRenameDeck, Op::RenameDeck => i18n.actions_rename_deck(),
Op::ScheduleAsNew => TR::UndoForgetCard, Op::ScheduleAsNew => i18n.undo_forget_card(),
Op::SetDueDate => TR::ActionsSetDueDate, Op::SetDueDate => i18n.actions_set_due_date(),
Op::Suspend => TR::StudyingSuspend, Op::Suspend => i18n.studying_suspend(),
Op::UnburyUnsuspend => TR::UndoUnburyUnsuspend, Op::UnburyUnsuspend => i18n.undo_unbury_unsuspend(),
Op::UpdateCard => TR::UndoUpdateCard, Op::UpdateCard => i18n.undo_update_card(),
Op::UpdateDeck => TR::UndoUpdateDeck, Op::UpdateDeck => i18n.undo_update_deck(),
Op::UpdateNote => TR::UndoUpdateNote, Op::UpdateNote => i18n.undo_update_note(),
Op::UpdatePreferences => TR::PreferencesPreferences, Op::UpdatePreferences => i18n.preferences_preferences(),
Op::UpdateTag => TR::UndoUpdateTag, Op::UpdateTag => i18n.undo_update_tag(),
Op::SetDeck => TR::BrowsingChangeDeck, Op::SetDeck => i18n.browsing_change_deck(),
Op::SetFlag => TR::UndoSetFlag, Op::SetFlag => i18n.undo_set_flag(),
Op::FindAndReplace => TR::BrowsingFindAndReplace, Op::FindAndReplace => i18n.browsing_find_and_replace(),
Op::ClearUnusedTags => TR::BrowsingClearUnusedTags, Op::ClearUnusedTags => i18n.browsing_clear_unused_tags(),
Op::SortCards => TR::BrowsingReschedule, Op::SortCards => i18n.browsing_reschedule(),
Op::RenameTag => TR::ActionsRenameTag, Op::RenameTag => i18n.actions_rename_tag(),
Op::RemoveTag => TR::ActionsRemoveTag, Op::RemoveTag => i18n.actions_remove_tag(),
Op::ReparentTag => TR::ActionsRenameTag, Op::ReparentTag => i18n.actions_rename_tag(),
Op::ReparentDeck => TR::ActionsRenameDeck, Op::ReparentDeck => i18n.actions_rename_deck(),
Op::BuildFilteredDeck => TR::UndoBuildFilteredDeck, Op::BuildFilteredDeck => i18n.undo_build_filtered_deck(),
Op::RebuildFilteredDeck => TR::UndoBuildFilteredDeck, Op::RebuildFilteredDeck => i18n.undo_build_filtered_deck(),
Op::EmptyFilteredDeck => TR::StudyingEmpty, Op::EmptyFilteredDeck => i18n.studying_empty(),
}; }
.into()
i18n.tr(key).to_string()
} }
} }

View File

@ -1,21 +1,21 @@
// Copyright: Ankitects Pty Ltd and contributors // Copyright: Ankitects Pty Ltd and contributors
// 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::i18n::{tr_args, I18n, TR}; use crate::i18n::I18n;
/// Short string like '4d' to place above answer buttons. /// Short string like '4d' to place above answer buttons.
pub fn answer_button_time(seconds: f32, i18n: &I18n) -> String { pub fn answer_button_time(seconds: f32, i18n: &I18n) -> String {
let span = Timespan::from_secs(seconds).natural_span(); let span = Timespan::from_secs(seconds).natural_span();
let args = tr_args!["amount" => span.as_rounded_unit_for_answer_buttons()]; let amount = span.as_rounded_unit_for_answer_buttons();
let key = match span.unit() { match span.unit() {
TimespanUnit::Seconds => TR::SchedulingAnswerButtonTimeSeconds, TimespanUnit::Seconds => i18n.scheduling_answer_button_time_seconds(amount),
TimespanUnit::Minutes => TR::SchedulingAnswerButtonTimeMinutes, TimespanUnit::Minutes => i18n.scheduling_answer_button_time_minutes(amount),
TimespanUnit::Hours => TR::SchedulingAnswerButtonTimeHours, TimespanUnit::Hours => i18n.scheduling_answer_button_time_hours(amount),
TimespanUnit::Days => TR::SchedulingAnswerButtonTimeDays, TimespanUnit::Days => i18n.scheduling_answer_button_time_days(amount),
TimespanUnit::Months => TR::SchedulingAnswerButtonTimeMonths, TimespanUnit::Months => i18n.scheduling_answer_button_time_months(amount),
TimespanUnit::Years => TR::SchedulingAnswerButtonTimeYears, TimespanUnit::Years => i18n.scheduling_answer_button_time_years(amount),
}; }
i18n.trn(key, args) .into()
} }
/// Short string like '4d' to place above answer buttons. /// Short string like '4d' to place above answer buttons.
@ -42,16 +42,15 @@ pub fn time_span(seconds: f32, i18n: &I18n, precise: bool) -> String {
} else { } else {
span.as_rounded_unit() span.as_rounded_unit()
}; };
let args = tr_args!["amount" => amount]; match span.unit() {
let key = match span.unit() { TimespanUnit::Seconds => i18n.scheduling_time_span_seconds(amount),
TimespanUnit::Seconds => TR::SchedulingTimeSpanSeconds, TimespanUnit::Minutes => i18n.scheduling_time_span_minutes(amount),
TimespanUnit::Minutes => TR::SchedulingTimeSpanMinutes, TimespanUnit::Hours => i18n.scheduling_time_span_hours(amount),
TimespanUnit::Hours => TR::SchedulingTimeSpanHours, TimespanUnit::Days => i18n.scheduling_time_span_days(amount),
TimespanUnit::Days => TR::SchedulingTimeSpanDays, TimespanUnit::Months => i18n.scheduling_time_span_months(amount),
TimespanUnit::Months => TR::SchedulingTimeSpanMonths, TimespanUnit::Years => i18n.scheduling_time_span_years(amount),
TimespanUnit::Years => TR::SchedulingTimeSpanYears, }
}; .into()
i18n.trn(key, args)
} }
const SECOND: f32 = 1.0; const SECOND: f32 = 1.0;

View File

@ -7,14 +7,13 @@ pub fn studied_today(cards: u32, secs: f32, i18n: &I18n) -> String {
let span = Timespan::from_secs(secs).natural_span(); let span = Timespan::from_secs(secs).natural_span();
let amount = span.as_unit(); let amount = span.as_unit();
let unit = span.unit().as_str(); let unit = span.unit().as_str();
let secs_per = if cards > 0 { let secs_per_card = if cards > 0 {
secs / (cards as f32) secs / (cards as f32)
} else { } else {
0.0 0.0
}; };
let args = tr_args!["amount" => amount, "unit" => unit, i18n.statistics_studied_today(unit, secs_per_card, amount, cards)
"cards" => cards, "secs-per-card" => secs_per]; .into()
i18n.trn(TR::StatisticsStudiedToday, args)
} }
impl Collection { impl Collection {

View File

@ -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, TR}; use crate::i18n::I18n;
use crate::{cloze::add_cloze_numbers_in_string, template_filters::apply_filters}; use crate::{cloze::add_cloze_numbers_in_string, template_filters::apply_filters};
use lazy_static::lazy_static; use lazy_static::lazy_static;
use nom::branch::alt; use nom::branch::alt;
@ -247,11 +247,11 @@ 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 header = i18n.tr(if q_side { let header = if q_side {
TR::CardTemplateRenderingFrontSideProblem i18n.card_template_rendering_front_side_problem()
} else { } else {
TR::CardTemplateRenderingBackSideProblem i18n.card_template_rendering_back_side_problem()
}); };
let details = localized_template_error(i18n, err); let details = localized_template_error(i18n, err);
let more_info = i18n.card_template_rendering_more_info(); let more_info = i18n.card_template_rendering_more_info();
let info = format!( let info = format!(
@ -273,31 +273,22 @@ fn localized_template_error(i18n: &I18n, err: TemplateError) -> String {
TemplateError::ConditionalNotOpen { TemplateError::ConditionalNotOpen {
closed, closed,
currently_open, currently_open,
} => { } => if let Some(open) = currently_open {
if let Some(open) = currently_open { i18n.card_template_rendering_wrong_conditional_closed(
i18n.trn( format!("{{{{/{}}}}}", closed),
TR::CardTemplateRenderingWrongConditionalClosed, format!("{{{{/{}}}}}", open),
tr_strs!(
"found"=>format!("{{{{/{}}}}}", closed),
"expected"=>format!("{{{{/{}}}}}", open)),
) )
} else { } else {
i18n.trn( i18n.card_template_rendering_conditional_not_open(
TR::CardTemplateRenderingConditionalNotOpen, format!("{{{{/{}}}}}", closed),
tr_strs!( format!("{{{{#{}}}}}", closed),
"found"=>format!("{{{{/{}}}}}", closed), format!("{{{{^{}}}}}", closed),
"missing1"=>format!("{{{{#{}}}}}", closed),
"missing2"=>format!("{{{{^{}}}}}", closed)
),
) )
} }
} .into(),
TemplateError::FieldNotFound { field, filters } => i18n.trn( TemplateError::FieldNotFound { field, filters } => i18n
TR::CardTemplateRenderingNoSuchField, .card_template_rendering_no_such_field(format!("{{{{{}{}}}}}", filters, field), field)
tr_strs!( .into(),
"found"=>format!("{{{{{}{}}}}}", filters, field),
"field"=>field),
),
} }
} }