Development Guides Home >> Guide to Locales

Guide to Locales - Basic Usage

Introduction

You can display locale system strings in custom code. This document includes examples for the use of the locale system in custom scripts or applications, or in custom interfaces.

Basic usage

The following example code uses the locale system to localize the string This is a phrase. in the application's output:

Perl

Copy
Copied
#!/usr/bin/perl

# Load the Cpanel::Locale module.
use Cpanel::Locale ();

# Declare the $locale variable.
my $locale = Cpanel::Locale->get_handle();

# Print a localized message.
print $locale->maketext('This is a phrase.');

Load the Cpanel::Locale module

Copy
Copied
# Load the Cpanel::Locale module.
use Cpanel::Locale ();

Loading the Cpanel::Locale module ensures that the script or application can access the locale system. For more information read the pearldoc.perl.org perlobj documentation.

Get the user's locale setting

Copy
Copied
# Declare the $locale variable.
my $locale = Cpanel::Locale->get_handle();

This section of the code uses the maketext() method to print the string This is a phrase. in the authenticated user's locale.

  • For example, when an authenticated user with the en locale runs this subroutine, the system prints This is a phrase.
  • If the authenticated user used the es locale and this phrase was present in the locale's lexicon, the sytem would print the same phrase in Spanish ( Esta es una frase. ).
  • If the authenticated user's locale does not contain this phrase, the phrase will display in the default locale (English).

Template Toolkit files

Copy
Copied
<p>[% locale.maketext('This is a phrase.') %]</p>

Template Toolkit templates automatically access the authenticated user's locale.

For more information, read our Guide to Template Toolkit in cPanel & WHM documentation.

JavaScript

Copy
Copied
LOCALE.maketext("This is a phrase.");

As long as the CJT locale.js file loads properly, JavaScript applications will automatically access the authenticated user's locale settings and the locale system.