Skip to content

Commit

Permalink
Localization bugfixes
Browse files Browse the repository at this point in the history
  • Loading branch information
TCA166 committed May 17, 2024
1 parent 3e15245 commit 7c641ff
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 10 deletions.
26 changes: 24 additions & 2 deletions src/localizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ use crate::types::Wrapper;
/// A function that demangles a generic name.
/// It will replace underscores with spaces and capitalize the first letter.
fn demangle_generic(input:&str) -> String{
let mut s = input.replace("_", " ");
let mut s = input.trim_start_matches("dynn_").trim_start_matches("nick_").trim_end_matches("_perk").trim_start_matches("death_")
.trim_start_matches("tenet_").trim_start_matches("doctrine_")
.trim_start_matches("ethos_").trim_start_matches("heritage_").trim_start_matches("language_").trim_start_matches("martial_custom_").trim_start_matches("tradition_")
.trim_start_matches("e_").trim_start_matches("k_").trim_start_matches("d_").trim_start_matches("c_").trim_start_matches("b_")
.replace("_", " ");
let bytes = unsafe { s.as_bytes_mut() };
bytes[0] = bytes[0].to_ascii_uppercase();
s
Expand Down Expand Up @@ -72,8 +76,15 @@ impl Localizer{
},
'\n' => {
if past && !quotes && !value.is_empty(){
//Removing trait_? good idea because the localisation isnt consistent enough with trait names
//Removing _name though... controversial. Possibly a bad idea
//MAYBE only do this in certain files
key = key.trim_start_matches("trait_").trim_end_matches("_name").to_string();
data.insert(mem::take(&mut key), GameString::wrap(mem::take(&mut value)));
}
else{
key.clear()
}
past = false;
quotes = false;
}
Expand All @@ -95,10 +106,16 @@ impl Localizer{
}
}
}
/*
From what I can gather there are two types of special localisation invocations:
- $key$ - use that key instead of the key that was used to look up the string
- [function(arg).function(arg)...] handling this one is going to be a nightmare
*/
//TODO resolve the localization functions
hmap = Some(data);
}
}
//println!("{:?}", hmap);
Localizer{
data: hmap
}
Expand All @@ -111,7 +128,12 @@ impl Localizer{
}
let data = self.data.as_ref().unwrap();
if data.contains_key(key){
return data.get(key).unwrap().clone();
let d = data.get(key).unwrap().clone();
if d.starts_with("$") && d.ends_with("$"){
return self.localize(&d[1..d.len()-1]);
} else {
return d;
}
}
//println!("Key not found: {}", key);
GameString::wrap(demangle_generic(key))
Expand Down
5 changes: 4 additions & 1 deletion src/structures/character.rs
Original file line number Diff line number Diff line change
Expand Up @@ -548,9 +548,10 @@ impl Renderable for Character {

impl Cullable for Character {
fn set_depth(&mut self, depth:usize, localization:&Localizer) {
if depth <= self.depth {
if depth <= self.depth && depth != 0{
return;
}
//TODO add separate toggle for name localization
if !self.localized {
if self.name.is_none() {
self.name = Some(GameString::wrap("Unknown".to_string()));
Expand All @@ -572,9 +573,11 @@ impl Cullable for Character {
for t in self.traits.iter_mut(){
*t = localization.localize(t.as_str());
}
/*
for t in self.recessive.iter_mut(){
*t = localization.localize(t.as_str());
}
*/
for t in self.languages.iter_mut(){
*t = localization.localize(t.as_str());
}
Expand Down
2 changes: 1 addition & 1 deletion src/structures/culture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ impl Cullable for Culture {
}

fn set_depth(&mut self, depth:usize, localization:&Localizer) {
if depth <= self.depth {
if depth <= self.depth && depth != 0 {
return;
}
if !self.localized {
Expand Down
2 changes: 1 addition & 1 deletion src/structures/dynasty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ impl Renderable for Dynasty {

impl Cullable for Dynasty {
fn set_depth(&mut self, depth:usize, localization:&Localizer) {
if depth <= self.depth{
if depth <= self.depth && depth != 0{
return;
}
//TODO deal with weird name chicanery
Expand Down
2 changes: 1 addition & 1 deletion src/structures/faith.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ impl Cullable for Faith {
}

fn set_depth(&mut self, depth: usize, localization:&Localizer) {
if depth <= self.depth{
if depth <= self.depth && depth != 0{
return;
}
if !self.localized{
Expand Down
26 changes: 24 additions & 2 deletions src/structures/title.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,12 +214,34 @@ impl Serialize for Title {
where
S: serde::Serializer,
{
let mut state = serializer.serialize_struct("Title", 5)?;
let mut state = serializer.serialize_struct("Title", 6)?;
state.serialize_field("name", &self.name)?;
if self.de_jure.is_some(){
let de_jure = DerivedRef::from_derived(self.de_jure.as_ref().unwrap().clone());
state.serialize_field("de_jure", &de_jure)?;
}
//match the first character of key
let first_char = self.key.as_ref().unwrap().as_str().chars().next().unwrap();
match first_char{
'e' => {
state.serialize_field("tier", "Empire of")?;
},
'k' => {
state.serialize_field("tier", "Kingdom of")?;
},
'd' => {
state.serialize_field("tier", "Duchy of")?;
},
'c' => {
state.serialize_field("tier", "County of")?;
},
'b' => {
state.serialize_field("tier", "Barony of")?;
},
_ => {
state.serialize_field("tier", "")?;
}
}
if self.de_facto.is_some(){
let de_facto = DerivedRef::from_derived(self.de_facto.as_ref().unwrap().clone());
state.serialize_field("de_facto", &de_facto)?;
Expand Down Expand Up @@ -277,7 +299,7 @@ impl Renderable for Title {

impl Cullable for Title {
fn set_depth(&mut self, depth:usize, localization:&Localizer) {
if depth <= self.depth {
if depth <= self.depth && depth != 0{
return;
}
if !self.localized { //localization
Expand Down
4 changes: 2 additions & 2 deletions templates/titleTemplate.html
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<html>
<head>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-BmbxuPwQa2lc/FVzBcNJ7UAyJxM6wuqIj61tLrc4wSX0szH/Ev+nYRRuWlolflfl" crossorigin="anonymous">
<title>{{title.name}}</title>
<title>{{title.tier}} {{title.name}}</title>
</head>
<body>
<header class="d-flex flex-column flex-md-row align-items-center p-3 px-md-4 mb-3 bg-body border-bottom shadow-sm">
<p class="h1 my-0 me-md-auto fw-normal">{{title.name}}</p>
<p class="h1 my-0 me-md-auto fw-normal">{{title.tier}} {{title.name}}</p>
<a class="btn btn-outline-primary" href="../index.html">Home</a>
</header>
<main style="margin:15px">
Expand Down

0 comments on commit 7c641ff

Please sign in to comment.