Child pages
  • Guide to Locales - Basic Usage
Skip to end of metadata
Go to start of metadata

 

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.

Notes:

Basic usage

Select a tab to view basic locale system usage for that type of code:

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

#!/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

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

Line 4 loads the Cpanel::Locale module. This ensures that the script or application can access the locale system. For more information, read the  perldoc.perl.org perlobj documentation .

Get the user's locale setting

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

Line 7 uses the get_handle() method to retrieve the authenticated user's locale object . This instantiates the locale object.

Note:

The get_handle() method does not require arguments.

Localize a string

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

Line 10 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 system 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).

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

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

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

Note:

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

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

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.