Translation Vipers Example

In this example, a word is added to a sentence and a word count is performed.

Smartsite SXML CopyCode image Copy Code
<se:pagetranslations id="helpers" rem="helper functions implemented as page translations">
    <se:collection>
        <se:member name="addword(str,word)" rem="add a word to an existing sentence">
            {translation.setresult(
                string.trim(translation.arg(str)) + ' ' + string.trim(translation.arg(word))
            )}
        </se:member>
        <se:member name="countwords(str)" rem="count the number of words in a string">
            {buffer.set(res,
                string.replace(translation.arg(1),'\w+','x',UseRegExp)
            )}
            {translation.setresult(
                string.length( string.removewhitespace($res) )
            )}
        </se:member>
    </se:collection>
</se:pagetranslations>

{buffer.set(sentence,'The lazy dog jumps over de crazy')}

Source text: {buffer.get(sentence)}

After append: {translation.helpers.addword($sentence, 'dog')}

Word count: {translation.helpers.countwords($sentence)}
Example Result CopyCode image Copy Code
Source text: The lazy dog jumps over de crazy

After append: The lazy dog jumps over de crazy dog

Word count: 7