1// The system's languages and which way a language is written, apart2// from std::locale so that a program asking only these loads none of3// CLDR's locale tables (alloy-ui's REFOCUS.md, R6e): what an app's4// language and direction are chosen by.5//6// ```alloy7// expect(writes_right_to_left(&"ar-EG") && writes_right_to_left(&"he"));8// expect(!writes_right_to_left(&"fr") && writes_right_to_left(&"pa-Arab"));9// ```1011import std::string;12import std::vector;13import std::option;14import std::process;15import std::ffi;16import std::language_data;1718extern al_sys_languages(buffer: &var [u8], capacity: u64) -> i64;19extern al_locale_page_languages(at: c_pointer, capacity: u32) -> u32; // languages.web.js2021// The system's languages, most wanted first, as BCP 47 tags (`ar-EG`,22// `en`): `LANGUAGE`'s, then the first of `LC_ALL`, `LC_MESSAGES` and23// `LANG` set, on Linux and under WASI (`C` and `POSIX` left out);24// GetUserPreferredUILanguages on Windows; CFLocaleCopyPreferredLanguages25// on macOS; navigator.languages on the web. Empty when it names none.26// Reads none of the locales' tables.27exp fn preferred_languages() -> Vector<String> {28 var buffer: [u8 : 1024] = [0 : 1024];29 var length: u64 = 0;30 if (target() == "web-wasm32") {31 length = al_locale_page_languages(c_pointer::of(&buffer[..]), 1024) to u64;32 } else {33 const given = al_sys_languages(&var buffer, 1024);34 length = if (given < 0) 0 else given as u64;35 }36 var languages = Vector<String>::empty();37 if (length > 1024) {38 return move languages;39 }40 // tags between colons, each without its codeset and modifier41 var from: u64 = 0;42 for ([..length + 1]) |k| {43 if (k < length && buffer[k] != ':') {44 continue;45 }46 var tag = &buffer[from..k];47 from = k + 1;48 for (tag, [..tag.length()]) |c, at| {49 if (c == '.' || c == '@') {50 tag = &tag[..at];51 break;52 }53 }54 if (tag.length() == 0 || tag == "C" || tag == "POSIX") {55 continue;56 }57 var written = String::empty();58 for (tag) |c| {59 written.push(if (c == '_') '-' else c);60 }61 var seen = false;62 for (languages.view()) |&l| {63 seen = seen || l == written;64 }65 if (!seen) {66 languages.push(move written);67 }68 }69 return move languages;70}7172// Whether text in the language the tag names is written right to left:73// by the script it names (Arab, Hebr, Thaa, ...), else by its language74// (ar, fa, he, ur, ...), from CLDR as `right_to_left()` is, but from a75// list of their names rather than the locales' tables.76exp fn writes_right_to_left(tag: &[u8]) -> bool {77 // the language and the subtag after it, which names a script when it78 // is four letters; no `Tag` is made, so a program asking only this79 // links none of `read_tag`80 var subtags: [&[u8] : 2] = [&tag[..0], &tag[..0]];81 var count: u64 = 0;82 var from: u64 = 0;83 for ([..tag.length() + 1]) |k| {84 if (count < 2 && (k == tag.length() || tag[k] == '-' || tag[k] == '_')) {85 subtags[count] = &tag[from..k];86 count += 1;87 from = k + 1;88 }89 }90 const script = &subtags[1];91 if (script.length() == 4 && all_letters(&script)) {92 return listed(&RIGHT_TO_LEFT_SCRIPTS, &script);93 }94 // Hebrew's and Yiddish's old codes too, as `read_tag` renames them95 const old: [&[u8] : 2] = [&"iw", &"ji"];96 return listed(&RIGHT_TO_LEFT_LANGUAGES, &subtags[0]) || listed(&old, &subtags[0]);97}9899// Whether the name is among the names, whatever the case of its letters.100fn listed(names: &[&[u8]], name: &[u8]) -> bool {101 for (names) |&n| {102 if (n.length() == name.length()) {103 var same = true;104 for (n, name) |a, b| {105 same = same && (a | 0x20) == (b | 0x20);106 }107 if (same) {108 return true;109 }110 }111 }112 return false;113}114fn all_letters(text: &[u8]) -> bool {115 for (text) |c| {116 if (!((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))) {117 return false;118 }119 }120 return true;121}122123/* --- How it was built --- */124125// **F14a as built** (2026-10-20; alloy-ui's decisions/F14.md, "Right to126// left"):127// - `preferred_languages()` asks the platform through one `al_sys_*`128// function a system C file (`al_sys_languages`, colon-separated tags in129// a buffer): `LANGUAGE`'s list then the first of `LC_ALL`,130// `LC_MESSAGES` and `LANG` on POSIX (`C`, `C.*` and `POSIX` naming131// none), GetUserPreferredUILanguages on Windows,132// CFLocaleCopyPreferredLanguages through `dlsym` on macOS, the133// interpreter through `system_host.alloy`; the web through this134// module's sidecar, `al_locale_page_languages` joining135// navigator.languages. Codesets and modifiers are dropped, `_` becomes136// `-`, repeats go.137// - `writes_right_to_left(tag)` reads the tag's script, else its138// language, against `RIGHT_TO_LEFT_SCRIPTS` (37, from CLDR's139// scriptMetadata) and `RIGHT_TO_LEFT_LANGUAGES` (23, the bare-language140// locales whose layout's characterOrder is right-to-left), which141// cldr/generate.alloy writes (into language_data.alloy since R6e); no142// locale's tables are read.143// - Tests: `tests/driver/locale_languages.sh` checks144// `writes_right_to_left` against `right_to_left()` on every locale and145// reads the languages from the environment interpreted, natively,146// under wasmtime and in node.147//148// **R6e** (2026-10-22, alloy-ui's REFOCUS.md): both moved here from149// std::locale with their externs and the web's in this module's sidecar150// (`languages.web.js`), so that pkg::alloy::ui reaches them without the151// locale's tables.