From f469776a1d57ae3d46aff758ee92c1d4b6ce22c6 Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Thu, 23 Mar 2023 19:58:59 +0100 Subject: [PATCH] Allow specifying locale via the `DATA_DRAGON_LOCALE` envvar --- src/data/corebundle/mod.rs | 12 ++++++------ src/data/setbundle/mod.rs | 8 ++++---- src/discord/main.rs | 13 +++++++++---- src/telegram/main.rs | 14 ++++++++++---- 4 files changed, 29 insertions(+), 18 deletions(-) diff --git a/src/data/corebundle/mod.rs b/src/data/corebundle/mod.rs index 8957087..4651a00 100644 --- a/src/data/corebundle/mod.rs +++ b/src/data/corebundle/mod.rs @@ -71,7 +71,7 @@ pub fn create_globalindexes_from_wd() -> globals::LocalizedGlobalsIndexes { .expect("a valid core bundle to exist"); let core = CoreBundle::load(&path) - .expect("to be able to load `core-en_us` bundle"); + .expect("to be able to load CoreBundle bundle"); globals::LocalizedGlobalsIndexes::from(core.globals) } @@ -80,15 +80,15 @@ pub fn create_globalindexes_from_wd() -> globals::LocalizedGlobalsIndexes { /// Create [`globals::LocalizedGlobalsIndexes`] from the latest english data in Data Dragon. /// /// This function tries to load data from `https://dd.b.pvp.net/latest`. -pub async fn create_globalindexes_from_dd_latest_en_us() -> globals::LocalizedGlobalsIndexes { +pub async fn create_globalindexes_from_dd_latest(locale: &str) -> globals::LocalizedGlobalsIndexes { let client = reqwest::Client::new(); - log::debug!("Fetching latest CoreBundle from Data Dragon..."); + log::debug!("Fetching {} CoreBundle from Data Dragon...", locale); - let core = CoreBundle::fetch(&client, "https://dd.b.pvp.net/latest", "en_us").await - .expect("to be able to fetch `core-en_us` bundle"); + let core = CoreBundle::fetch(&client, "https://dd.b.pvp.net/latest", locale).await + .expect("to be able to fetch CoreBundle"); - log::debug!("Fetched latest CoreBundle: it defines {} regions, {} keywords, {} rarities, {} sets, {} spell speeds, and {} vocab terms!", &core.globals.regions.len(), &core.globals.keywords.len(), &core.globals.rarities.len(), &core.globals.sets.len(), &core.globals.spell_speeds.len(), &core.globals.vocab_terms.len()); + log::debug!("Fetched {} CoreBundle: it defines {} regions, {} keywords, {} rarities, {} sets, {} spell speeds, and {} vocab terms!", locale, &core.globals.regions.len(), &core.globals.keywords.len(), &core.globals.rarities.len(), &core.globals.sets.len(), &core.globals.spell_speeds.len(), &core.globals.vocab_terms.len()); globals::LocalizedGlobalsIndexes::from(core.globals) } diff --git a/src/data/setbundle/mod.rs b/src/data/setbundle/mod.rs index ab22081..57e1516 100644 --- a/src/data/setbundle/mod.rs +++ b/src/data/setbundle/mod.rs @@ -163,17 +163,17 @@ pub const DD_KNOWN_SET_CODES: [&str; 7] = [ /// Create a [`card::CardIndex`] from the latest known english data in Data Dragon. /// /// This function tries to load data from `https://dd.b.pvp.net/latest`. -pub async fn create_cardindex_from_dd_latest_en_us() -> card::CardIndex { +pub async fn create_cardindex_from_dd_latest(locale: &str) -> card::CardIndex { let client = reqwest::Client::new(); let mut index = card::CardIndex::new(); for set_code in DD_KNOWN_SET_CODES { - log::debug!("Fetching SetBundle with code {} from Data Dragon...", &set_code); + log::debug!("Fetching {} SetBundle with code {} from Data Dragon...", locale, &set_code); - let set = SetBundle::fetch(&client, "https://dd.b.pvp.net/latest", "en_us", set_code).await + let set = SetBundle::fetch(&client, "https://dd.b.pvp.net/latest", locale, set_code).await .expect("to be able to fetch set bundle"); - log::debug!("Fetched SetBundle with code {}: it defines {} cards!", &set_code, set.cards.len()); + log::debug!("Fetched {} SetBundle with code {}: it defines {} cards!", locale, &set_code, set.cards.len()); for card in set.cards { index.insert(card.code.clone(), card); diff --git a/src/discord/main.rs b/src/discord/main.rs index 1ef5b05..2437550 100644 --- a/src/discord/main.rs +++ b/src/discord/main.rs @@ -3,8 +3,8 @@ use std::env; use log::*; use serenity::prelude::*; -use crate::data::corebundle::create_globalindexes_from_dd_latest_en_us; -use crate::data::setbundle::create_cardindex_from_dd_latest_en_us; +use crate::data::corebundle::create_globalindexes_from_dd_latest; +use crate::data::setbundle::create_cardindex_from_dd_latest; use crate::discord::handler::EventHandler; use crate::search::cardsearch::CardSearchEngine; @@ -13,12 +13,17 @@ pub async fn main() { pretty_env_logger::init(); debug!("Logger initialized successfully!"); + debug!("Detecting locale to use..."); + let locale = env::var("DATA_DRAGON_LOCALE") + .expect("DATA_DRAGON_LOCALE to be set"); + debug!("Using {} locale!", &locale); + debug!("Creating LocalizedGlobalIndexes..."); - let globals = create_globalindexes_from_dd_latest_en_us().await; + let globals = create_globalindexes_from_dd_latest(&locale).await; debug!("Created LocalizedGlobalIndexes!"); debug!("Creating CardIndex..."); - let cards = create_cardindex_from_dd_latest_en_us().await; + let cards = create_cardindex_from_dd_latest(&locale).await; debug!("Created CardIndex!"); debug!("Creating CardSearchEngine..."); diff --git a/src/telegram/main.rs b/src/telegram/main.rs index 0aebaec..95df6e6 100644 --- a/src/telegram/main.rs +++ b/src/telegram/main.rs @@ -1,7 +1,8 @@ //! Module defining the [`main`] function for `patched_porobot_telegram`. -use crate::data::corebundle::create_globalindexes_from_dd_latest_en_us; -use crate::data::setbundle::create_cardindex_from_dd_latest_en_us; +use std::env; +use crate::data::corebundle::create_globalindexes_from_dd_latest; +use crate::data::setbundle::create_cardindex_from_dd_latest; use crate::search::cardsearch::CardSearchEngine; use crate::telegram::handler::{inline_query_handler, message_handler}; use log::*; @@ -13,12 +14,17 @@ pub async fn main() { pretty_env_logger::init(); debug!("Logger initialized successfully!"); + debug!("Detecting locale to use..."); + let locale = env::var("DATA_DRAGON_LOCALE") + .expect("DATA_DRAGON_LOCALE to be set"); + debug!("Using {} locale!", &locale); + debug!("Creating LocalizedGlobalIndexes..."); - let globals = create_globalindexes_from_dd_latest_en_us().await; + let globals = create_globalindexes_from_dd_latest(&locale).await; debug!("Created LocalizedGlobalIndexes!"); debug!("Creating CardIndex..."); - let cards = create_cardindex_from_dd_latest_en_us().await; + let cards = create_cardindex_from_dd_latest(&locale).await; debug!("Created CardIndex!"); debug!("Creating CardSearchEngine...");