XAMPPRocky / fluent-templates

Easily add Fluent to your Rust project.
Apache License 2.0
136 stars 28 forks source link

how to type the returned value for `LOCALES.locales()`? #27

Closed vidyli closed 3 years ago

vidyli commented 3 years ago

I need to get all language identifier for the static loader in a function, how can type the return object please?

use unic_langid::{LanguageIdentifier, langid};
use fluent_templates::{Loader, static_loader};

const US_ENGLISH: LanguageIdentifier = langid!("en-US");
const SIMPLE_CHINESE: LanguageIdentifier = langid!("zh-CN");

static_loader! {
    static LOCALES = {
        locales: "./translations",
        fallback_language: "en-US",
        // Optional: A fluent resource that is shared with every locale.
        core_locales: "./translations/core.ftl",
    };
}

pub fn trans(locale: &LanguageIdentifier, message: &str) -> String {
    LOCALES.lookup(locale, message)
}

//Error:  The lifetime bound for this object type cannot be deduced from context; please supply an explicit bound
pub fn locales() -> Box<dyn Iterator<Item = &LanguageIdentifier> + '_> {
    LOCALES.locales()
}
XAMPPRocky commented 3 years ago

Thank you for your issue! I believe the issue is that you're eliding the lifetime, when you need to specify it as 'static. So changing it to dyn Iterator<Item = &'static LanguageIdentifier> + '_ should work. If that doesn't work you can always use .collect to return a Vec, LOCALES.locales().collect().

I'm going to close this, as this is more of a general Rust issue than a fluent-templates specific issue, but feel free to keep commenting if neither of the above solutions work for you.