use crate::prelude::*;
use crate::project::DEFAULT_ASSET_DIR;
use bevy::prelude::*;
use std::iter;
use std::path::{Path, PathBuf};
pub(crate) fn localization_config_plugin(_app: &mut App) {}
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct Localizations {
pub base_localization: Localization,
pub translations: Vec<Localization>,
}
impl Localizations {
pub fn supports_language(&self, language: &Language) -> bool {
self.supported_languages()
.any(|supported_language| supported_language == language)
}
pub(crate) fn translation(&self, language: &Language) -> Option<&Localization> {
self.translations
.iter()
.find(|localization| localization.language == *language)
}
pub(crate) fn supported_localization(&self, language: &Language) -> Option<&Localization> {
iter::once(&self.base_localization)
.chain(self.translations.iter())
.find(|localization| localization.language == *language)
}
pub fn supported_languages(&self) -> impl Iterator<Item = &Language> {
iter::once(&self.base_localization.language).chain(
self.translations
.iter()
.map(|localization| &localization.language),
)
}
pub(crate) fn strings_file_path(&self, language: impl Into<Language>) -> Option<&Path> {
let language = language.into();
self.translations
.iter()
.find_map(|t| (t.language == language).then_some(t.strings_file.as_path()))
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[non_exhaustive]
pub struct Localization {
pub language: Language,
pub strings_file: PathBuf,
pub assets_sub_folder: PathBuf,
}
impl<T> From<T> for Localization
where
Language: From<T>,
{
fn from(language: T) -> Self {
Self::with_language(language)
}
}
impl Localization {
pub fn with_language(language: impl Into<Language>) -> Self {
let language = language.into();
let strings_file = PathBuf::from(format!("{DEFAULT_ASSET_DIR}/{language}.strings.csv"));
let assets_sub_folder = PathBuf::from(format!("{DEFAULT_ASSET_DIR}/{language}/"));
Self {
language,
strings_file,
assets_sub_folder,
}
}
pub fn with_strings_file(mut self, strings_file: impl Into<PathBuf>) -> Self {
self.strings_file = strings_file.into();
self
}
pub fn with_assets_sub_folder(mut self, assets_sub_folder: impl Into<PathBuf>) -> Self {
self.assets_sub_folder = assets_sub_folder.into();
self
}
}