Skip to content

Instantly share code, notes, and snippets.

@alabamenhu
Created April 8, 2021 17:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alabamenhu/a84cec3a6f976302368525b26fc74907 to your computer and use it in GitHub Desktop.
Save alabamenhu/a84cec3a6f976302368525b26fc74907 to your computer and use it in GitHub Desktop.
# To enable known localization frameworks, include their identifiers in the use statement
use Intl::Localization <xliff fluent>;
# Using the module inserts a dynamic variable used for settings
# $*INTL-LOCALIZATION-DOMAIN <-- a "grouping" of files. Only used where
# $*INTL-LOCALIZATION-LANGUAGE
# Tells the framework that files will be structure like
# foo/en.ftl
# foo/es-ES.ftl
add-localization-basepath 'foo/' ~ * ~ '.ftl';
# Tells the framework that files will be structure like
# foo/en/bar.ftl
# foo/es-ES/bar.ftl
add-localization-basepath 'foo/' ~ * ~ '/bar.ftl';
# Tells the framework that even though the file extension
# is xml, treat it as an xliff type
add-localization-basepath 'foo/' ~ * ~ '.xml', :type<xliff>;
# Tells the framework that even though the file extension
# is xml, treat it as an xliff type
add-localization-basepath 'foo/' ~ * ~ '.xml', :type<xliff>;
# Tells the framework that the localization files are in the
# resource folder
add-localization-basepath 'foo/' ~ * ~ '.ftl', :resource;
# Tells the framework to use a separate named manager
# is xml, treat it as an xliff type
add-localization-basepath 'foo/' ~ * ~ '.xml', :domain<websiteA>;
# Retrieve a localized string
localize "message-id";
# Retrieve a localized string passing in an argument
# (to be interpreted by each localization engine)
localize "message-id", $pos1, $pos2, :$named1, :$named2;
# by default, localizations will work as expected, but the current
# message settings can be adjusted on the fly by using
localize "title"; # "Webseitentitel" (or whatever was loaded without a :domain arg)
localization-domain 'website'; # switch away from the default set of messages
localization-language 'en-US'; # switch away from the default language (user-language or inherited)
localize "title"; # "Website Title" (loaded from files with :domain<website>, and in English)
# but these settings can lexically scoped (and follow call scope!) by using
localization-domain { 
localization-domain 'different-website';
localize "title"; # "Title of Different Website"
localization-language 'es-ES';
localize "title"; # "Título de sitio distinto"
}
localize "title"; # Back to "Website Title";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment