Struct zng::l10n::L10N

source ·
pub struct L10N;
Expand description

Localization service.

Implementations§

source§

impl L10N

source

pub fn load(&self, source: impl L10nSource)

Change the localization resources to source.

All active variables and handles will be updated to use the new source.

source

pub fn load_dir(&self, dir: impl Into<PathBuf>)

Start watching the dir for dir/{lang}/*.ftl and dir/{lang}/deps/*/*/*.ftl files.

The available_langs variable maintains an up-to-date list of locale files found, the files are only loaded when needed, and also are watched to update automatically.

source

pub fn available_langs( &self, ) -> Box<dyn VarBoxed<Arc<LangMap<HashMap<LangFilePath, PathBuf>>>>>

Available localization files.

The value maps lang to one or more files, the files can be from the project dir/{lang}/{file}.ftl or from dependencies dir/{lang}/deps/{pkg-name/{pkg-version}/{file}.ftl.

Note that this map will include any file in the source dir that has a name that is a valid lang!, that includes the template.ftl file and test pseudo-locales such as qps-ploc.ftl.

source

pub fn available_langs_status(&self) -> Box<dyn VarBoxed<LangResourceStatus>>

Status of the available_langs list.

This will be NotAvailable before the first call to load_dir, then it changes to Loading, then Loaded or Error.

Note that this is the status of the resource list, not of each individual resource, you can use LangResource::status for that.

source

pub async fn wait_available_langs(&self)

Waits until available_langs_status is not Loading.

source

pub fn app_lang(&self) -> ArcCowVar<Langs, ArcVar<Langs>>

Gets a read-write variable that sets the preferred languages for the app. Lang not available are ignored until they become available, the first language in the vec is the most preferred.

The value is the same as sys_lang, if set the variable disconnects from system lang.

Note that the LANG_VAR is used in message requests, the default value of that context variable is this one.

source

pub fn sys_lang(&self) -> ReadOnlyVar<Langs, ArcVar<Langs>>

Gets a read-only variable that is the current system language.

The variable will update when the view-process notifies that the config has changed. Is empty if the system locale cannot be retrieved.

source

pub fn message( &self, file: impl Into<LangFilePath>, id: impl Into<Txt>, attribute: impl Into<Txt>, fallback: impl Into<Txt>, ) -> L10nMessageBuilder

Gets a read-only variable that is a localized message in the localization context where the variable is first used. The variable will update when the contextual language changes.

If the message has variable arguments they must be provided using L10nMessageBuilder::arg, the returned variable will also update when the arg variables update.

Prefer using the l10n! macro instead of this method, the macro does compile time validation.

§Params
  • file: Name of the resource file, in the default directory layout the file is searched at dir/{lang}/{file}.ftl, if empty the file is searched at dir/{lang}/_.ftl. Only a single file name is valid, no other path components allowed. Note that the file can also be a full LangFilePath that includes dependency package info. Those files are searched in dir/{lang}/deps/{pkg-name}/{pkg-version}/{file}.ftl.
  • id: Message identifier inside the resource file.
  • attribute: Attribute of the identifier, leave empty to not use an attribute.
  • fallback: Message to use when a localized message cannot be found.

The id and attribute is only valid if it starts with letter [a-zA-Z], followed by any letters, digits, _ or - [a-zA-Z0-9_-]*.

Panics if any parameter is invalid.

source

pub fn lang_resource( &self, lang: impl Into<Lang>, file: impl Into<LangFilePath>, ) -> LangResource

Gets a handle to the lang file resource.

The resource will be loaded and stay in memory until all clones of the handle are dropped, this can be used to pre-load resources so that localized messages find it immediately avoiding flashing the fallback text in the UI.

If the resource directory or file changes it is auto-reloaded, just like when a message variable held on the resource does.

§Params
  • lang: Language identifier.
  • file: Name of the resource file, in the default directory layout the file is searched at dir/{lang}/{file}.ftl, if empty the file is searched at dir/{lang}/_.ftl. Only a single file name is valid, no other path components allowed. Note that the file can also be a full LangFilePath that includes dependency package info. Those files are searched in dir/{lang}/deps/{pkg-name}/{pkg-version}/{file}.ftl.

Panics if the file is invalid.

source

pub async fn wait_lang(&self, lang: impl Into<Lang>) -> LangResources

Gets a handle to all resource files for the lang after they load.

This awaits for the available langs to load, then collect an awaits for all lang files.

source

pub async fn wait_first( &self, langs: impl Into<Langs>, ) -> (Option<Lang>, LangResources)

Gets a handle to all resource files of the first lang in langs that is available and loaded.

This awaits for the available langs to load, then collect an awaits for all lang files.

source

pub fn bind_command_meta( &self, file: impl Into<LangFilePath>, cmd: Command, meta_name: impl Into<Txt>, meta_value: Box<dyn VarBoxed<Txt>>, )

Bind the command metadata to a message.

This is automatically called by command! instances that set the metadata l10n!: true or l10n!: "file".

Auto Trait Implementations§

§

impl Freeze for L10N

§

impl RefUnwindSafe for L10N

§

impl Send for L10N

§

impl Sync for L10N

§

impl Unpin for L10N

§

impl UnwindSafe for L10N

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<T> Downcast for T
where T: Any,

§

fn into_any(self: Box<T>) -> Box<dyn Any>

Convert Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.
§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

Convert Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait.
§

fn as_any(&self) -> &(dyn Any + 'static)

Convert &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s.
§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Convert &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
§

impl<T> DowncastSync for T
where T: Any + Send + Sync,

§

fn into_any_arc(self: Arc<T>) -> Arc<dyn Any + Sync + Send>

Convert Arc<Trait> (where Trait: Downcast) to Arc<Any>. Arc<Any> can then be further downcast into Arc<ConcreteType> where ConcreteType implements Trait.
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> IntoEither for T

source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
§

impl<T> Pointable for T

§

const ALIGN: usize = _

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
source§

impl<T> Same for T

source§

type Output = T

Should always be Self
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

source§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more
source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
§

impl<T> ErasedDestructor for T
where T: 'static,

§

impl<T> MaybeSendSync for T

source§

impl<T> StateValue for T
where T: Any + Send + Sync,