Opened 14 years ago

Closed 13 years ago

#7495 closed enhancement (fixed)

program to get locale settings

Reported by: mmadia Owned by: leavengood
Priority: normal Milestone: R1
Component: Applications/Command Line Tools Version: R1/Development
Keywords: Cc: zooey
Blocked By: Blocking: #7412
Platform: All

Description

Paraphrasing from ticket:7412#comment:10

We need a command line tool that:

  1. fetches the current locale settings (via the locale kit)
  2. returns a corresponding POSIX locale string.
  3. set the locale environment variable(s) in /etc/profile

Note: having that variable in SetupEnvironment would not be the best place. (SetupEnvironment is executed only on boot-up, so it cannot work for multi-user and locale settings changes wouldn't take effect until reboot.)

Change History (7)

comment:1 by mmadia, 14 years ago

Type: bugenhancement

comment:2 by mmadia, 14 years ago

The following snippet from ReadOnlyBootPrompt will get the preferred language.

	// Get current first preferred language of the user
	BMessage preferredLanguages;
	BLocaleRoster::Default()->GetPreferredLanguages(&preferredLanguages);
	const char* firstPreferredLanguage;
	if (preferredLanguages.FindString("language", &firstPreferredLanguage)
			!= B_OK) {
		// Fall back to built-in language of this application.
		firstPreferredLanguage = "en";
	}

A little snippet for additional motivation ... printf("export LC_CTYPE=%s.UTF-8\n", firstPreferredLanguage);

comment:3 by mmadia, 14 years ago

If that data were written to B_COMMON_ETC_DIRECTORY/locale_type, this would conditionally execute it.

Index: data/etc/profile
===================================================================
--- data/etc/profile	(revision 41373)
+++ data/etc/profile	(working copy)
@@ -5,6 +5,10 @@
 
 echo -e "\nWelcome to the Haiku shell.\n"
 
+if [ -f locale_type ]; then
+	 . /etc/locale_type
+fi
+
 export USER=`id -un`
 export GROUP=`id -gn`
 

... and yes, I don't know how to output the text to a file using C++.

in reply to:  3 comment:4 by bonefish, 14 years ago

Replying to mmadia:

If that data were written to B_COMMON_ETC_DIRECTORY/locale_type, this would conditionally execute it.

I was rather thinking of something like this:

export LANG=`haiku_locale -l`
export LC_TIME=`haiku_locale -t`
export LC_MONETARY=`haiku_locale -m`
...

There's a whole bunch of LC_* variables. Not sure, which we want to set. Possibly all, save LC_ALL (which overrides all) and LC_CTYPE probably won't be necessary since it only specifies the character encoding. LANG is a general fallback for all unset variables, so only setting this one (from what the ReadOnlyBootPrompt snippet yields) would be a start. The string for the formatting specific ones will probably be the same for all. Looking at the API, I guess one would construct it like this:

BFormattingConventions conventions;
BLocale::Default()->GetFormattingConventions(&conventions);
printf("%s_%s.UTF-8\n", conventions.LanguageCode(), conventions.CountryCode());

Don't know, if there's a way to encode the "12/24 clock" and the "month/day names from preferred language" settings into the string.

... and yes, I don't know how to output the text to a file using C++.

Printing to stdout is fine. Though FYI to print to a file in C one would use fprintf() after opening the file with fopen() (close with fclose()). Cf. the Open Group Base Specs for the exact semantics of the functions.

comment:5 by bonefish, 14 years ago

Cc: zooey added
Version: R1/alpha2R1/Development

comment:6 by leavengood, 13 years ago

Owner: changed from nobody to leavengood
Status: newassigned

Sounds fun and fairly easy. Taking ownership.

comment:7 by leavengood, 13 years ago

Resolution: fixed
Status: assignedclosed

Implemented in hrev42394 and hrev42395.

Note: See TracTickets for help on using tickets.