Skip to content

Commit

Permalink
feat: support user locale
Browse files Browse the repository at this point in the history
  • Loading branch information
imLinguin committed Nov 19, 2024
1 parent e60b2e6 commit f1f9f1e
Show file tree
Hide file tree
Showing 8 changed files with 683 additions and 329 deletions.
986 changes: 665 additions & 321 deletions Cargo.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ async_zip = { version = "0.0.17", features = ["tokio", "deflate"] }
base64 = "0.22.0"
serde_ini = "0.2.0"
toml = "0.8.19"
sys-locale = "0.3.2"

[build-dependencies]
protobuf-codegen = "3.4.0"
Expand Down
2 changes: 1 addition & 1 deletion src/api/gog/achievements.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ pub async fn fetch_achievements(
let response = reqwest_client
.get(url)
.bearer_auth(token.access_token)
.header("X-Gog-Lc", "en-US") // TODO: Handle languages
.header("X-Gog-Lc", crate::LOCALE.as_str())
.send()
.await
.map_err(|err| MessageHandlingError::new(MessageHandlingErrorKind::Network(err)))?;
Expand Down
4 changes: 2 additions & 2 deletions src/api/gog/leaderboards.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ where
let response = reqwest_client
.get(new_url)
.bearer_auth(token.access_token)
.header("X-Gog-Lc", "en-US") // TODO: Handle languages
.header("X-Gog-Lc", crate::LOCALE.as_str())
.send()
.await?;

Expand Down Expand Up @@ -113,7 +113,7 @@ where
let response = reqwest_client
.get(new_url)
.bearer_auth(token.access_token)
.header("X-Gog-Lc", "en-US") // TODO: Handle languages
.header("X-Gog-Lc", crate::LOCALE.as_str())
.send()
.await?;

Expand Down
2 changes: 1 addition & 1 deletion src/api/handlers/communication_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,7 @@ async fn get_user_achievements(
);
let mut content = GetUserAchievementsResponse::new();
content.set_achievements_mode(achievements_mode);
content.set_language("en-US".to_string());
content.set_language(crate::LOCALE.clone());

for achievement in achievements {
let mut proto_achievement = UserAchievement::new();
Expand Down
5 changes: 3 additions & 2 deletions src/api/handlers/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,10 @@ impl HandlerContext {
.await?;

// This may already exist, we don't care
let _ = sqlx::query("INSERT INTO database_info VALUES ('language', 'en-US')")
let _ = sqlx::query("INSERT INTO database_info VALUES ('language', $1) ON CONFLICT(key) DO UPDATE SET value = excluded.value")
.bind(crate::LOCALE.as_str())
.execute(&mut *connection)
.await; // TODO: Handle languages
.await;

self.db_connected = true;
Ok(())
Expand Down
6 changes: 4 additions & 2 deletions src/db/gameplay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,9 @@ pub async fn set_statistics(database: SqlitePool, stats: &Vec<Stat>) -> Result<(
min_value,
max_change,
} => {
log::debug!("Inserting int");
sqlx::query(
"INSERT INTO int_statistic VALUES ($1, $2, $3, $4, $5, $6) ON CONFLICT(id)
"INSERT INTO int_statistic VALUES ($1, $2, $3, $4, $5, $6) ON CONFLICT
DO UPDATE SET value=excluded.value, default_value=excluded.default_value,
min_value=excluded.min_value, max_change=excluded.max_change
WHERE int_statistic.id NOT IN (SELECT id FROM statistic WHERE changed=1)",
Expand Down Expand Up @@ -195,8 +196,9 @@ pub async fn set_statistics(database: SqlitePool, stats: &Vec<Stat>) -> Result<(
max_value,
max_change,
} => {
log::debug!("Inserting float");
sqlx::query(
"INSERT INTO float_statistic VALUES ($1, $2, $3, $4, $5, $6, $7) ON CONFLICT(id)
"INSERT INTO float_statistic VALUES ($1, $2, $3, $4, $5, $6, $7) ON CONFLICT
DO UPDATE SET value=excluded.value, default_value=excluded.default_value,
min_value=excluded.min_value, max_change=excluded.max_change, window=excluded.window
WHERE float_statistic.id NOT IN (SELECT id FROM statistic WHERE changed=1)",
Expand Down
6 changes: 6 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ struct Args {
subcommand: Option<SubCommand>,
}

lazy_static! {
static ref LOCALE: String = sys_locale::get_locale().unwrap_or_else(|| String::from("en-US"));
}

#[tokio::main]
async fn main() {
let args = Args::parse();
Expand All @@ -95,6 +99,8 @@ async fn main() {
.filter_module("h2::codec", log::LevelFilter::Off)
.init();

log::info!("Prefered language: {}", LOCALE.as_str());

let (access_token, refresh_token, galaxy_user_id) =
import_parsers::handle_credentials_import(&args);

Expand Down

0 comments on commit f1f9f1e

Please sign in to comment.