If the locale you request using setlocale is neither ‘C’ nor ‘POSIX’, the C library calls the function __user_find_locale to find a user-supplied locale. The standard implementation of this function is to return a null pointer which indicates that no additional locales are installed and, hence, no locale matches the request.

The prototype for __user_find_locale is:

const __RAL_locale_t *__user_find_locale(const char *locale);

The parameter locale is the locale to find; the locale name is terminated either by a zero character or by a semicolon. The locale name, up to the semicolon or zero, is identical to the name passed to setlocale when you select a locale.

Now let's install the Hungarian locale using both UTF-8 and ISO 8859-2 encodings. The UTF-8 codecs are included in the SEGGER Embedded Studio C library, but the Hungarian locale and the ISO 8859-2 codec are not.

You will find the file locale_hu_HU.c in the source directory as described in the previous section. Add this file to your project.

Although this adds the data needed for the locale, it does not make the locale available for the C library: we need to write some code for __user_find_locale to return the appropriate locales.

To create the locales, we need to add the following code and data to tie everything together:

#include <__crossworks.h>

static const __RAL_locale_t hu_HU_utf8 = {
  "hu_HU.utf8",
  &locale_hu_HU,
  &codeset_utf8
};

static const __RAL_locale_t hu_HU_iso_8859_2 = {
  "hu_HU.iso_8859_2",
  &locale_hu_HU,
  &codeset_iso_8859_2
};

const __RAL_locale_t *
__user_find_locale(const char *locale)
{
  if (__RAL_compare_locale_name(locale, hu_HU_utf8.name) == 0)
    return &hu_HU_utf8;
  else if (__RAL_compare_locale_name(locale, hu_HU_iso_8859_2.name) == 0)
    return &hu_HU_iso_8859_2;
  else
    return 0;
}

The function __RAL_compare_locale_name matches locale names up to a terminating null character, or a semicolon (which is required by the implementation of setlocale in the C library when setting multiple locales using LC_ALL).

In addition to this, you must provide a buffer, __user_locale_name_buffer, for locale names encoded by setlocale. The buffer must be large enough to contain five locale names, one for each category. In the above example, the longest locale name is hu_HU.iso_8859_2 which is 16 characters in length. Using this information, buffer must be at least (16+1)×5 = 85 characters in size:

const char __user_locale_name_buffer[85];