The Open Group Base Specifications Issue 8
IEEE Std 1003.1-2024
Copyright © 2001-2024 The IEEE and The Open Group

NAME

newlocale — create or modify a locale object

SYNOPSIS

[CX] [Option Start] #include <locale.h>

locale_t newlocale(int
category_mask, const char *locale,
       locale_t
base); [Option End]

DESCRIPTION

The newlocale() function shall create a new locale object or modify an existing one. If the base argument is (locale_t)0, a new locale object shall be created, otherwise the locale specified by base shall be modified. In the latter case it is unspecified whether the resulting locale object shall be that pointed to by base modified in place, or whether that object shall be freed after a new locale object is first created using some values from it.

The category_mask argument specifies the locale categories to be set or modified. Values for category_mask shall be constructed by a bitwise-inclusive OR of the symbolic constants LC_CTYPE_MASK , LC_NUMERIC_MASK , LC_TIME_MASK , LC_COLLATE_MASK , LC_MONETARY_MASK , and LC_MESSAGES_MASK , or any of the implementation-defined mask values defined in <locale.h>.

For each category with the corresponding bit set in category_mask the data from the locale named by locale shall be used. In the case of modifying an existing locale object, the data from the locale named by locale shall replace the existing data within the locale object. If a completely new locale object is created, the data for all sections not requested by category_mask shall be taken from the POSIX locale.

The following preset values of locale are defined for all settings of category_mask:

"POSIX"
Specifies the minimal environment for C-language translation called the POSIX locale.
"C"
Equivalent to "POSIX".
""
Specifies an implementation-defined native environment. This corresponds to the value of the associated environment variables, LC_* and LANG ; see XBD 7. Locale and 8. Environment Variables.

If the base argument is not (locale_t)0 and the newlocale() function call succeeds, the contents of base are unspecified. Applications shall ensure that they stop using base as a locale object before calling newlocale(). If the function call fails and the base argument is not (locale_t)0, the contents of base shall remain valid and unchanged.

The behavior is undefined if the base argument is the special locale object LC_GLOBAL_LOCALE, or is not a valid locale object handle and is not (locale_t)0.

RETURN VALUE

Upon successful completion, the newlocale() function shall return a handle which the caller may use on subsequent calls to duplocale(), freelocale(), and other functions taking a locale_t argument.

Upon failure, the newlocale() function shall return (locale_t)0 and set errno to indicate the error.

ERRORS

The newlocale() function shall fail if:

[ENOMEM]
There is not enough memory available to create the locale object or load the locale data.
[EINVAL]
The category_mask contains a bit that does not correspond to a valid category.
[ENOENT]
For any of the categories in category_mask, the locale data is not available.

The newlocale() function may fail if:

[EINVAL]
The locale argument is not a valid string pointer.

The following sections are informative.

EXAMPLES

Constructing a Locale Object from Different Locales

The following example shows the construction of a locale where the LC_CTYPE category data comes from a locale loc1 and the LC_TIME category data from a locale loc2:

#include <locale.h>
...
locale_t loc, new_loc;

/* Get the "loc1" data. */
loc = newlocale (LC_CTYPE_MASK, "loc1", (locale_t)0); if (loc == (locale_t) 0) abort ();
/* Get the "loc2" data. */
new_loc = newlocale (LC_TIME_MASK, "loc2", loc); if (new_loc != (locale_t) 0) /* We don t abort if this fails. In this case this simply used to unchanged locale object. */ loc = new_loc;
...
Freeing up a Locale Object

The following example shows a code fragment to free a locale object created by newlocale():

#include <locale.h>
...

/* Every locale object allocated with newlocale() should be * freed using freelocale(): */
locale_t loc;
/* Get the locale. */
loc = newlocale (LC_CTYPE_MASK | LC_TIME_MASK, "locname", (locale_t)0);
/* ... Use the locale object ... */ ...
/* Free the locale object resources. */ freelocale (loc);

APPLICATION USAGE

Handles for locale objects created by the newlocale() function should either be released by a corresponding call to freelocale(), or be used as a base locale to another newlocale() call.

The special locale object LC_GLOBAL_LOCALE must not be passed for the base argument, even when returned by the uselocale() function.

RATIONALE

None.

FUTURE DIRECTIONS

None.

SEE ALSO

duplocale, freelocale, getlocalename_l, uselocale

XBD 7. Locale, 8. Environment Variables, <locale.h>

CHANGE HISTORY

First released in Issue 7.

POSIX.1-2008, Technical Corrigendum 1, XSH/TC1-2008/0403 [227], XSH/TC1-2008/0404 [283], XSH/TC1-2008/0405 [295], and XSH/TC1-2008/0406 [227] are applied.

POSIX.1-2008, Technical Corrigendum 2, XSH/TC2-2008/0232 [781] and XSH/TC2-2008/0233 [673] are applied.

Issue 8

Austin Group Defect 1220 is applied, adding getlocalename_l() to the SEE ALSO section.

Austin Group Defect 1243 is applied, clarifying the handling of a non-zero base argument.

Austin Group Defect 1264 is applied, changing "default locale" to "POSIX locale".

End of informative text.

 

return to top of page

UNIX® is a registered Trademark of The Open Group.
POSIX™ is a Trademark of The IEEE.
Copyright © 2001-2024 The IEEE and The Open Group, All Rights Reserved
[ Main Index | XBD | XSH | XCU | XRAT ]