From 02c8ecde3e5bffc73913ba1d318a5bf21157d1f5 Mon Sep 17 00:00:00 2001 From: Sasha Koshka Date: Fri, 4 Oct 2024 23:25:18 -0400 Subject: [PATCH] LC_LANG now works as a fallback properly --- cmd/locale/main.ha | 24 ++++++++++++++++++++++++ locale/+linux.ha | 19 +++++++++---------- locale/-linux.ha | 8 -------- 3 files changed, 33 insertions(+), 18 deletions(-) create mode 100644 cmd/locale/main.ha diff --git a/cmd/locale/main.ha b/cmd/locale/main.ha new file mode 100644 index 0000000..bd0ad64 --- /dev/null +++ b/cmd/locale/main.ha @@ -0,0 +1,24 @@ +use fmt; +use locale; + +export fn main() void = { + let ctype = locale::format(locale::get_ctype()); + defer free(ctype); + let collate = locale::format(locale::get_collate()); + defer free(collate); + let monetary = locale::format(locale::get_monetary()); + defer free(monetary); + let numeric = locale::format(locale::get_numeric()); + defer free(numeric); + let time = locale::format(locale::get_time()); + defer free(time); + let messages = locale::format(locale::get_messages()); + defer free(messages); + + fmt::printf("LC_CTYPE={}\n", ctype)!; + fmt::printf("LC_COLLATE={}\n", collate)!; + fmt::printf("LC_MONETARY={}\n", monetary)!; + fmt::printf("LC_NUMERIC={}\n", numeric)!; + fmt::printf("LC_TIME={}\n", time)!; + fmt::printf("LC_MESSAGES={}\n", messages)!; +}; diff --git a/locale/+linux.ha b/locale/+linux.ha index 2649d95..8cb943e 100644 --- a/locale/+linux.ha +++ b/locale/+linux.ha @@ -37,25 +37,24 @@ export fn get_time() locale = get_locale("LC_TIME"); // overwritten later, so use [[dup]] to extend its lifetime. export fn get_messages() locale = get_locale("LC_MESSAGES"); -// Returns the locale to use for linguistic translations. -// The memory is statically allocated and must not be free'd. It may be -// overwritten later, so use [[dup]] to extend its lifetime. -export fn get_lang() locale = get_locale("LC_LANG"); - -// TODO fn get_language() []locale; fn get_locale(var: str) locale = - match (get_env_locale(var)) { + match (get_locale_no_fallback(var)) { case let local: locale => yield local; case => - yield match (get_locale_conf_entry(var)) { + yield match (get_locale_no_fallback("LC_LANG")) { case let local: locale => yield local; - case => - yield c; + case => yield c; // TODO: get from LC_LANGUAGE }; }; +fn get_locale_no_fallback(var: str) (locale | errors::invalid) = + match (get_env_locale(var)) { + case let local: locale => yield local; + case => yield (get_locale_conf_entry(var)); + }; + fn get_env_locale(var: str) (locale | errors::invalid) = match (os::getenv(var)) { case let env: str => return parse(env); diff --git a/locale/-linux.ha b/locale/-linux.ha index afd3df5..f31a887 100644 --- a/locale/-linux.ha +++ b/locale/-linux.ha @@ -30,11 +30,3 @@ export fn get_time() locale = c; // The memory is statically allocated and must not be free'd. It may be // overwritten later, so use [[dup]] to extend its lifetime. export fn get_messages() locale = c; - -// Returns the locale to use for linguistic translations. -// The memory is statically allocated and must not be free'd. It may be -// overwritten later, so use [[dup]] to extend its lifetime. -export fn get_lang() locale = c; - -// TODO -export fn get_language() []locale;