Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

i18n: adds translation support #307

Merged
merged 1 commit into from
Jul 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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