Skip to content

Commit

Permalink
Treat values surrounded by brackets as invalid
Browse files Browse the repository at this point in the history
  • Loading branch information
Zireael-N committed Jul 19, 2020
1 parent f50165a commit 320b935
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 12 deletions.
8 changes: 7 additions & 1 deletion localize_npc_names/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,13 @@ impl Localizer {
match result {
Ok(translation) => {
let _ = tx.send(Ok(()));
Some((name, translation))
let (translation, is_valid) = match translation.as_bytes() {
[b'[', rest @ .., b']'] => {
(String::from_utf8(rest.to_vec()).unwrap(), false)
}
_ => (translation, true),
};
Some((name, (translation, is_valid)))
}
Err(e) => {
let _ = tx
Expand Down
46 changes: 35 additions & 11 deletions localize_npc_names/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ pub(crate) fn discard_existing(
fn replace<'a, 'b>(
src: &'a str,
header: &'b str,
mut values: Map<String, String>,
mut values: Map<String, (String, bool)>,
) -> Option<Cow<'a, str>> {
let mut state = State::Initial;
let mut scratch: Vec<u8> = Vec::new();
Expand Down Expand Up @@ -104,9 +104,15 @@ fn replace<'a, 'b>(
let offset = offset(src, line);

scratch.extend_from_slice(&bytes[copy_from..offset]);
for (name, value) in &values {
for (name, (translation, is_valid)) in &values {
scratch.extend_from_slice(
format!("\tL.{} = \"{}\"", name, value).as_bytes(),
format!(
"{}L.{} = \"{}\"",
if *is_valid { "\t" } else { "\t -- " },
name,
translation
)
.as_bytes(),
);
scratch.extend_from_slice(LINE_ENDING);
}
Expand All @@ -117,15 +123,17 @@ fn replace<'a, 'b>(
} else if let Some(caps) = LOCALE_ASSIGNMENT_REGEX.captures(line) {
let name = caps.get(2).unwrap().as_str();

if let Some(value) = values.remove(name) {
if let Some((translation, is_valid)) = values.remove(name) {
let is_comment = caps.get(1).is_some();
let leftover = caps.get(4).unwrap().as_str();
if is_comment || caps.get(3).unwrap().as_str() != value {
if is_valid && (is_comment || caps.get(3).unwrap().as_str() != translation)
{
let offset = offset(src, line);

scratch.extend_from_slice(&bytes[copy_from..offset]);
scratch.extend_from_slice(
format!("\tL.{} = \"{}\"{}", name, value, leftover).as_bytes(),
format!("\tL.{} = \"{}\"{}", name, translation, leftover)
.as_bytes(),
);
copy_from = offset + line.len();
}
Expand Down Expand Up @@ -166,8 +174,16 @@ fn replace<'a, 'b>(
scratch.extend_from_slice(b"if L then");
scratch.extend_from_slice(LINE_ENDING);

for (name, value) in values {
scratch.extend_from_slice(format!("\tL.{} = \"{}\"", name, value).as_bytes());
for (name, (translation, is_valid)) in values {
scratch.extend_from_slice(
format!(
"{}L.{} = \"{}\"",
if is_valid { "\t" } else { "\t -- " },
name,
translation
)
.as_bytes(),
);
scratch.extend_from_slice(LINE_ENDING);
}
scratch.extend_from_slice(b"end");
Expand All @@ -181,7 +197,7 @@ pub(crate) fn write_to_dir(
output_dir: &Path,
language_code: &str,
header: &str,
values: Map<String, String>,
values: Map<String, (String, bool)>,
) -> Result<(), (PathBuf, io::Error)> {
let to_path = output_dir.join(format!("{}.lua", language_code));
match File::open(&to_path) {
Expand Down Expand Up @@ -251,9 +267,17 @@ pub(crate) fn write_to_dir(
.write_all(LINE_ENDING)
.map_err(|e| (to_path.clone(), e))?;

for (name, translation) in values {
for (name, (translation, is_valid)) in values {
to_file
.write_all(format!("\tL.{} = \"{}\"", name, translation).as_bytes())
.write_all(
format!(
"{}L.{} = \"{}\"",
if is_valid { "\t" } else { "\t -- " },
name,
translation
)
.as_bytes(),
)
.map_err(|e| (to_path.clone(), e))?;
to_file
.write_all(LINE_ENDING)
Expand Down

0 comments on commit 320b935

Please sign in to comment.