Skip to content

Commit

Permalink
ci: Add integrated test for L10n (#510)
Browse files Browse the repository at this point in the history
  • Loading branch information
void-cat-0 authored Feb 6, 2025
1 parent 023fa20 commit cbffc51
Show file tree
Hide file tree
Showing 46 changed files with 454 additions and 277 deletions.
1 change: 1 addition & 0 deletions .github/workflows/cargo_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,4 @@ jobs:
- name: Run tests
run: |
cargo test -p phira --no-default-features -- --skip test_parse_chart
cargo test -p prpr --no-default-features
18 changes: 18 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ members = [
"prpr",
"prpr-avc",
"prpr-pbc",
"prpr-l10n",
"phira",
"phira-main",
"phira-monitor",
Expand Down
5 changes: 5 additions & 0 deletions phira/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ once_cell = "*"
openssl = { version = "*", features = ["vendored"] }
pollster = "0.3.0"
prpr = { path = "../prpr", features = ["log"], default-features = false }
prpr-l10n ={ path = "../prpr-l10n" }
rand = "0.8.5"
regex = "1.7.0"
reqwest = { version = "0.12.5", features = ["json", "stream", "gzip"] }
Expand Down Expand Up @@ -75,3 +76,7 @@ objc-foundation = "*"

[build-dependencies]
dotenv-build = "0.1"

[dev-dependencies]
fluent = "0.16.0"
fluent-syntax = "0.11.0"
3 changes: 2 additions & 1 deletion phira/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ use crate::{anti_addiction_action, get_data, get_data_mut, save_data};
use anyhow::{anyhow, bail, Context, Result};
use arc_swap::ArcSwap;
use once_cell::sync::Lazy;
use prpr::{l10n::LANG_IDENTS, scene::SimpleRecord};
use prpr::scene::SimpleRecord;
use prpr_l10n::LANG_IDENTS;
use reqwest::{header, ClientBuilder, Method, RequestBuilder, Response, StatusCode};
use serde::{Deserialize, Serialize};
use serde_json::json;
Expand Down
4 changes: 2 additions & 2 deletions phira/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
prpr::tl_file!("common" ttl crate::);
prpr_l10n::tl_file!("common" ttl crate::);

#[cfg(feature = "closed")]
mod inner;
Expand Down Expand Up @@ -28,13 +28,13 @@ use prpr::{
build_conf,
core::{init_assets, PGR_FONT},
ext::SafeTexture,
l10n::{set_prefered_locale, GLOBAL, LANGS},
log,
scene::{show_error, show_message},
time::TimeManager,
ui::{FontArc, TextPainter},
Main,
};
use prpr_l10n::{set_prefered_locale, GLOBAL, LANGS};
use scene::MainScene;
use std::sync::{mpsc, Mutex};
use tracing::{error, info};
Expand Down
2 changes: 1 addition & 1 deletion phira/src/login.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
prpr::tl_file!("login");
prpr_l10n::tl_file!("login");

use crate::{
client::{Client, LoginParams, User, UserManager},
Expand Down
2 changes: 1 addition & 1 deletion phira/src/mp.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
prpr::tl_file!("multiplayer" mtl);
prpr_l10n::tl_file!("multiplayer" mtl);

mod panel;
pub use panel::MPPanel;
2 changes: 1 addition & 1 deletion phira/src/page/coll.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
prpr::tl_file!("collection");
prpr_l10n::tl_file!("collection");

use super::{Illustration, NextPage, Page, SharedState};
use crate::{icons::Icons, load_res_tex, resource::rtl, scene::ChapterScene};
Expand Down
4 changes: 2 additions & 2 deletions phira/src/page/home.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
prpr::tl_file!("home");
prpr_l10n::tl_file!("home");

use super::{
load_font_with_cksum, set_bold_font, EventPage, LibraryPage, MessagePage, NextPage, Page, ResPackPage, SFader, SettingsPage, SharedState,
Expand All @@ -15,6 +15,7 @@ use crate::{
sync_data,
threed::ThreeD,
};
use prpr_l10n::LANG_IDENTS;
use ::rand::{random, thread_rng, Rng};
use anyhow::{bail, Context, Result};
use chrono::NaiveDate;
Expand All @@ -24,7 +25,6 @@ use prpr::{
core::BOLD_FONT,
ext::{open_url, screen_aspect, semi_black, semi_white, RectExt, SafeTexture, ScaleType},
info::ChartInfo,
l10n::LANG_IDENTS,
scene::{show_error, NextScene},
task::Task,
ui::{button_hit_large, clip_rounded_rect, ClipType, DRectButton, Dialog, FontArc, RectButton, Scroll, Ui},
Expand Down
2 changes: 1 addition & 1 deletion phira/src/page/library.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
prpr::tl_file!("library");
prpr_l10n::tl_file!("library");

use super::{CollectionPage, NextPage, Page, SharedState};
use crate::{
Expand Down
2 changes: 1 addition & 1 deletion phira/src/page/message.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
prpr::tl_file!("message");
prpr_l10n::tl_file!("message");

use std::borrow::Cow;

Expand Down
2 changes: 1 addition & 1 deletion phira/src/page/offset.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
prpr::tl_file!("cali");
prpr_l10n::tl_file!("cali");

use std::borrow::Cow;

Expand Down
2 changes: 1 addition & 1 deletion phira/src/page/respack.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
prpr::tl_file!("respack");
prpr_l10n::tl_file!("respack");

use super::{Page, SharedState};
use crate::{
Expand Down
4 changes: 2 additions & 2 deletions phira/src/page/settings.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
prpr::tl_file!("settings");
prpr_l10n::tl_file!("settings");

use super::{NextPage, OffsetPage, Page, SharedState};
use crate::{
Expand All @@ -15,11 +15,11 @@ use macroquad::prelude::*;
use prpr::{
core::BOLD_FONT,
ext::{open_url, poll_future, semi_white, LocalTask, RectExt, SafeTexture},
l10n::{LanguageIdentifier, LANG_IDENTS, LANG_NAMES},
scene::{request_input, return_input, show_error, show_message, take_input},
task::Task,
ui::{DRectButton, Scroll, Slider, Ui},
};
use prpr_l10n::{LanguageIdentifier, LANG_IDENTS, LANG_NAMES};
use reqwest::Url;
use std::{borrow::Cow, fs, io, net::ToSocketAddrs, path::PathBuf, sync::atomic::Ordering};

Expand Down
2 changes: 1 addition & 1 deletion phira/src/rate.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
prpr::tl_file!("rate");
prpr_l10n::tl_file!("rate");

use crate::page::Fader;
use macroquad::prelude::*;
Expand Down
2 changes: 1 addition & 1 deletion phira/src/resource.rs
Original file line number Diff line number Diff line change
@@ -1 +1 @@
prpr::tl_file!("resource" rtl);
prpr_l10n::tl_file!("resource" rtl);
2 changes: 1 addition & 1 deletion phira/src/scene.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
prpr::tl_file!("import" itl);
prpr_l10n::tl_file!("import" itl);

mod chart_order;
pub use chart_order::{ChartOrder, ORDERS};
Expand Down
2 changes: 1 addition & 1 deletion phira/src/scene/chapter.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
prpr::tl_file!("chapter");
prpr_l10n::tl_file!("chapter");

use crate::{
anim::Anim,
Expand Down
2 changes: 1 addition & 1 deletion phira/src/scene/chart_order.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
prpr::tl_file!("chart_order");
prpr_l10n::tl_file!("chart_order");

use crate::page::ChartItem;

Expand Down
2 changes: 1 addition & 1 deletion phira/src/scene/event.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
prpr::tl_file!("event");
prpr_l10n::tl_file!("event");

use super::{render_ldb, LdbDisplayItem, ProfileScene};
use crate::{
Expand Down
2 changes: 1 addition & 1 deletion phira/src/scene/profile.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
prpr::tl_file!("profile");
prpr_l10n::tl_file!("profile");

use super::{confirm_delete, TEX_BACKGROUND, TEX_ICON_BACK};
use crate::{
Expand Down
2 changes: 1 addition & 1 deletion phira/src/scene/song.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
prpr::tl_file!("song");
prpr_l10n::tl_file!("song");

#[cfg(feature = "video")]
use super::UnlockScene;
Expand Down
2 changes: 1 addition & 1 deletion phira/src/tags.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
prpr::tl_file!("tags");
prpr_l10n::tl_file!("tags");

use crate::{client::Permissions, page::Fader};
use macroquad::prelude::*;
Expand Down
9 changes: 9 additions & 0 deletions phira/tests/integrated_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
use prpr_l10n::tools::check_langfile;

#[test]
fn check_all() {
match check_langfile(concat!(env!("CARGO_MANIFEST_DIR"), "/locales/")) {
Ok(_) => {}
Err(e) => panic!("Error: {}", e),
}
}
14 changes: 14 additions & 0 deletions prpr-l10n/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "prpr-l10n"
version = "0.1.0"
edition = "2021"

[dependencies]
fluent = "0.16.0"
fluent-syntax = "0.11.0"
lru = "0.9.0"
once_cell = "1.16.0"
sys-locale = "0.3.1"
tracing = "0.1.37"
unic-langid = { version = "0.9.1", features = ["macros"] }
walkdir = "2.3.3"
42 changes: 42 additions & 0 deletions prpr-l10n/src/global.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
use std::{collections::HashMap, sync::Mutex};

use tracing::warn;
use unic_langid::LanguageIdentifier;

use crate::{fallback_langid, FALLBACK_LANG, LANG_IDENTS};

pub struct L10nGlobal {
pub lang_map: HashMap<LanguageIdentifier, usize>,
pub order: Mutex<Vec<usize>>,
}

impl Default for L10nGlobal {
fn default() -> Self {
Self::new()
}
}

impl L10nGlobal {
pub fn new() -> Self {
let mut lang_map = HashMap::new();
let mut order = Vec::new();
let locale_lang = sys_locale::get_locale().unwrap_or_else(|| String::from(FALLBACK_LANG));
let locale_lang: LanguageIdentifier = locale_lang.parse().unwrap_or_else(|_| {
warn!("Invalid locale detected, defaulting to `{}`", FALLBACK_LANG);
// Debug log: send lang tag to log
warn!("Locale detected: {:?}", locale_lang);
fallback_langid!()
});
for (id, lang) in LANG_IDENTS.iter().enumerate() {
lang_map.insert(lang.clone(), id);
if *lang == locale_lang {
order.push(id);
}
}
order.push(*lang_map.get(&fallback_langid!()).unwrap());
Self {
lang_map,
order: order.into(),
}
}
}
79 changes: 79 additions & 0 deletions prpr-l10n/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
//! Localization utilities.
pub use fluent::{fluent_args, FluentBundle, FluentResource};
pub use once_cell::sync::Lazy;
pub use unic_langid::LanguageIdentifier;

use std::sync::atomic::{AtomicU8, Ordering};

mod global;
pub use global::*;

mod local;
pub use local::*;

mod macros;

pub mod tools;

langs! {
"en-US": "English",
"fr-FR": "Français",
"id-ID": "Bahasa Indonesia",
"ja-JP": "日本語",
"ko-KR": "한국어",
"pl-PL": "Polski",
"pt-BR": "Português",
"ru-RU": "Русский",
"th-TH": "แบบไทย",
"vi-VN": "Tiếng Việt",
"zh-CN": "简体中文",
"zh-TW": "繁體中文"
}

#[macro_export]
macro_rules! fallback_langid {
() => {
unic_langid::langid!("en-US")
};
}

pub const FALLBACK_LANG: &str = "en-US";

pub static GLOBAL: Lazy<L10nGlobal> = Lazy::new(L10nGlobal::new);

pub fn set_prefered_locale(locale: Option<LanguageIdentifier>) {
let mut ids = Vec::new();
let map = &GLOBAL.lang_map;
if let Some(lang) = locale.and_then(|it| map.get(&it)) {
ids.push(*lang);
}
if let Some(lang) = sys_locale::get_locale()
.and_then(|it| it.parse::<LanguageIdentifier>().ok())
.and_then(|it| map.get(&it))
{
ids.push(*lang);
}
ids.push(*map.get(&fallback_langid!()).unwrap());
*GLOBAL.order.lock().unwrap() = ids;
GENERATION.fetch_add(1, Ordering::Relaxed);
}

pub fn locale_order() -> Vec<usize> {
GLOBAL.order.lock().unwrap().clone()
}

pub struct L10nBundles {
inner: Vec<FluentBundle<FluentResource>>,
}

impl From<Vec<FluentBundle<FluentResource>>> for L10nBundles {
fn from(inner: Vec<FluentBundle<FluentResource>>) -> Self {
Self { inner }
}
}

unsafe impl Send for L10nBundles {}
unsafe impl Sync for L10nBundles {}

pub static GENERATION: AtomicU8 = AtomicU8::new(0);
Loading

0 comments on commit cbffc51

Please sign in to comment.