Skip to content

Commit

Permalink
feat(i18n) Custom i18n strings (#4000)
Browse files Browse the repository at this point in the history
* Custom i18n strings (and some code formatting)

* Documentation for per-instance l10n overwrites
  • Loading branch information
orblivion committed May 19, 2020
1 parent 170f471 commit 61c7bb9
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 6 deletions.
20 changes: 20 additions & 0 deletions doc/localization.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,23 @@ For example, if you want to replace `Chat` with `Notes`, simply add...
, "pad.chat": "Notes"
}
```

## Customization for Administrators

As an Etherpad administrator, it is possible to overwrite core mesages as well as messages in plugins. These include error messages, labels, and user instructions. Whereas the localization in the source code is in separate files separated by locale, an administrator's custom localizations are in `settings.json` under the `customLocaleStrigns` key, with each locale separated by a sub-key underneath.

For example, let's say you want to change the text on the "New Pad" button on Etherpad's home page. If you look in `locales/en.json` (or `locales/en-gb.json`) you'll see the key for this text is `"index.newPad"`. You could add the following to `settings.json`:

```
"customLocaleStrings": {
"fr": {
"index.newPad": "Créer un document"
},
"en-gb": {
"index.newPad": "Create a document"
},
"en": {
"index.newPad": "Create a document"
}
}
```
5 changes: 4 additions & 1 deletion settings.json.docker
Original file line number Diff line number Diff line change
Expand Up @@ -551,5 +551,8 @@
*/

]
} // logconfig
}, // logconfig

/* Override any strings found in locale directories */
"customLocaleStrings": {}
}
5 changes: 4 additions & 1 deletion settings.json.template
Original file line number Diff line number Diff line change
Expand Up @@ -556,5 +556,8 @@
*/

]
} // logconfig
}, // logconfig

/* Override any strings found in locale directories */
"customLocaleStrings": {}
}
4 changes: 2 additions & 2 deletions src/node/hooks/express/adminsettings.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ exports.socketio = function (hook_name, args, cb) {
return console.log(err);
}

//if showSettingsInAdminPage is set to false, then return NOT_ALLOWED in the result
// if showSettingsInAdminPage is set to false, then return NOT_ALLOWED in the result
if(settings.showSettingsInAdminPage === false) {
socket.emit("settings", {results:'NOT_ALLOWED'});
socket.emit("settings", {results: 'NOT_ALLOWED'});
}
else {
socket.emit("settings", {results: data});
Expand Down
19 changes: 18 additions & 1 deletion src/node/hooks/i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ var languages = require('languages4translatewiki')
, plugins = require('ep_etherpad-lite/static/js/pluginfw/plugins.js').plugins
, semver = require('semver')
, existsSync = require('../utils/path_exists')
, settings = require('../utils/Settings');
;


Expand Down Expand Up @@ -43,7 +44,7 @@ function getAllLocales() {
//add plugins languages (if any)
for(var pluginName in plugins) extractLangs(path.join(npm.root, pluginName, 'locales'));

// Build a locale index (merge all locale data)
// Build a locale index (merge all locale data other than user-supplied overrides)
var locales = {}
_.each (locales2paths, function(files, langcode) {
locales[langcode]={};
Expand All @@ -54,6 +55,22 @@ function getAllLocales() {
});
});

// Add custom strings from settings.json
// Since this is user-supplied, we'll do some extra sanity checks
const wrongFormatErr = Error(
"customLocaleStrings in wrong format. See documentation " +
"for Customization for Administrators, under Localization.")
if (settings.customLocaleStrings) {
if (typeof settings.customLocaleStrings !== "object") throw wrongFormatErr
_.each(settings.customLocaleStrings, function(overrides, langcode) {
if (typeof overrides !== "object") throw wrongFormatErr
_.each(overrides, function(localeString, key) {
if (typeof localeString !== "string") throw wrongFormatErr
locales[langcode][key] = localeString
})
})
}

return locales;
}

Expand Down
5 changes: 5 additions & 0 deletions src/node/utils/Settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,11 @@ exports.scrollWhenFocusLineIsOutOfViewport = {
*/
exports.exposeVersion = false;

/*
* Override any strings found in locale directories
*/
exports.customLocaleStrings = {};

/*
* From Etherpad 1.8.3 onwards, import and export of pads is always rate
* limited.
Expand Down
2 changes: 1 addition & 1 deletion src/templates/admin/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<div class="menu">
<h1><a href="../">Etherpad</a></h1>
<ul>
<% e.begin_block("adminMenu"); %>
<% e.begin_block("adminMenu"); %>
<li><a href="plugins">Plugin manager</a> </li>
<li><a href="settings">Settings</a> </li>
<li><a href="plugins/info">Troubleshooting information</a> </li>
Expand Down

0 comments on commit 61c7bb9

Please sign in to comment.