Enforcing plain-text strings with the fbs API
TL;DR
fbsis a specialized version of thefbtAPI to represent only translatable plain-text strings- It's a subset of
fbtsince the latter can also represent rich text contents (like a mix of text and HTML elements)
- It's a subset of
- Use
fbswhenever you want to force the translated result to be a plain string.- This is typically useful for HTML attributes whose value can only be plain text strings (e.g.
title,placeholder,alt,aria-label)
- This is typically useful for HTML attributes whose value can only be plain text strings (e.g.
- The translation process of
fbsis the same as with regular fbt strings
What is it?
fbsrepresents a translatable plain-text string- It's a subset of fbt which can also represent rich text contents (i.e. a mix of text and HTML elements)
fbsmeans something like "FB string", it's not a true acronym. 😅
Why using it?
- Enforce plain text only translatable strings, which is useful for:
- Writing localizable HTML attributes like
title,label,placeholder, etc...- Why again? Because those HTML attributes only expect a string value; no HTML!
- Page titles, emails subjects, API responses or any code where you need to enforce those same constraints
- Writing localizable HTML attributes like
How to use it?
- Use the
fbs()functional API (recommended)- You can still use the
<fbs>HTML API (see the transform requirements), but it can't be used inside an HTML attribute value.
- You can still use the
- All existing fbt constructs are supported. Just write
fbsinstead offbt.- E.g.
<fbt:param>and<fbs:param>, orfbt::plural()andfbs::plural()work the same way. - Use the constructs of the same module, e.g.
fbs::param()(notfbt::param()) withinfbs(). - Options like
desc,common,project, ... are supported too. For common strings, the description can be omitted:fbs('Accept', ['common' => true]).
- E.g.
- 🚨 IMPORTANT: the values of
fbs:param/fbs::param()(andfbs::plural()values) must be strings orfbsresults, otherwise an exception is thrown. HTML elements inside<fbs>are not allowed either. - ⚠️ NOTE: like with
fbt, the parameter values are not escaped! fbtandfbscan't be nested in each other, but anfbsresult can be passed as a parameter value tofbtorfbs.- How to submit translation requests for it?
- Please follow the same process as for the regular
fbtstrings
- Please follow the same process as for the regular
Examples in HTML
<input
type="search"
placeholder="<?=fbs('Search', 'search input placeholder')?>"
aria-label="<?=fbs('Search', 'search input label')?>">
<fbs desc="some desc">Hello world!</fbs>
<fbs desc="some desc">
Hello
<fbs:name name="name" gender="<?=$someGender?>"><?=$name?></fbs:name>
</fbs>
Examples in regular PHP
$myPlainTranslatedText = fbs('Hello world!', 'description');
$myPlainTranslatedText = fbs(
[
'I have ',
\fbt\fbs::plural('a dream', $count, [
'many' => 'dreams',
'showCount' => 'yes',
]),
'.',
],
'desc',
);
// singular text = "I have a dream."
// plural text = "I have {number} dreams."
// the string is rendered when the object is converted to a string
$title = (string)$myPlainTranslatedText;
What do fbs result values return?
- Upon invoking
fbs(), you'll receive an\fbt\fbsobject, which is rendered when it's converted to a string. - At runtime, the translated result is built by the
getFbsResulthook, which returns anFbtPureStringResultby default. fbsvalues can be used in lieu offbtvalues (e.g. as afbt::param()value)fbtvalues CANNOT be used in lieu offbsvalues (as expected)fbsstrings are never inlined for translation, since they are meant to be used as plain text.