Skip to content

Instantly share code, notes, and snippets.

@cdrini
Last active April 26, 2022 01:42
Show Gist options
  • Save cdrini/615d75653e1e47115930fa394e83ab17 to your computer and use it in GitHub Desktop.
Save cdrini/615d75653e1e47115930fa394e83ab17 to your computer and use it in GitHub Desktop.
Convert OL page to i18n page

Script to convert a normal /type/page to a /type/i18n_page. Assumes the current page is in English, and creates the .en page with the same body.

~80% tested. I used it recently but then made some changes. You should be able to copy and paste it into your terminal, or it'll probably also work as a bookmarklet:

Get the Bookmarklet

javascript:(async function() {
function copy_json(json) {
return JSON.parse(JSON.stringify(json));
}
function convert_page_to_i18n_page(page) {
const copy = copy_json(page);
copy.type = { key: '/type/i18n_page' };
delete copy.body;
return copy;
}
/**
* @param {string} key The key of the source page to become an i18n_page
*/
async function make_i18n_page(key) {
const data = await fetch(`${key}.json`).then(r => r.json());
if (data.type.key != '/type/page') {
throw new Error(`${key} is not a page; maybe already done?`);
}
if ('.' in data.key) {
throw new Error(`${key} contains a '.', is it already an i18n page?`);
}
/* Assume source in English */
await fetch(`${key}.en.json`, {
method: 'PUT',
body: JSON.stringify({
...copy_json(data),
key: `${key}.en`,
_comment: 'Convert to i18n page',
}),
});
await fetch(`${key}.json`, {
method: 'PUT',
body: JSON.stringify({
...convert_page_to_i18n_page(data),
_comment: 'Convert to i18n page',
}),
});
}
if (!confirm(`Convert '${location.pathname}' to an i18n page?`)) {
return;
}
try {
await make_i18n_page(location.pathname);
alert('Done!');
} catch (e) {
alert(`Error: ${e.message}`);
throw e;
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment