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.

Notes:

  • Many developers will want to use the most common Locale::Maketext bracket notation methods (for example, thenumf() or format_bytes() methods).

Basic usage

Perl 

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

1
2
3
4
5
6
7
8
9
10
#!/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

3
4
# 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. 

Get the user's locale setting

6
7
# 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

9
10
# 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).


Template Toolkit Files

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.



  • 13 Users Found This Useful
Was this answer helpful?

Related Articles

The cPanel Interface

For  cPanel  &  WHM  version  58 Overview The cPanel interface is...

User Preferences

For cPanel & WHM version 58 Overview This document outlines how to access your cPanel...

Manage External Authentications

For cPanel & WHM version 58 Overview Manage credentials Additional documentation...

What is cPanelID?

In This Article:  Overview ServicesHow to get a cPanelID cPanelID External...

Guide to cPanel Interface Customization - cPanel Style Development

Introduction You can develop custom styles that modify the appearance of the cPanel interface....