Skip to content

Instantly share code, notes, and snippets.

@greyelf
Forked from tmedwards/sc-i18n-example.twee
Last active March 26, 2018 21:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save greyelf/d92a355e3f1f83bb88bb141beb34e23c to your computer and use it in GitHub Desktop.
Save greyelf/d92a355e3f1f83bb88bb141beb34e23c to your computer and use it in GitHub Desktop.
SugarCube v2 basic internationalization (i18n) example (in Twee notation)
:: StoryTitle
SugarCube i18n example
:: StoryAuthor
Original by TMEdwards
Modified by Greyelf
:: Language Switching [script]
/***********************************************************
Set up a `i18n` object on SugarCube's `setup` object.
***********************************************************/
setup.i18n = {
/*
Map of language labels to codes for all supported
languages go here.
*/
langs : {
// NOTE: User customization required here.
'Deutsche' : 'de',
'English' : 'en',
'Français' : 'fr',
},
/*
Utility code. You probably do not need to worry
about any of these.
*/
codes : function () {
return Object.keys(this.langs).map(function (label) {
return this.langs[label];
}, this);
},
labels : function () {
return Object.keys(this.langs);
},
labelFromCode : function (code) {
var label = Object.keys(this.langs).find(function (label) {
return this.langs[label] === code;
}, this);
if (!label) {
throw new Error('unknown language code "' + code + '"');
}
return label;
},
default : 'English'
};
/***********************************************************
Language switching setting.
***********************************************************/
function initLanguage() {
/*
Set the `l10nStrings` properties to the appropriate
values for each supported language.
English need not receive a case, unless you simply
want to alter the default values, as it is the
default language.
*/
switch (setup.i18n.langs[settings.lang]) {
// NOTE: User customization required here.
case 'de':
l10nStrings.settingsTitle = 'Einstellungen';
l10nStrings.settingsOff = 'Deaktivieren';
l10nStrings.settingsOn = 'Aktivieren';
l10nStrings.settingsReset = 'Auf Standardeinstellung zurücksetzen';
break;
case 'fr':
l10nStrings.settingsTitle = 'Paramètres';
l10nStrings.settingsOff = 'Désactiver';
l10nStrings.settingsOn = 'Activer';
l10nStrings.settingsReset = 'Réinitialiser les paramètres par défaut';
break;
}
/*
Set the `lang` attribute on the document element to
the appropriate value. This is mostly notational,
though it could also be used to enable localization
specific styling.
*/
$('html').attr('lang', setup.i18n.langs[settings.lang]);
}
function changeLanguage() {
/*
Reload the application to ensure that the proper
localizations are loaded.
*/
window.location.reload();
}
Setting.addList('lang', {
label : 'Language.',
list : setup.i18n.labels(),
default : setup.i18n.default,
onInit : initLanguage,
onChange : changeLanguage
});
/***********************************************************
Set up a `postrender` task which renders the appropriate
language code suffixed passage into each non-suffixed
passage whenever current language isn't the default.
***********************************************************/
postrender['i18n-passage-include'] = function (content) {
if (settings.lang !== setup.i18n.default) {
var passage = State.passage + '_' + setup.i18n.langs[settings.lang];
$(content).empty().wiki(Story.get(passage).processText());
}
};
:: Start
The English start of the story.
[[Next|Next]]
/% NOTE:
When creating links between passages, you must always link to
the non-suffixed versions and never to the suffixed versions.
The postrender task will handle displaying the correct version
based on the currently selected language.
%/
*/
:: Start_de
Der deutsche Anfang der Geschichte.
[[Nächster|Next]]
:: Start_fr
Le début français de l'histoire.
[[Prochain|Next]]
:: Next
The Next passage.
:: Next_de
Die nächste Passage.
:: Next_fr
Le prochain passage.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment