// Get all currently set values
hx.userFacingText()
// Get all values that were first set
hx.userFacingText.defaults()
// Set multiple values
var text = {
form: {
missingValue: 'Value Missing'
},
yourComponentName: {
yourKeyValue: 'Some Text'
}
}
hx.userFacingText(text)
// Get and set inbuilt values:
hx.userFacingText('form', 'missingValue') // Get the text for the 'form' module and 'missingValue' key
hx.userFacingText('form', 'missingValue', 'Value Missing') // Set the text
// Get and set custom values
hx.userFacingText('yourComponentName', 'yourKeyValue', 'Some Text') // Set a custom user facing text
hx.userFacingText('yourComponentName', 'yourKeyValue') // Returns: "Some Text"
// Plurals use the `$n` parameter. This defaults to 1 if not provided.
hx.userFacingText('example', 'plural-key', [
[null, 0, 'Value Zero'],
[1, 1, 'Value Singular'],
[2, 2, 'Value Two'],
[3, null, 'Value $n']
])
hx.userFacingText('example', 'plural-key')
hx.userFacingText('example', 'plural-key', { n: 0 })
// => 'Value Singular'
hx.userFacingText('example', 'plural-key', { n: 0 })
// => 'Value Zero'
hx.userFacingText('example', 'plural-key', { n: 2 })
// => 'Value Two'
hx.userFacingText('example', 'plural-key', { n: 100 })
// => 'Value 100'
// The following are equivalent:
hx.userFacingText('form', 'missingValue', 'Value Missing') // Set the text
hx.userFacingText('form', 'missingValue', [[null, null, 'Value Missing']) // Set the text
<h4>Main Page - <span id="currentVersion"></span></h4>
<h4>Main Page - <span id="currentVersion-noparse"></span></h4>
<h4>Main Page - <span id="currentVersion-arrayparam"></span></h4>
<div id="introduction"></div>
<button id="confirmBtn" class="hx-btn"></button>
<button id="cancelBtn" class="hx-btn"></button>
// Translation text is two-levels deep
const myText = {
buttons: {
confirm: 'Confirm',
},
homePage: {
introduction: 'This is an introduction.\nIt contains multiple lines',
version: 'Current version: $version',
versionCustom: 'Current version: {0}',
},
}
// Set with an object
hx.userFacingText(myText)
// Set with a specific key
hx.userFacingText('buttons', 'cancel', 'Cancel')
// Use keys
hx.select('#confirmBtn').text(hx.userFacingText('buttons', 'confirm'))
hx.select('#cancelBtn').text(hx.userFacingText('buttons', 'cancel'))
// Use multiline strings in selections
hx.select('#introduction').add(hx.userFacingText.toMultilineSelection(hx.userFacingText('homePage', 'introduction')))
// Inject parameters
hx.select('#currentVersion').text(hx.userFacingText('homePage', 'version', { version: '10.10.10' }))
// Parse parameters after creation
const customVersionText = hx.userFacingText('homePage', 'version', true)
hx.select('#currentVersion-noparse').text(hx.userFacingText.format(customVersionText, { version: '1.2.3' }))
// Custom param replacement
const customText = hx.userFacingText('homePage', 'versionCustom')
const arrayReplacer = (str, key, array) => str.replace(new RegExp(`\\\{${key}\\\}`, 'g'), array[key]);
hx.select('#currentVersion-arrayparam').text(hx.userFacingText.format(customText, ['10.11.12'], arrayReplacer));
var text = {
picker: {
chooseValue: 'Text'
},
form: {
typeMismatch: 'Type mismatch error',
missingValue: 'Missing Value'
}
}
hx.userFacingText(text)
hx.userFacingText(hx.userFacingText.defaults())
$n
parameter is used to indicate how many of the value are being used. $n
parameter is not provided when getting the value, it defaults to 1. hx.userFacingText('something', 'value', 'the value')
hx.userFacingText('something', 'value')
// => 'the value'
// Plurals use the `$n` parameter. This defaults to 1 if not provided.
hx.userFacingText('example', 'plural-key', [
[null, 0, 'Value Zero'],
[1, 1, 'Value Singular'],
[2, 2, 'Value Two'],
[3, null, 'Value $n']
])
hx.userFacingText('example', 'plural-key')
hx.userFacingText('example', 'plural-key', { n: 1 })
// => 'Value Singular'
hx.userFacingText('example', 'plural-key', { n: 0 })
// => 'Value Zero'
hx.userFacingText('example', 'plural-key', { n: 2 })
// => 'Value Two'
hx.userFacingText('example', 'plural-key', { n: 100 })
// => 'Value 100'
[
[min, max, string]
]
$n
that will show this string. When using null
, this is treated as 'everything below max'. $n
that will show this string. When using null
, this is treated as 'everything above min'. $n
is between min/max. hx.userFacingText.format(string, parameters)
and allows only $param
style parameters. hx.userFacingText({
myModule: {
helloUser: 'Hello $user!',
},
})
hx.userFacingText('myModule', 'helloUser', { user: 'Bob' })
// => Hello Bob!
false
textFirst = {
module: {
key1: 'value1orig',
key2: 'value2orig',
key4: 'value4orig'
}
}
textSecond = {
module: {
key1: 'value1changed',
key3: 'value3orig',
key4: 'value4changed',
}
otherModule: {
key: 'valueorig',
}
}
userFacingText(textFirst)
userFacingText(textSecond)
userFacingText()
// => {
// module: {
// key1: 'value1changed',
// key2: 'value2orig',
// key3: 'value3orig',
// key4: 'value4changed',
// },
// otherModule: {
// key: 'valueorig'
// }
// }
userFacingText.defaults()
// => {
// module: {
// key1: 'value1orig',
// key2: 'value2orig',
// key3: 'value3orig',
// key4: 'value4orig',
// },
// otherModule: {
// key: 'valueorig'
// }
// }
hx.userFacingText.format('Hello $name!', { name: 'Bob' })
// => Hello Bob!
function arrayReplacer (str, key, array) {
return str.replace(new RegExp("\\\{#{key}\\\}", 'g'), array[key])
}
hx.userFacingText.format('{0} {1}', ['A', 'B'], arrayReplacer)
// => 'A B'
function arrayReplacer (str, key, array) {
return str.replace(new RegExp("\\\{#{key}\\\}", 'g'), array[key])
}
function replaceNumericArgs(string, thingsToReplace...) {
return hx.userFacingText.format(text, thingsToReplace, arrayReplacer)
}
replaceNumericArgs('{0} {1} {2}', 'A', 'B', 'C')
// => 'A B C'
$param
values to replace. format
uses Object.keys(parameters)
and then reduces over the string with this function to produce the final parsed string. // Replaces $key with parameters[key]
function (str, key, parameters) {
return str.replace(new RegExp("\\\$#{key}", 'g'), parameters[key])
}
format
\n
characters into a selection containing text elements separated by br
elements. br
tag. <h4>Basic Example</h4>
<div id="toMultiline-example-1"></div>
<h4>Paragraph Example</h4>
<div id="toMultiline-example-2"></div>
const stringWithMultiline = 'First Line\nSecond Line\nThird Line';
hx.select('#toMultiline-example-1').add(hx.userFacingText.toMultilineSelection(stringWithMultiline))
hx.select('#toMultiline-example-2').add(hx.userFacingText.toMultilineSelection(stringWithMultiline, 'p', true))
\n
characters. 'span'
br
elements from the selection false
br
tags.