Usage of the 't' Function
Comprehensive guide on how to use the t
function to translate and format various types of content.
Using the Extension
By using the Quetzal VSCode/Cursor extension, you should be able to wrap your strings with the t
function automatically. However, this page will document manual use.
Import the ‘t’ function
To translate strings in your app, they must be wrapped in Quetzal’s t
function.
The t
function is how Quetzal knows what to send for translation, and where to put the translated text.
The Quetzal t function is built on top of the next-intl t function, and behaves similarly. Check out the next-intl docs for t function usage instructions.
Wrap Strings
Once you have imported and initialized the t function, you can begin marking strings for translation
Output: Here is normal text
Special cases
Dynamic Text
Use placeholders to dynamically insert values into your translated text.
Output: Here’s a 4
Pluralization
Handle pluralization based on the value passed.
Output: You have 4 followers.
Ordinal Numbers
Format ordinal numbers correctly in different languages.
Output: It’s your 1st birthday!
Multi-case Text
Handle multi-case selections, such as gender-based translations.
Output: He is online.
Escaping Curly Braces
Escape curly braces in your text by using single quotes.
Output: Escape curly braces with single quotes (e.g. {name})
Rich Text Formatting
Embed rich text or HTML elements within your translations.
Output: Please refer to the guidelines.
Embedded HTML Elements
You can embed HTML elements directly within your translated strings.
Output: This is important
Regular Numbers
Format plain numbers according to the user’s locale.
Output: 1,234.56 (in en-US locale)
Currencies
Format numbers as currencies.
Output: $499.90
Percentages
Format numbers as percentages.
Output: 85.7%
Custom Number Formats
Apply custom number formatting.
Output: 1,234.56
ICU Syntax for Numbers
Utilize ICU syntax for formatting numbers within messages.
Output: This product costs €32,000.99
Dates
Format dates according to the user’s locale.
Output: August 12, 2024 (in en-US locale)
Date Ranges
Format a range of dates.
Output: January 1, 2020 – December 31, 2020 (in en-US locale)
Lists (Conjunctions)
Format lists using conjunctions.
Output: HTML, CSS, and JavaScript
Lists (Disjunctions)
Format lists using disjunctions.
Output: HTML, CSS, or JavaScript
Variable Lists
Format lists generated from variables.
Output: Alice, Bob, and Charlie