Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 15 additions & 9 deletions crates/edit/build/i18n.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,8 @@ pub fn generate(definitions: &str) -> String {
}
for c in unsafe { lang.as_bytes_mut() } {
*c = match *c {
b'A'..=b'Z' | b'a'..=b'z' => c.to_ascii_lowercase(),
b'-' => b'_',
b'_' => b'_',
_ => panic!("i18n: language tag \"{lang}\" must be [a-zA-Z_-]"),
b'A'..=b'Z' | b'a'..=b'z' | b'-' => c.to_ascii_lowercase(),
_ => panic!("i18n: language tag \"{lang}\" must be [a-zA-Z-]"),
}
}
}
Expand Down Expand Up @@ -111,15 +109,15 @@ pub fn generate(definitions: &str) -> String {
// Sort languages by:
// - "en" first, because it'll map to `LangId::en == 0`, which is the default.
// - then alphabetically
// - but tags with subtags (e.g. "zh_hans") before those without (e.g. "zh").
// - but tags with subtags (e.g. "zh-hans") before those without (e.g. "zh").
{
fn sort(a: &String, b: &String) -> std::cmp::Ordering {
match (a == "en", b == "en") {
(true, false) => std::cmp::Ordering::Less,
(false, true) => std::cmp::Ordering::Greater,
_ => {
let (a0, a1) = a.split_once('_').unwrap_or((a, "xxxxxx"));
let (b0, b1) = b.split_once('_').unwrap_or((b, "xxxxxx"));
let (a0, a1) = a.split_once('-').unwrap_or((a, "xxxxxx"));
let (b0, b1) = b.split_once('-').unwrap_or((b, "xxxxxx"));
match a0.cmp(b0) {
std::cmp::Ordering::Equal => a1.cmp(b1),
ord => ord,
Expand Down Expand Up @@ -160,7 +158,7 @@ pub enum LangId {{
);

for lang in &languages {
_ = writeln!(out, " {lang},");
_ = writeln!(out, " {},", HyphenToUnderscore(lang));
}

_ = write!(
Expand All @@ -173,7 +171,7 @@ const LANGUAGES: &[(&str, LangId)] = &[
);

for (alias, lang) in &languages_with_aliases {
_ = writeln!(out, " ({alias:?}, LangId::{lang}),");
_ = writeln!(out, " ({alias:?}, LangId::{}),", HyphenToUnderscore(lang));
}

_ = write!(
Expand Down Expand Up @@ -202,3 +200,11 @@ const TRANSLATIONS: [[&str; {}]; {}] = [

out
}

struct HyphenToUnderscore<'a>(&'a str);

impl<'a> std::fmt::Display for HyphenToUnderscore<'a> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.0.chars().map(|c| if c == '-' { '_' } else { c }).try_for_each(|c| f.write_char(c))
}
}
24 changes: 10 additions & 14 deletions crates/edit/src/sys/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -511,26 +511,22 @@ where
}
}

pub fn preferred_languages(arena: &Arena) -> BVec<'_, BString<'_>> {
pub fn preferred_languages(arena: &Arena) -> BVec<'_, &'_ str> {
let mut locales = BVec::empty();

for key in ["LANGUAGE", "LC_ALL", "LANG"] {
if let Ok(val) = std::env::var(key)
&& !val.is_empty()
{
locales.extend_sloppy(
arena,
val.split(':').filter(|s| !s.is_empty()).map(|s| {
// Replace all underscores with dashes,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wait we already had "replace underscores with dashes"? how did this get broken

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Before we had the localization compiler, my hand-written localizations used dashes and so the sys code worked perfectly. This PR fixes the compiler.

I also simplified this code a bit as a drive-by. It makes no sense to allocate strings one by one. It's much better to do one allocation, split it and store just slices in the resulting vector.

// because the localization code expects pt-br, not pt_BR.
let mut res = BVec::empty();
res.extend(
arena,
s.as_bytes().iter().map(|&b| if b == b'_' { b'-' } else { b }),
);
unsafe { BString::from_utf8_unchecked(res) }
}),
);
let val = BString::from_str(arena, &val).leak();

for c in unsafe { val.as_bytes_mut() } {
if *c == b'_' {
*c = b'-';
}
}

locales.extend_sloppy(arena, val.split(':').filter(|s| !s.is_empty()));
break;
}
}
Expand Down
Loading
Loading