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()
orformat_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 printsThis 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.