-
Notifications
You must be signed in to change notification settings - Fork 18
Widgets Language v7
- status: complete
- version: 7.x
- follows from: Create a New Widget
All the text displayed to the user should be parametrized: instead of hardcoding it, make use of the texts
object to store all your text strings.
This has the key advantage of enabling the easy replacing of any text with another, without changing the code of the widget.
If you are creating a new widget, define a static texts
variable with all the display strings:
/** ### NewWidget.texts
*
* Default texts to display
*/
NewWidget.texts = {
// Variables in here contains display text.
welcomeMessage: 'Hello!',
// HTML is supported.
disconnect: '<span style="color: red">You have been ' +
'<strong>disconnected</strong>.</span><br>',
// Parametric text is supported.
gameMode: function(widget, param) {
if (param === 'PRACTICE') {
return 'You are in practice mode.';
}
return 'This is the real game.';
},
// More text variables can be added as needed.
};
Use the getText
method to access the requested text when your widgets needs it. For instance:
// Inside a method of the NewWidget class.
this.getText('welcomeMessage'); // 'Hello!'
this.getText('disconnect'); // '<span style="color: red">You have been ' +
// '<strong>disconnected</strong>.</span><br>'
this.getText('gameMode', 'PRACTICE'); // 'You are in practice mode.'
If you need to change the value of any default text, you can use the setText
method or initialize the widget with a custom texts
property. For instance, this translates your widget in Spanish:
node.widget.append('NewWidget', root, {
texts: {
welcomeMessage: 'Hola!',
disconnect: '<span style="color: red">Usted esta ' +
'<strong>desconnectado</strong>.</span><br>',
gameMode: function(widget, param) {
if (param === 'PRACTICE') {
return 'Usted esta en practice mode.';
}
return 'Ya empezamos!';
}
}
})
Go back to the wiki Home.
Copyright (C) 2021 Stefano Balietti
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.