Skip to content
Open
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ cargo run --release -p gridcraft -- --sample sales --control 7979 # + JSON con
cargo xtask ci # fmt, clippy, tests, assets, layering, wasm
```

Choose Español under View → Interface language for [Spanish formula names, decimal commas, and semicolon arguments](docs/formula-input.md).
Choose Español or Português (Brasil) under View → Interface language for [localized formula names, decimal commas, and semicolon arguments](docs/formula-input.md).
Workbooks, clipboard exchange, and programmatic APIs retain canonical English syntax.

### Web
Expand Down
4 changes: 2 additions & 2 deletions ROADMAP.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ The ribbon is translated into Japanese, Simplified Chinese, Korean, Brazilian Po
and German (266–268 labels each, ~12% of ~2,200 strings; merged 10-10 in #31, #104, #82, German
added 10-11), the
interface follows the system language, and CJK, Thai and Hebrew text falls back to system fonts
(#38). Dialogs, menus and messages are English; formulas and number formats are en-US only.
(#38). Brazilian Portuguese formula entry covers all 514 recognized names; number formats remain en-US only.
Localized German formulas and numbers are in PR #163. Detail: [localization-parity.md](docs/localization-parity.md).

| Language | Code | Status | UI strings |
Expand All @@ -104,7 +104,7 @@ Localized German formulas and numbers are in PR #163. Detail: [localization-pari
| Hindi | hi | none | 0% |
| Arabic | ar | none (no RTL layout, no shaping) | 0% |
| French | fr | none | 0% |
| Portuguese | pt-BR | menus only | ~12% |
| Portuguese | pt-BR | menus, dialogs and formulas | ~42% UI plus all formula names |
| Indonesian | id | none | 0% |
| Japanese | ja | menus only | ~12% |
| German | de | menus only | ~12% |
Expand Down
3 changes: 3 additions & 0 deletions contributors/people.toml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ sync_github_name = true
real_name = "James Symes"
sync_github_name = true

[people.paulohsconceicao]
sync_github_name = true


[people.Silvertree2010]
display_name = "Netter"
Expand Down
72 changes: 40 additions & 32 deletions crates/formula/src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub enum FormulaLocale {
#[default]
En,
Es,
PtBr,
}

const SPANISH: &[(&str, &str)] = &[
Expand All @@ -26,26 +27,26 @@ const SPANISH: &[(&str, &str)] = &[

impl FormulaLocale {
pub fn list_separator(self) -> char {
if self == Self::Es { ';' } else { ',' }
if self == Self::En { ',' } else { ';' }
}
pub fn decimal_separator(self) -> char {
if self == Self::Es { ',' } else { '.' }
if self == Self::En { '.' } else { ',' }
}
pub fn array_column_separator(self) -> char {
if self == Self::Es { '\\' } else { ',' }
if self == Self::En { ',' } else { '\\' }
}
pub fn function_name(self, canonical: &str) -> &str {
if self == Self::Es {
SPANISH.iter().find(|(en, _)| en.eq_ignore_ascii_case(canonical)).map(|(_, es)| *es).unwrap_or(canonical)
} else {
canonical
}
self.names().iter().find(|(en, _)| en.eq_ignore_ascii_case(canonical)).map(|(_, localized)| *localized).unwrap_or(canonical)
}
pub fn canonical_name(self, localized: &str) -> &str {
if self == Self::Es {
SPANISH.iter().find(|(_, es)| es.eq_ignore_ascii_case(localized)).map(|(en, _)| *en).unwrap_or(localized)
} else {
localized
self.names().iter().find(|(_, alias)| alias.eq_ignore_ascii_case(localized)).map(|(en, _)| *en).unwrap_or(localized)
}

fn names(self) -> &'static [(&'static str, &'static str)] {
match self {
Self::En => &[],
Self::Es => SPANISH,
Self::PtBr => crate::pt_br::FUNCTIONS,
}
}
}
Expand All @@ -54,8 +55,8 @@ impl FormulaLocale {
/// LET/LAMBDA bindings take precedence over aliases. File and API syntax uses [`crate::parse`].
pub fn parse_input(src: &str, locale: FormulaLocale, known_name: impl Fn(&str) -> bool) -> Result<Expr, ParseError> {
let mut expr = parser::parse_syntax(src, locale)?;
if locale == FormulaLocale::Es {
normalize(&mut expr, &known_name, &mut Vec::new(), 0, false)?;
if locale != FormulaLocale::En {
normalize(&mut expr, locale, &known_name, &mut Vec::new(), 0, false)?;
}
Ok(expr)
}
Expand Down Expand Up @@ -89,7 +90,7 @@ pub fn print_input(expr: &Expr, locale: FormulaLocale, known_name: impl Fn(&str)
return Ok(crate::print(expr));
}
let mut expr = expr.clone();
normalize(&mut expr, &known_name, &mut Vec::new(), 0, true)?;
normalize(&mut expr, locale, &known_name, &mut Vec::new(), 0, true)?;
let canonical = crate::print(&expr);
let tokens = crate::lexer::tokenize(&canonical).map_err(|e| ParseError { msg: e.msg, pos: e.pos })?;
let mut out = String::new();
Expand All @@ -114,30 +115,37 @@ pub fn print_input(expr: &Expr, locale: FormulaLocale, known_name: impl Fn(&str)
Ok(out)
}

pub(crate) fn boolean_alias(name: &str) -> Option<bool> {
if name.eq_ignore_ascii_case("VERDADERO") {
pub(crate) fn boolean_alias(locale: FormulaLocale, name: &str) -> Option<bool> {
if locale.canonical_name(name).eq_ignore_ascii_case("TRUE") {
Some(true)
} else if name.eq_ignore_ascii_case("FALSO") {
} else if locale.canonical_name(name).eq_ignore_ascii_case("FALSE") {
Some(false)
} else {
None
}
}

fn normalize(expr: &mut Expr, known_name: &impl Fn(&str) -> bool, bindings: &mut Vec<String>, depth: usize, display: bool) -> Result<(), ParseError> {
fn normalize(
expr: &mut Expr,
locale: FormulaLocale,
known_name: &impl Fn(&str) -> bool,
bindings: &mut Vec<String>,
depth: usize,
display: bool,
) -> Result<(), ParseError> {
if depth > parser::MAX_DEPTH {
return Err(ParseError { msg: "formula is nested too deeply".into(), pos: 0 });
}
let bound = |name: &str, bindings: &[String]| bindings.iter().rev().any(|n| n.eq_ignore_ascii_case(name)) || known_name(name);
match expr {
Expr::Bool(value) if display => {
let alias = if *value { "VERDADERO" } else { "FALSO" };
let alias = locale.function_name(if *value { "TRUE" } else { "FALSE" });
if !bound(alias, bindings) {
*expr = Expr::Name(alias.into());
}
}
Expr::Name(name) if !display && !bound(name, bindings) => {
if let Some(value) = boolean_alias(name) {
if let Some(value) = boolean_alias(locale, name) {
*expr = Expr::Bool(value);
}
}
Expand All @@ -147,13 +155,13 @@ fn normalize(expr: &mut Expr, known_name: &impl Fn(&str) -> bool, bindings: &mut
for pair in pairs.chunks_mut(2) {
if let [declaration, value] = pair {
// The new name is not in scope in its own value expression.
normalize(value, known_name, bindings, depth + 1, display)?;
normalize(value, locale, known_name, bindings, depth + 1, display)?;
if let Expr::Name(name) = declaration {
bindings.push(name.clone());
}
}
}
normalize(body, known_name, bindings, depth + 1, display)?;
normalize(body, locale, known_name, bindings, depth + 1, display)?;
}
bindings.truncate(saved);
}
Expand All @@ -165,34 +173,34 @@ fn normalize(expr: &mut Expr, known_name: &impl Fn(&str) -> bool, bindings: &mut
bindings.push(name.clone());
}
}
normalize(body, known_name, bindings, depth + 1, display)?;
normalize(body, locale, known_name, bindings, depth + 1, display)?;
}
bindings.truncate(saved);
}
Expr::Call(name, args) => {
let alias = if display { FormulaLocale::Es.function_name(name) } else { FormulaLocale::Es.canonical_name(name) };
let alias = if display { locale.function_name(name) } else { locale.canonical_name(name) };
if !bound(name, bindings) && (!display || !bound(alias, bindings)) {
*name = alias.to_string();
}
for arg in args {
normalize(arg, known_name, bindings, depth + 1, display)?;
normalize(arg, locale, known_name, bindings, depth + 1, display)?;
}
}
Expr::Invoke(callee, args) => {
normalize(callee, known_name, bindings, depth + 1, display)?;
normalize(callee, locale, known_name, bindings, depth + 1, display)?;
for arg in args {
normalize(arg, known_name, bindings, depth + 1, display)?;
normalize(arg, locale, known_name, bindings, depth + 1, display)?;
}
}
Expr::Unary(_, child) | Expr::Paren(child) => normalize(child, known_name, bindings, depth + 1, display)?,
Expr::Unary(_, child) | Expr::Paren(child) => normalize(child, locale, known_name, bindings, depth + 1, display)?,
Expr::Binary(_, left, right) => {
normalize(left, known_name, bindings, depth + 1, display)?;
normalize(right, known_name, bindings, depth + 1, display)?;
normalize(left, locale, known_name, bindings, depth + 1, display)?;
normalize(right, locale, known_name, bindings, depth + 1, display)?;
}
Expr::Array(rows) if display => {
for row in rows {
for child in row {
normalize(child, known_name, bindings, depth + 1, display)?;
normalize(child, locale, known_name, bindings, depth + 1, display)?;
}
}
}
Expand Down
1 change: 1 addition & 0 deletions crates/formula/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ mod input;
pub mod lexer;
pub mod parser;
pub mod printer;
mod pt_br;

pub use ast::{Anchor, BinOp, Expr, RefKind, Reference, SheetSel, StructItem, StructRef, TrimMode, UnOp};
pub use input::{FormulaLocale, parse_input, print_input};
Expand Down
5 changes: 3 additions & 2 deletions crates/formula/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub(crate) fn parse_syntax(src: &str, locale: crate::FormulaLocale) -> Result<Ex
let body = src.strip_prefix('=').unwrap_or(src);
let toks = tokenize_locale(body, locale).map_err(|e| ParseError { msg: e.msg, pos: e.pos })?;
let toks = significant_spaces(toks);
let mut p = Parser { toks, i: 0, depth: 0, input: locale == crate::FormulaLocale::Es };
let mut p = Parser { toks, i: 0, depth: 0, input: locale != crate::FormulaLocale::En, locale };
let e = p.expr(0, false)?;
match p.peek() {
Tok::Eof => Ok(e),
Expand Down Expand Up @@ -68,6 +68,7 @@ struct Parser {
i: usize,
depth: usize,
input: bool,
locale: crate::FormulaLocale,
}

fn word_is_cols(w: &str) -> Option<(u32, bool)> {
Expand Down Expand Up @@ -384,7 +385,7 @@ impl Parser {
},
Tok::Text(s) => Expr::Text(Arc::from(s)),
Tok::Bool(b) => Expr::Bool(b),
Tok::Word(w) if self.input => crate::input::boolean_alias(&w)
Tok::Word(w) if self.input => crate::input::boolean_alias(self.locale, &w)
.map(Expr::Bool)
.ok_or_else(|| self.err(format!("array constants may only contain constants, found {}", describe(&Tok::Word(w)))))?,
Tok::Error(e) => Expr::Error(e),
Expand Down
Loading