11//! Wraps the fluent API and provides easy to use functions and macros for translation
22
3+ use std:: borrow:: Cow ;
4+
35use crate :: { Context , Data , Error } ;
46
57type FluentBundle = fluent:: bundle:: FluentBundle <
@@ -30,27 +32,27 @@ pub(crate) use tr;
3032
3133/// Given a language file and message identifier, returns the translation
3234pub fn format (
33- bundle : & FluentBundle ,
35+ bundle : & ' static FluentBundle ,
3436 id : & str ,
3537 attr : Option < & str > ,
3638 args : Option < & fluent:: FluentArgs < ' _ > > ,
37- ) -> Option < String > {
39+ ) -> Option < Cow < ' static , str > > {
3840 let message = bundle. get_message ( id) ?;
3941 let pattern = match attr {
4042 Some ( attribute) => message. get_attribute ( attribute) ?. value ( ) ,
4143 None => message. value ( ) ?,
4244 } ;
4345 let formatted = bundle. format_pattern ( pattern, args, & mut vec ! [ ] ) ;
44- Some ( formatted. into_owned ( ) )
46+ Some ( formatted)
4547}
4648
4749/// Retrieves the appropriate language file depending on user locale and calls [`format`]
4850pub fn get (
4951 ctx : Context ,
50- id : & str ,
52+ id : & ' static str ,
5153 attr : Option < & str > ,
5254 args : Option < & fluent:: FluentArgs < ' _ > > ,
53- ) -> String {
55+ ) -> Cow < ' static , str > {
5456 let translations = & ctx. data ( ) . translations ;
5557 ctx. locale ( )
5658 // Try to get the language-specific translation
@@ -60,7 +62,7 @@ pub fn get(
6062 // If this message ID is not present in any translation files whatsoever
6163 . unwrap_or_else ( || {
6264 tracing:: warn!( "unknown fluent message identifier `{}`" , id) ;
63- id . to_string ( )
65+ Cow :: Borrowed ( id )
6466 } )
6567}
6668
@@ -97,7 +99,7 @@ pub fn read_ftl() -> Result<Translations, Error> {
9799
98100/// Given a set of language files, fills in command strings and their localizations accordingly
99101pub fn apply_translations (
100- translations : & Translations ,
102+ translations : & ' static Translations ,
101103 commands : & mut [ poise:: Command < Data , Error > ] ,
102104) {
103105 for command in & mut * commands {
@@ -108,21 +110,24 @@ pub fn apply_translations(
108110 Some ( x) => x,
109111 None => continue , // no localization entry => skip localization
110112 } ;
111- command
112- . name_localizations
113- . insert ( locale. clone ( ) , localized_command_name) ;
114- command. description_localizations . insert (
113+
114+ let locale = Cow :: Borrowed ( locale. as_str ( ) ) ;
115+ let name_localizations = command. name_localizations . to_mut ( ) ;
116+ let description_localizations = command. description_localizations . to_mut ( ) ;
117+
118+ name_localizations. push ( ( locale. clone ( ) , localized_command_name) ) ;
119+ description_localizations. push ( (
115120 locale. clone ( ) ,
116121 format ( bundle, & command. name , Some ( "description" ) , None ) . unwrap ( ) ,
117- ) ;
122+ ) ) ;
118123
119124 for parameter in & mut command. parameters {
120125 // Insert localized parameter name and description
121- parameter. name_localizations . insert (
126+ parameter. name_localizations . to_mut ( ) . push ( (
122127 locale. clone ( ) ,
123128 format ( bundle, & command. name , Some ( & parameter. name ) , None ) . unwrap ( ) ,
124- ) ;
125- parameter. description_localizations . insert (
129+ ) ) ;
130+ parameter. description_localizations . to_mut ( ) . push ( (
126131 locale. clone ( ) ,
127132 format (
128133 bundle,
@@ -131,14 +136,14 @@ pub fn apply_translations(
131136 None ,
132137 )
133138 . unwrap ( ) ,
134- ) ;
139+ ) ) ;
135140
136141 // If this is a choice parameter, insert its localized variants
137- for choice in & mut parameter. choices {
138- choice. localizations . insert (
142+ for choice in parameter. choices . to_mut ( ) . iter_mut ( ) {
143+ choice. localizations . to_mut ( ) . push ( (
139144 locale. clone ( ) ,
140145 format ( bundle, & choice. name , None , None ) . unwrap ( ) ,
141- ) ;
146+ ) ) ;
142147 }
143148 }
144149 }
@@ -170,7 +175,7 @@ pub fn apply_translations(
170175 ) ;
171176
172177 // If this is a choice parameter, set the choice names to en-US
173- for choice in & mut parameter. choices {
178+ for choice in parameter. choices . to_mut ( ) . iter_mut ( ) {
174179 choice. name = format ( bundle, & choice. name , None , None ) . unwrap ( ) ;
175180 }
176181 }
0 commit comments