First start in the system language; sample databases named in it
The first start (no settings file yet) picks the first of the system's preferred languages that ChessPenguin has, otherwise English (sys-locale, MIT OR Apache-2.0). A language chosen in the settings is kept. The sample databases get names in the interface language. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
673f447491
commit
9f661c9e3c
6 changed files with 91 additions and 12 deletions
10
Cargo.lock
generated
10
Cargo.lock
generated
|
|
@ -700,6 +700,7 @@ dependencies = [
|
|||
"serde_json",
|
||||
"sha2",
|
||||
"shakmaty",
|
||||
"sys-locale",
|
||||
"tar",
|
||||
"uci",
|
||||
"ureq",
|
||||
|
|
@ -3834,6 +3835,15 @@ dependencies = [
|
|||
"unicode-ident",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "sys-locale"
|
||||
version = "0.3.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8eab9a99a024a169fe8a903cf9d4a3b3601109bcc13bd9e3c6fff259138626c4"
|
||||
dependencies = [
|
||||
"libc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tar"
|
||||
version = "0.4.46"
|
||||
|
|
|
|||
|
|
@ -31,5 +31,8 @@ tar = "0.4"
|
|||
sha2 = "0.11"
|
||||
zip = { version = "9.0.5", default-features = false, features = ["deflate-flate2-zlib-rs"] }
|
||||
|
||||
# The system language for the first start.
|
||||
sys-locale = "0.3"
|
||||
|
||||
[build-dependencies]
|
||||
winresource = "0.1"
|
||||
|
|
|
|||
|
|
@ -95,6 +95,7 @@ impl DebugStartup {
|
|||
impl App {
|
||||
pub fn new(ctx: &egui::Context) -> Self {
|
||||
let mut settings = Settings::load();
|
||||
// Before the samples, which are named in this language.
|
||||
crate::i18n::set_lang(settings.language);
|
||||
crate::engine_download::clean_leftovers();
|
||||
let engines_found = crate::engines::first_run_setup(&mut settings);
|
||||
|
|
|
|||
|
|
@ -1,16 +1,19 @@
|
|||
//! Sample databases for the first start, so the library is not empty: classic games
|
||||
//! with short introductions, and Alekhine's games (moves only). They are written once
|
||||
//! to `<local data dir>/examples/` and put on the library shelf; a sample removed from
|
||||
//! the shelf does not come back.
|
||||
//! with short introductions, and Alekhine's games (moves only). They are written once,
|
||||
//! named in the interface language, to `<local data dir>/examples/` and put on the
|
||||
//! library shelf; a sample removed from the shelf does not come back.
|
||||
|
||||
use std::path::PathBuf;
|
||||
|
||||
use crate::i18n::tr;
|
||||
use crate::settings::Settings;
|
||||
|
||||
const FILES: [(&str, &str); 2] = [
|
||||
("Classic games.pgn", include_str!("../../../examples/classics.pgn")),
|
||||
("Alekhine.pgn", include_str!("../../../examples/alekhine.pgn")),
|
||||
];
|
||||
fn files() -> [(&'static str, &'static str); 2] {
|
||||
[
|
||||
(tr("Classic games"), include_str!("../../../examples/classics.pgn")),
|
||||
(tr("Alekhine"), include_str!("../../../examples/alekhine.pgn")),
|
||||
]
|
||||
}
|
||||
|
||||
fn dir() -> Option<PathBuf> {
|
||||
directories::ProjectDirs::from("io.github", "vrubelroman", "ChessPenguin")
|
||||
|
|
@ -31,8 +34,8 @@ pub fn first_run_setup(settings: &mut Settings) -> bool {
|
|||
eprintln!("examples: cannot create {}: {e}", dir.display());
|
||||
return true;
|
||||
}
|
||||
for (name, text) in FILES {
|
||||
let path = dir.join(name);
|
||||
for (name, text) in files() {
|
||||
let path = dir.join(format!("{name}.pgn"));
|
||||
if !path.exists()
|
||||
&& let Err(e) = std::fs::write(&path, text)
|
||||
{
|
||||
|
|
@ -52,10 +55,10 @@ mod tests {
|
|||
|
||||
#[test]
|
||||
fn samples_are_valid_pgn() {
|
||||
for (name, text) in FILES {
|
||||
for (name, text) in files() {
|
||||
let dir = std::env::temp_dir().join(format!("cp-examples-{}", std::process::id()));
|
||||
std::fs::create_dir_all(&dir).unwrap();
|
||||
let path = dir.join(name);
|
||||
let path = dir.join(format!("{name}.pgn"));
|
||||
std::fs::write(&path, text).unwrap();
|
||||
let db = chess_core::PgnDatabase::open(
|
||||
&path,
|
||||
|
|
|
|||
|
|
@ -36,6 +36,32 @@ impl Lang {
|
|||
fn column(self) -> usize {
|
||||
self as usize
|
||||
}
|
||||
|
||||
/// The first supported language among the system's preferred ones ("uk-UA, ru-RU"
|
||||
/// gives Russian), or English.
|
||||
pub fn from_locales<S: AsRef<str>>(locales: impl IntoIterator<Item = S>) -> Lang {
|
||||
locales
|
||||
.into_iter()
|
||||
.find_map(|l| {
|
||||
// "ru-RU", "ru_RU.UTF-8", "de-CH", "es-419", "fr".
|
||||
let code = l.as_ref().split(['-', '_', '.', '@']).next()?.to_ascii_lowercase();
|
||||
match code.as_str() {
|
||||
"en" => Some(Lang::En),
|
||||
"ru" => Some(Lang::Ru),
|
||||
"fr" => Some(Lang::Fr),
|
||||
"de" => Some(Lang::De),
|
||||
"es" => Some(Lang::Es),
|
||||
"it" => Some(Lang::It),
|
||||
_ => None,
|
||||
}
|
||||
})
|
||||
.unwrap_or_default()
|
||||
}
|
||||
|
||||
/// The language of this system (see [`Lang::from_locales`]).
|
||||
pub fn system() -> Lang {
|
||||
Lang::from_locales(sys_locale::get_locales())
|
||||
}
|
||||
}
|
||||
|
||||
static CURRENT: AtomicU8 = AtomicU8::new(0);
|
||||
|
|
@ -2645,12 +2671,37 @@ const ROWS: &[[&str; 6]] = &[
|
|||
"Boceto",
|
||||
"Schizzo",
|
||||
],
|
||||
// Names of the sample databases (examples.rs).
|
||||
[
|
||||
"Classic games",
|
||||
"Классические партии",
|
||||
"Parties classiques",
|
||||
"Klassische Partien",
|
||||
"Partidas clásicas",
|
||||
"Partite classiche",
|
||||
],
|
||||
["Alekhine", "Алехин", "Alekhine", "Aljechin", "Alekhine", "Aljechin"],
|
||||
];
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn system_language() {
|
||||
assert_eq!(Lang::from_locales(["ru-RU"]), Lang::Ru);
|
||||
assert_eq!(Lang::from_locales(["ru_RU.UTF-8"]), Lang::Ru);
|
||||
assert_eq!(Lang::from_locales(["uk-UA", "ru-RU", "en-US"]), Lang::Ru);
|
||||
assert_eq!(Lang::from_locales(["de-CH"]), Lang::De);
|
||||
assert_eq!(Lang::from_locales(["es-419"]), Lang::Es);
|
||||
assert_eq!(Lang::from_locales(["fr"]), Lang::Fr);
|
||||
assert_eq!(Lang::from_locales(["it_IT@euro"]), Lang::It);
|
||||
assert_eq!(Lang::from_locales(["en-GB", "ru-RU"]), Lang::En);
|
||||
assert_eq!(Lang::from_locales(["ja-JP", "pt-BR"]), Lang::En);
|
||||
assert_eq!(Lang::from_locales(["C"]), Lang::En);
|
||||
assert_eq!(Lang::from_locales(Vec::<String>::new()), Lang::En);
|
||||
}
|
||||
|
||||
/// Every `tr("…")` / `trf("…"` key used in the sources must have a translation row.
|
||||
#[test]
|
||||
fn all_used_keys_are_translated() {
|
||||
|
|
@ -2666,6 +2717,7 @@ mod tests {
|
|||
include_str!("notation.rs"),
|
||||
include_str!("review.rs"),
|
||||
include_str!("engines.rs"),
|
||||
include_str!("examples.rs"),
|
||||
include_str!("settings.rs"),
|
||||
include_str!("settings_ui.rs"),
|
||||
include_str!("widgets.rs"),
|
||||
|
|
|
|||
|
|
@ -159,7 +159,7 @@ impl Settings {
|
|||
|
||||
pub fn load() -> Self {
|
||||
let Some(path) = Self::file() else {
|
||||
return Self::default();
|
||||
return Self::first_start();
|
||||
};
|
||||
migrate_legacy_config(&path);
|
||||
match std::fs::read(&path) {
|
||||
|
|
@ -170,10 +170,20 @@ impl Settings {
|
|||
);
|
||||
Self::default()
|
||||
}),
|
||||
Err(e) if e.kind() == std::io::ErrorKind::NotFound => Self::first_start(),
|
||||
Err(_) => Self::default(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Settings of the very first start: the defaults in the system's language (if it
|
||||
/// is one of ours, otherwise English).
|
||||
fn first_start() -> Self {
|
||||
Self {
|
||||
language: Lang::system(),
|
||||
..Self::default()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn save(&self) -> std::io::Result<()> {
|
||||
let Some(path) = Self::file() else {
|
||||
return Ok(());
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue