Skip to content

Commit

Permalink
i18n: adds translation support
Browse files Browse the repository at this point in the history
  • Loading branch information
mb-wali committed Jul 5, 2021
1 parent 57cc8f3 commit 5a52647
Show file tree
Hide file tree
Showing 17 changed files with 1,975 additions and 1,012 deletions.
32 changes: 32 additions & 0 deletions .tx/config
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# -*- coding: utf-8 -*-
#
# Copyright (C) 2021 CERN.
# Copyright (C) 2021 Graz University of Technology.
#
# React-Invenio-Deposit is free software; you can redistribute it and/or modify it
# under the terms of the MIT License; see LICENSE file for more details.

#
# 1) Extract translation keys/values,
# $ npm run i18n-scan
# 2) Update .pot file, this file is pushed to transifex
# $ npm run i18n-conv-json
# 3) Convert/update .json from .po
# $ npm run i18n-conv-po-all
# 4) Install the transifex-client
# $ pip install transifex-client
# 5) Push source (.pot) and translations (.po) to Transifex
# $ tx push -s -t
# 6) Pull translations for a single language from Transifex
# $ tx pull -l <lang>
# 7) Pull translations for all languages from Transifex
# $ tx pull -a

[main]
host = https://www.transifex.com

[invenio.react-invenio-deposit-messages]
file_filter = src/lib/translations/<lang>/messages.po
source_file = src/lib/translations/translations.pot
source_lang = en
type = PO
24 changes: 24 additions & 0 deletions getTextConverter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// This file is part of React-Invenio-Deposit
// Copyright (C) 2021 Graz University of Technology.
//
// React-Invenio-Deposit is free software; you can redistribute it and/or modify it
// under the terms of the MIT License; see LICENSE file for more details.

const { readFileSync, writeFileSync } = require('fs');
const { gettextToI18next } = require('i18next-conv');
const { languages } = require('./package').config;

// it accepts the same options as the cli.
// https://github.com/i18next/i18next-gettext-converter#options
const options = {/* you options here */}

function save(target) {
return result => {
writeFileSync(target, result);
};
}

for (lang of languages) {
gettextToI18next(lang, readFileSync(`src/lib/translations/${lang}/messages.po`), options)
.then(save(`src/lib/translations/${lang}/translations.json`));
}
69 changes: 69 additions & 0 deletions i18next-scanner.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// This file is part of React-Invenio-Deposit
// Copyright (C) 2021 Graz University of Technology.
//
// React-Invenio-Deposit is free software; you can redistribute it and/or modify it
// under the terms of the MIT License; see LICENSE file for more details.

const fs = require('fs');
const chalk = require('chalk');
const { languages } = require('./package').config;

// list of func used to
// mark the strings for translation
const funcList = ['i18next.t', 'i18n.t', 't'];

module.exports = {
options: {
debug: true,
browserLanguageDetection: true,
func: {
list: funcList,
extensions: ['.js', '.jsx']
},
trans: false, // Enable for using Trans component
lngs: languages,
ns: [
// file name (.json)
'translations',
],
defaultLng: 'en',
defaultNs: 'translations',
resource: {
// The path where resources get loaded from. Relative to current working directory.
loadPath: 'src/lib/translations/{{lng}}/{{ns}}.json',

// The path to store resources.
savePath: 'src/lib/translations/{{lng}}/{{ns}}.json',

jsonIndent: 2,
lineEnding: '\n'
},
nsSeparator: false, // namespace separator

//Set to false to disable key separator
// if you prefer having keys as the fallback for translation (e.g. gettext).
keySeparator: false,
},

// func is provided
// (if needed)for more custom extractions.
transform: function customTransform(file, enc, done) {
"use strict";
const parser = this.parser;
const content = fs.readFileSync(file.path, enc);
let count = 0;

parser.parseFuncFromString(content, { list: funcList }, (key, options) => {
parser.set(key, Object.assign({}, options, {
nsSeparator: false,
keySeparator: false
}));
++count;
});

if (count > 0) {
console.log(`i18next-scanner: count=${chalk.cyan(count)}, file=${chalk.yellow(JSON.stringify(file.relative))}`);
}
done();
}
};
Loading

0 comments on commit 5a52647

Please sign in to comment.