Accessing locale settings

How to

Access the locale settings to determine the language and country associated with a user's BlackBerry PlayBook tablet.

Solution

Retrieve current locale settings

char* country = NULL;
char* language = NULL;

bps_initialize();
locale_get(&language, &country);

// This is where you handle the locale information

Listen for changes to locale settings

bps_initialize();
locale_request_events(0);

while (!shutdown) {
    bps_event_t *event = NULL;
    bps_get_event(&event, -1); // -1 means that the function blocks
                               // while it waits for an event
    if (event) {
        if (bps_event_get_domain(event) == locale_get_domain()) {
            if (LOCALE_INFO == bps_event_get_code(event)) {
                const char* language = locale_event_get_language(event);
                const char* country = locale_event_get_country(event);

                // This is where you handle the locale information
            }
        }
    }
}

Build requirements

You must include the following header files from the BlackBerry Platform Services (BPS) library:

#include <bps/bps.h>
#include <bps/locale.h>

Discussion

Retrieving the locale settings from the tablet is easy enough - just initialize the BlackBerry Platform Services library and call the locale_get() function from the bps/locale.h library. To listen for changes to locale settings, you must first register the application to receive locale events. Once your application has been registered to receive events, you can call bps_get_event() to retrieve the application's next event.

Once you've retrieved the event, you just need to confirm that it is in fact a locale event. First, you should verify the domain of the event to make sure it has the locale domain. Then you need to verify the code for the event. In this case, you're looking for events of the LOCALE_INFO type since these events are generated whenever the current language or country changes. Once you've determined that this is the event you're looking for, all that's left to do is retrieve the language and country from the event and respond to the change as you see fit.