text
- Description:
- Does string formatting and translation given a locale.
Because method names are going to be used in templates, we keep them short, but descriptive.
Method names are nouns describing what they return (rather than verbs), e.g..lower()and.upper()
Methods ususally take one argument, and an additional options dictionary. See options for full documentation of available options.
Some methods can be chained, for readability:month().upper()is equivalent toupper(month()).
- Does string formatting and translation given a locale.
Methods
(static) capital(text) → {string}
- Description:
- Make the first character upper case, if language has uppercase letters and everything else lowercase.
Examples
sv
// returns 'Istanbul'
capital("istanbul")
// returns 'Istanbul'
capital("INSTANBUL")
tr
// returns 'İstanbul'
capital("istanbul")
Parameters:
| Name | Type | Description |
|---|---|---|
text |
string | Input string |
Returns:
Capitalised string
- Type
- string
(static) char(codePoint) → {string}
- Description:
- Print character by Unicode code point, or HTML entity name.
This is useful for printing invisible characters where HTML
entities can not be used for some reason.
Note that as of ECMAScript 5,charis no longer a reserved word.
- Print character by Unicode code point, or HTML entity name.
This is useful for printing invisible characters where HTML
entities can not be used for some reason.
Example
// returns ' '
char("nbsp")
// returns '←'
char("ShortLeftArrow")
// returns '〹'
char(12345)
// returns '你'
char(0x2F804)
Parameters:
| Name | Type | Description |
|---|---|---|
codePoint |
number | string | A codepoint, or HTML entity name |
Returns:
A character
- Type
- string
(static) debugData(data) → {string}
- Description:
- Utility function for inserting debugging data (used by the robot-writer)
Example
// returns '<span data-time="20:15" data-author="Mary"></span>'
debugData({time: "20:15", author: "Mary"})
Parameters:
| Name | Type | Description |
|---|---|---|
data |
object | to insert as html5 data properties |
Returns:
html
- Type
- string
(static) demonym(isoCode, formopt, optsopt) → {string}
- Description:
- Get a stylistically neutral demonym for a territory. Limited data available.
Example
sv
// returns 'stockholmare'
territory("se-0180")
// returns 'stockholmaren'
territory("se-0180", "sg def")
// returns 'stockholmarnas'
territory("se-0180", "plural def")
// returns 'stockholmarnas'
territory("se-0180", ["plural" "def"])
Parameters:
| Name | Type | Attributes | Description | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
isoCode |
string | CLDR territory code | |||||||||||
form |
string | Array |
<optional> |
Grammatical case, or other form, e.g. "plural genitive", in any order. This is theoretically an open set of grammatical terms, but for Swedish: - sg/singular/one (default) OR pl/plural/other - indef/indefinite (defaulr) OR def/definite - nom/nominative (default) OR gen/genitive |
||||||||||
opts |
object |
<optional> |
Options
Properties
|
Returns:
demonym
- Type
- string
(static) footnote(referenceopt, optsopt) → {string}
- Description:
- Add a footnote, and save the reference for later use with references
The returned string contains html, meaning that you will have to use!{footnote()}(with a ! instead of a #) in Pug templates.
You can have multiple parallell footnote systems, by specifying a numbers system.
- Add a footnote, and save the reference for later use with references
Examples
// returns '¹'
footnote("Source: The World Health Organization")
PugJS
p Berlin is a big city !{ footnote("Wikipedia") }.
aside Paris is a city in France!{ footnote("CIA", {numberSystem: "roman"}) }.
p Paris is the capital of Germany !{ footnote("Encyclopædia Britannica") }.
// references() will now contain "Wikipedia" and "Encyclopædia Britannica"
// and references({numberSystem: "roman"}) will contain "CIA" references
Parameters:
| Name | Type | Attributes | Description | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
reference |
string |
<optional> |
A text to save for the footnotes | ||||||||||||
opts |
object |
<optional> |
Options
Properties
|
Returns:
A number, linked to the reference
- Type
- string
(static) format(str, …args) → {string}
- Description:
- Utility function for string formatting. This method is primarily intended for usage in other, template level functions.
Example
// returns 'The wheels on the bus go round and round'
format("The {0} on the bus go {1} and {1}", ["wheels", "round"])
// returns 'The wheels on the bus go round and round'
format("The {0} on the bus go {1} and {1}", "wheels", "round")
Parameters:
| Name | Type | Attributes | Description |
|---|---|---|---|
str |
string | Template string | |
args |
* |
<repeatable> |
Values to insert |
Returns:
Formatted string
- Type
- string
(static) list(arr, optsopt) → {string}
- Description:
- Return a conjunctive textual representation of a list
- See:
-
- tList
Examples
sv
// returns 'a, b och c'
list(["a", "b", "c"])
sv
// returns 'mjölk, fil och yogurt; mjöl, kakao och bakpulver och bananer'
list(["mjölk, fil och yogurt", "mjöl, kakao och bakpulver", "bananer"], {level=2})
sv
// returns 'a, b respektive c'
list(["a", "b", "c"], {glue: "respektive"})
en-US
// returns 'a, b, and c'
list(["a", "b", "c"])
en-AU
// returns 'a, b and c'
list(["a", "b", "c"])
ja
// returns 'イ、ロ、ハ'
list(["イ", "ロ", "ハ"])
Parameters:
| Name | Type | Attributes | Description | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
arr |
Array | An array to represent textually | |||||||||||||
opts |
object |
<optional> |
Options
Properties
|
Returns:
A textual representation
- Type
- string
(static) listOr(arr, optsopt) → {string}
- Description:
- Return a disjunctive list (or-list), using ICU patterns http://www.unicode.org/reports/tr35/tr35-general.html#ListPatterns
- See:
-
- tList
Examples
sv
// returns 'a, b eller c'
listOr(["a", "b", "c"])
// returns 'A, B eller för den delen C'
listOr(["A", "B", "C"], {glue: "eller för den delen"})
en-US
// returns 'a, b, or c'
listOr(["a", "b", "c"])
en-AU
// returns 'a, b or c'
listOr(["a", "b", "c"])
ja
// returns 'イ、ロ、またはハ'
listOr(["イ", "ロ", "ハ"])
Parameters:
| Name | Type | Attributes | Description | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
arr |
Array | An array to represent textually | |||||||||||||
opts |
object |
<optional> |
Options
Properties
|
Returns:
A textual representation
- Type
- string
(static) listOrUnique(arr, optsopt) → {string}
- Description:
- Return a disjunctive textual list of unique elements. As we are in an environmant where most vairables are complex objects, depuplication is tricky. This method handles it uniformly for all supported variable types.
- See:
-
- tList
Example
sv
// returns 'a, b och c'
list(["a", "b", "c"])
// returns 'a och c'
list(["a", "a", "c"])
Parameters:
| Name | Type | Attributes | Description | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
arr |
Array | An array to represent textually | |||||||||||||
opts |
object |
<optional> |
Options
Properties
|
Returns:
A textual representation
- Type
- string
(static) listUnique(arr, optsopt) → {string}
- Description:
- Return a conjunctive textual list of unique elements. As we are in an environmant where most vairables are complex objects, depuplication is tricky. This method handles it uniformly for all supported variable types.
- See:
-
- tList
Example
sv
// returns 'a, b och c'
list(["a", "b", "c"])
// returns 'a och c'
list(["a", "a", "c"])
Parameters:
| Name | Type | Attributes | Description | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
arr |
Array | An array to represent textually | |||||||||||||
opts |
object |
<optional> |
Options
Properties
|
Returns:
A textual representation
- Type
- string
(static) lower(text) → {string}
- Description:
- Convert to lower case
Examples
sv
// returns 'iana'
lower("IANA")
tr
// returns 'ıana'
lower("IANA")
Parameters:
| Name | Type | Description |
|---|---|---|
text |
string | Input string |
Returns:
Lower case string
- Type
- string
(static) lowerFirst(text) → {string}
- Description:
- Make the first character lower case, if language has lower case letters, not touching any other letters
Example
sv
// returns 'åklagarkammaren i Jönköping'
lowerFirst("Åklagarkammaren i Jönköping")
Parameters:
| Name | Type | Description |
|---|---|---|
text |
string | Input string |
Returns:
Uncapitalised string
- Type
- string
(static) plural(val, …strings, optsopt) → {string}
- Description:
- Short syntax cordinal plural function, taking one argument for each plural class. For classes, see: http://www.unicode.org/cldr/charts/latest/supplemental/language_plural_rules.html
Examples
en
// returns 'days'
plural(2.5, "day", "days")
uk
// returns 'день'
plural(1, "день", "дні", "днів", "дня")
sv
// returns 'flygplatserna' (with a dictionary installed)
plural(2, "flygplatsen")
// returns 'enhörningarnas' (with a dictionary installed)
plural(12, "enhörningens")
Parameters:
| Name | Type | Attributes | Description | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
val |
number | Array | object | Map | Set | The value used to determine plural. If given an array, we will use the length | |||||||||||
strings |
string |
<repeatable> |
One string for each plural form, or an example string, that will be looked up in a dictionary | ||||||||||
opts |
object |
<optional> |
Options
Properties
|
Returns:
one of the supplied plural forms
- Type
- string
(static) random(…vars) → {string}
- Description:
- Randomly select one of multiple choices
Example
random("ökar stort", "skjuter i höjden", "rusar")
random(["ökar stort", "skjuter i höjden", "rusar"])
Parameters:
| Name | Type | Attributes | Description |
|---|---|---|---|
vars |
* |
<repeatable> |
A list of arguments, or an array |
Returns:
One of the provided strings
- Type
- string
(static) references(optsopt) → {Array.<string>}
- Description:
- Return all footnotes added with footnote() The returned array may be empty, if no footnotes were given.
Example
// returns '¹ Source: The World Health Organization'
// given an earlier footnote("Source: The World Health Organization")
references()[0]
Parameters:
| Name | Type | Attributes | Description | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|
opts |
object |
<optional> |
Options
Properties
|
Returns:
An array of footnotes, linked to the reference
- Type
- Array.<string>
(static) subScript(char) → {string}
- Description:
- Return Unicode subscript version of a character. The method should NOT be used for stylistic purposes, but only when the subscript carries semantic meaning, e.g. in Mathematics and Chemistry: H₂O.
Example
// returns 'H₂O'
"H" + subScript(2) + "O"
Parameters:
| Name | Type | Description |
|---|---|---|
char |
string | A character or string of characters |
Returns:
A subscript equivalent
- Type
- string
(static) superScript(char) → {string}
- Description:
- Return Unicode superscript version of a character. The method should NOT be used for stylistic purposes, but only when the superscript carries semantic meaning, e.g. in Mathematics and Chemistry: Pb⁴⁺ (a +4 charged lead ion).
Example
//returns 'Pb⁴⁺'
"Pb" + superScript("4+")
//returns '⁴'
superScript(4)
Parameters:
| Name | Type | Description |
|---|---|---|
char |
string | A character or string of characters |
Returns:
A superscript equivalent
- Type
- string
(static) t(key, conjugationOrOptionsopt) → {string}
- Description:
- Do string translation.
Note that passing a translation variant (conjugation) as a second argument is deprecated, and will be removed! Until the old behaviour is supported, this method will not work well with plugins, or postprocessing.
- Do string translation.
Examples
en
// returns 'Stockholm municipality'
t("Stockholms kommun")
sv
// returns' Stockholms kommun'
t("Stockholms kommun")
// returns 'Stockholms kommuns'
t("Stockholms kommun", {conjugation: "genitive"})
Parameters:
| Name | Type | Attributes | Description | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|
key |
string | to translate | |||||||||
conjugationOrOptions |
object | string |
<optional> |
Options or DEPRECATED a translation variant
Properties
|
Returns:
A translation, or key if no translation found.
- Type
- string
(static) tList(arr, optsopt) → {string}
- Description:
- Do string translation over an array, and return as a list.
Examples
sv
// returns 'Norge, Sverige och Finland'
tList(["no", "se", "fi"])
en-US
// returns 'Norway, Sweden, and Finland'
tList(["no", "se", "fi"])
Parameters:
| Name | Type | Attributes | Description | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
arr |
Array.<string> | A list of translation strings | |||||||||||||
opts |
object |
<optional> |
Options
Properties
|
Returns:
A formatted list
- Type
- string
(static) tListOr(arr, optsopt) → {string}
- Description:
- Do string translation over an array, and return as a disjunctive list.
Examples
sv
// returns 'Norge, Sverige eller Finland'
tListOr(["no", "se", "fi"])
en-US
// returns 'Norway, Sweden, or Finland'
tListOr(["no", "se", "fi"])
Parameters:
| Name | Type | Attributes | Description | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
arr |
Array.<string> | A list of translation strings | |||||||||||||
opts |
object |
<optional> |
Options
Properties
|
Returns:
A formatted list
- Type
- string
(static) territory(isoCode, optsopt) → {string}
- Description:
- Get a display name for a territory. Will return a customary short name,
that is intented for everyday use and not always the official name,
e.g. Macedonia (not Former Yugoslavian Republic of Macedonia).
The name is returned in a grammatically neutral format, suitable for lists, tables, etc.
The CLDR territory codes used here are similar, but not equal to ISO 3166 codes. While ISO codes are sometimes reused or changed, CLDR codes are not. If no CLDR code is found, we will also check a dictionary of ISO code that are either different from CLDR codes, or deprecated.
- Get a display name for a territory. Will return a customary short name,
that is intented for everyday use and not always the official name,
e.g. Macedonia (not Former Yugoslavian Republic of Macedonia).
Examples
sv
// returns 'Åland'
territory("ax")
fi
// returns 'Ahvenanmaa'
territory("ax")
en
// returns 'Zealand'
territory("dk85")
// returns 'California'
territory("usca")
Parameters:
| Name | Type | Attributes | Description | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|
isoCode |
string | CLDR territory code | |||||||||
opts |
object |
<optional> |
Options
Properties
|
Returns:
territory name
- Type
- string
(static) territoryDistinct(isoCode, optsopt) → {string}
- Description:
- Get a minimal distinct display name for a territory, if available, otherwise falling back to the default. A minimal distinct name depends on context. Setting an explicit context is not yet supported. We'll assume a national level context. (E.g. no need to separate the US state and the country of Georgia).
Example
sv
// returns 'Stockholms län'
territoryDistinct("SE-AB")
// returns 'Värmland'
territoryDistinct("SE-S")
Parameters:
| Name | Type | Attributes | Description | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|
isoCode |
string | CLDR territory code | |||||||||
opts |
object |
<optional> |
Options
Properties
|
Returns:
territory name
- Type
- string
(static) territoryList(territories, optsopt) → {string}
- Description:
- Create a list of territories, avoid repetition of administrative levels, e.g. ”Stockholms, Södermanlands och Uppsala län”.
Example
sv
// returns 'Stockholms, Södermanlands och Uppsala län'
territoryList(["se-01", "se-04", "se-03"])
Parameters:
| Name | Type | Attributes | Description |
|---|---|---|---|
territories |
Array.<string> | A list of territory codes | |
opts |
object |
<optional> |
Options |
Returns:
The list of territories
- Type
- string
(static) territoryListOr(territories, optsopt) → {string}
- Description:
- Create a disjunctive list of territories, avoid repetition of administrative levels, e.g. ”Stockholms, Södermanlands och Uppsala län”.
Example
sv
// returns 'Stockholms, Södermanlands eller Uppsala län'
territoryListOr(["se-01", "se-04", "se-03"])
Parameters:
| Name | Type | Attributes | Description |
|---|---|---|---|
territories |
Array.<string> | A list of territory codes | |
opts |
object |
<optional> |
Options |
Returns:
The list of territories
- Type
- string
(static) territoryListUnique(territories, optsopt) → {string}
- Description:
- Create a unique list of territories, avoid repetition of administrative levels, e.g. ”Stockholms, Södermanlands och Uppsala län”.
Example
sv
// returns 'Stockholms, Södermanlands och Uppsala län'
territoryListUnique(["se-01", "se-04", "se-03", "se-03"])
Parameters:
| Name | Type | Attributes | Description |
|---|---|---|---|
territories |
Array.<string> | A list of territory codes | |
opts |
object |
<optional> |
Options |
Returns:
The list of territories
- Type
- string
(static) territoryListUniqueOr(territories, optsopt) → {string}
- Description:
- Create a unique disjunctive list of territories, avoid repetition of administrative levels, e.g. ”Stockholms, Södermanlands och Uppsala län”.
Example
sv
// returns 'Stockholms, Södermanlands ekker Uppsala län'
territoryListUniqueOr(["se-01", "se-04", "se-03", "se-03"])
Parameters:
| Name | Type | Attributes | Description |
|---|---|---|---|
territories |
Array.<string> | A list of territory codes | |
opts |
object |
<optional> |
Options |
Returns:
The list of territories
- Type
- string
(static) territoryShort(isoCode, optsopt) → {string}
- Description:
- Get a short display name for a territory, if available, otherwise falling back to the default.
Examples
sv
// returns 'Stockholm'
territoryShort("SE-AB")
// returns 'Falun'
territoryShort("SE-2080")
// returns 'i Falun'
territoryShort("SE-2080", "locative")
// returns 'Falu'
territoryShort("SE-2080", "compound")
// returns 'Faluns'
territoryShort("SE-2080", "genitive")
fi
// returns 'Ahvenanmaa'
territoryShort("ax")
Parameters:
| Name | Type | Attributes | Description | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|
isoCode |
string | CLDR territory code | |||||||||
opts |
object |
<optional> |
Options
Properties
|
Returns:
territory name
- Type
- string
(static) territoryType(isoCode) → {string}
- Description:
- Get a string describing the administative level of a territory, using the most specific term
Examples
sv
// returns 'län'
territoryType("SE-AB")
ja
// returns '県'
territoryTypeGeneric("jp-19")
Parameters:
| Name | Type | Description |
|---|---|---|
isoCode |
string | CLDR territory code |
Returns:
territory name
- Type
- string
(static) territoryTypeGeneric(isoCode) → {string}
- Description:
- Get a string describing the administative level of a territory, using the most generic term
Examples
sv
// returns 'län'
territoryType("SE-AB")
ja
// returns '都道府県'
territoryTypeGeneric("jp-19")
Parameters:
| Name | Type | Description |
|---|---|---|
isoCode |
string | CLDR territory code |
Returns:
territory name
- Type
- string
(static) territoryTypeSpecific()
- Description:
- Alias for territoryType
(static) text(str, optionsopt) → {string}
- Description:
- Make sure text from external sources are HTML compliant
Example
// returns 'T&T'
text("T&T")
Parameters:
| Name | Type | Attributes | Description | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|
str |
string | A text string | |||||||||
options |
object |
<optional> |
Options
Properties
|
Returns:
A html compliant text string
- Type
- string
(static) title(text) → {string}
- Description:
- Make the first letter of each word upper case (Title Case).
Please note that title casing, as used in e.g. Enlish titles and headlines, follows different (and often complex) rules in different style guides. This method simply turns the first letter if each word into uppercase. This function uses the (ES) regular expression /[\s-]/ to find word boundaries. This is not guaranteed to work in all script systems. It should, however, work as intended for most scripts (e.g. Latin) that have a concept of upper and lower case.
- Make the first letter of each word upper case (Title Case).
Examples
sv
// returns 'Ikea Holdings'
title("IKEA Holdings")
tr
// returns 'İstanbul Airport'
title("istanbul airPORT")
ja
// returns '血液型はa型である'
title("血液型はa型である")
Parameters:
| Name | Type | Description |
|---|---|---|
text |
string | Input string |
Returns:
Title cased string
- Type
- string
(static) upper(text) → {string}
- Description:
- Convert to upper case
Examples
sv
// returns 'IANA'
upper("iana")
tr
// returns 'İANA'
upper("iana")
ja
// returns '血液型はA型である'
upper("血液型はa型である")
Parameters:
| Name | Type | Description |
|---|---|---|
text |
string | Input string |
Returns:
Upper case string
- Type
- string
(static) upperFirst(text) → {string}
- Description:
- Make the first character upper case, if language has uppercase letters, not touching any other letters
Example
sv
// returns 'Istanbul var Konstantinopel'
upperFirst("istanbul var Konstantinopel")
Parameters:
| Name | Type | Description |
|---|---|---|
text |
string | Input string |
Returns:
Capitalised string
- Type
- string
(static) void(input, optionsopt) → {*}
- Description:
- Doesa nothing
Example
// returns 'tåg 21'
void("tåg 21", {break: "neverBreak"})
Parameters:
| Name | Type | Attributes | Description |
|---|---|---|---|
input |
* | Anything | |
options |
object |
<optional> |
Options |
Returns:
The input, as returned by any pre- or postprocessors, core or plugin
- Type
- *