/// Translates the given ID, returning graceful errors. This additionally takes any arguments that should be interpolated. If your
/// i18n system also has variants, they should be specified somehow in the ID.
Translates the given ID, returning graceful errors. This additionally takes any arguments that should be interpolated. If your
i18n system also has variants, they should be specified somehow in the ID.
Translates the given ID returning graceful errors . This additionally takes any arguments that should be interpolated . If your i18n system also has variants they should be specified somehow in the ID .
pub fn translate_checked(
        &self,
        id: &str,
        args: Option<FluentArgs>,
    ) -> Result<String, TranslatorError> {
        let id_str = id.to_string();
        // Deal with the possibility of a specified variant
        let id_vec: Vec<&str> = id_str.split('.').collect();
        let id_str = id_vec[0].to_string();
        let variant = id_vec.get(1);
        // This is the message in the Fluent system, an unformatted translation (still needs variables etc.)
        // This may also be compound, which means it has multiple variants
        let msg = self.bundle.get_message(&id_str);
        let msg = match msg {
            Some(msg) => msg,
            None => {
                return Err(TranslatorError::TranslationIdNotFound {
                    id: id_str,
                    locale: self.locale.clone(),
                })
            }
        };
        // This module accumulates errors in a provided buffer, we'll handle them later
        let mut errors = Vec::new();
        let value = msg.value();
        let mut translation = None; // If it's compound, the requested variant may not exist
        if let Some(value) = value {
            // Non-compound, just one variant
            translation = Some(
                self.bundle
                    .format_pattern(value, args.as_ref(), &mut errors),
            );
        } else {
            // Compound, many variants, one should be specified
            if let Some(variant) = variant {
                for attr in msg.attributes() {
                    // Once we find the requested variant, we don't need to continue (they should all be unique)
                    if &attr.id() == variant {
                        translation = Some(self.bundle.format_pattern(
                            attr.value(),
                            args.as_ref(),
                            &mut errors,
                        ));
                        break;
                    }
                }
            } else {
                return Err(TranslatorError::TranslationFailed {
                    id: id_str,
                    locale: self.locale.clone(),
                    source: "no variant provided for compound message".into(),
                });
            }
        }
        // Check for any errors
        // TODO apparently these aren't all fatal, but how do we know?
        if !errors.is_empty() {
            return Err(TranslatorError::TranslationFailed {
                id: id_str,
                locale: self.locale.clone(),
                source: errors
                    .iter()
                    .map(|e| e.to_string())
                    .collect::<String>()
                    .into(),
            });
        }
        // Make sure we've actually got a translation
        match translation {
            Some(translation) => Ok(translation.to_string()),
            None => Err(TranslatorError::NoTranslationDerived {
                id: id_str,
                locale: self.locale.clone(),
            }),
        }
    }
pub fn translate_checked ( & self , id : & str , args : Option < FluentArgs > , ) -> Result < String , TranslatorError > { let id_str = id . to_string ( ) ; // Deal with the possibility of a specified variant let id_vec : Vec < & str > = id_str . split ( '.' ) . collect ( ) ; let id_str = id_vec [ 0 ] . to_string ( ) ; let variant = id_vec . get ( 1 ) ; // This is the message in the Fluent system, an unformatted translation (still needs variables etc.) // This may also be compound, which means it has multiple variants let msg = self . bundle . get_message ( & id_str ) ; let msg = match msg { Some ( msg ) => msg , None => { return Err ( TranslatorError :: TranslationIdNotFound { id : id_str , locale : self . locale . clone ( ) , } ) } } ; // This module accumulates errors in a provided buffer, we'll handle them later let mut errors = Vec :: new ( ) ; let value = msg . value ( ) ; let mut translation = None ; // If it's compound, the requested variant may not exist if let Some ( value ) = value { // Non-compound, just one variant translation = Some ( self . bundle . format_pattern ( value , args . as_ref ( ) , & mut errors ) , ) ; } else { // Compound, many variants, one should be specified if let Some ( variant ) = variant { for attr in msg . attributes ( ) { // Once we find the requested variant, we don't need to continue (they should all be unique) if & attr . id ( ) == variant { translation = Some ( self . bundle . format_pattern ( attr . value ( ) , args . as_ref ( ) , & mut errors , ) ) ; break ; } } } else { return Err ( TranslatorError :: TranslationFailed { id : id_str , locale : self . locale . clone ( ) , source : " " . into ( ) , } ) ; } } // Check for any errors // TODO apparently these aren't all fatal, but how do we know? if ! errors . is_empty ( ) { return Err ( TranslatorError :: TranslationFailed { id : id_str , locale : self . locale . clone ( ) , source : errors . iter ( ) . map ( | e | e . to_string ( ) ) . collect :: < String > ( ) . into ( ) , } ) ; } // Make sure we've actually got a translation match translation { Some ( translation ) => Ok ( translation . to_string ( ) ) , None => Err ( TranslatorError :: NoTranslationDerived { id : id_str , locale : self . locale . clone ( ) , } ) , } }
Translates the given ID, returning graceful errors.
Translates the given ID returning graceful errors .
&self
id
&str
args
Option<FluentArgs>