Skip to content

Commit

Permalink
download/sync: introduce --languages lng1,lng2 argument (#96)
Browse files Browse the repository at this point in the history
  • Loading branch information
adrai committed Sep 19, 2024
1 parent bcb5c02 commit 71af654
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 5 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ All notable changes to this project will be documented in this file.
Project versioning adheres to [Semantic Versioning](http://semver.org/).
Change log format is based on [Keep a Changelog](http://keepachangelog.com/).

## [8.1.0](https://github.com/locize/locize-cli/compare/v8.0.2...v8.1.0) - 2024-09-19

- introduce `--languages lng1,lng2` argument (#96)

## [8.0.2](https://github.com/locize/locize-cli/compare/v8.0.1...v8.0.2) - 2024-08-14

- update some dependencies to address #95 and #94
Expand Down
6 changes: 6 additions & 0 deletions bin/locize
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ program
.option('-i, --project-id <projectId>', 'The project-id that should be used')
.option('-v, --ver <version>', 'The version that should be targeted (default: latest)')
.option('-l, --language <lng>', 'The language that should be targeted')
.option('-ls, --languages <lng1,lng2>', 'The languages that should be targeted')
.option('-n, --namespace <ns>', 'The namespace that should be targeted')
.option('-p, --path <path>', `Specify the path that should be used (default: ${process.cwd()})`, process.cwd())
.option('-g, --get-path <url>', `Specify the get-path url that should be used (default: ${getPathUrl})`)
Expand All @@ -253,6 +254,7 @@ program
const apiKey = options.apiKey || config.apiKey || process.env.LOCIZE_API_KEY || process.env.LOCIZE_KEY;

const language = options.language || config.language || config.lng || process.env.LOCIZE_LANGUAGE || process.env.LOCIZE_LANG || process.env.LOCIZE_LNG;
const languages = options.languages || config.languages || config.lngs || process.env.LOCIZE_LANGUAGES || process.env.LOCIZE_LANGS || process.env.LOCIZE_LNGS;

const version = options.ver || config.ver || config.version || process.env.LOCIZE_VERSION || process.env.LOCIZE_VER || 'latest';

Expand All @@ -277,6 +279,7 @@ program
projectId: projectId,
apiPath: url.parse(getPath).protocol + '//' + url.parse(getPath).host,
language: language,
languages: languages && languages.split(','),
version: version,
namespace: namespace,
path: options.path,
Expand Down Expand Up @@ -370,6 +373,7 @@ program
.option('-R, --reference-language-only <true|false>', 'Check for changes in reference language only. (default: true)', 'true')
.option('-t, --compare-modification-time <true|false>', 'while comparing the namespace content between local and remote, take the modification time of the local file and the last modified time of the remote namespace into account. (default: false)', 'false')
.option('-l, --language <lng>', 'The language that should be targeted')
.option('-ls, --languages <lng1,lng2>', 'The languages that should be targeted')
.option('-n, --namespace <ns>', 'The namespace that should be targeted')
.option('-g, --get-path <url>', `Specify the get-path url that should be used (default: ${getPathUrl})`)
.option('-up, --unpublished <true|false>', 'Downloads the current (unpublished) translations. This will generate private download costs (default: false)', 'false')
Expand Down Expand Up @@ -398,6 +402,7 @@ program
const version = options.ver || config.ver || config.version || process.env.LOCIZE_VERSION || process.env.LOCIZE_VER || 'latest';

const language = options.language || config.language || config.lng || process.env.LOCIZE_LANGUAGE || process.env.LOCIZE_LANG || process.env.LOCIZE_LNG;
const languages = options.languages || config.languages || config.lngs || process.env.LOCIZE_LANGUAGES || process.env.LOCIZE_LANGS || process.env.LOCIZE_LNGS;

const namespace = options.namespace;

Expand Down Expand Up @@ -436,6 +441,7 @@ program
referenceLanguageOnly: referenceLanguageOnly,
compareModificationTime: compareModificationTime,
language: language,
languages: languages && languages.split(','),
namespace: namespace,
dry: dry,
pathMask: pathMask,
Expand Down
20 changes: 17 additions & 3 deletions download.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,22 @@ const handleError = (err, cb) => {
if (cb) cb(err);
};

const filterDownloadsLanguages = (opt, downloads) => {
if (opt.languages) {
downloads = downloads.filter((d) => {
const splitted = d.key.split('/');
// const p = splitted[d.isPrivate ? 1 : 0];
// const v = splitted[d.isPrivate ? 2 : 1];
const l = splitted[d.isPrivate ? 3 : 2];
const n = splitted[d.isPrivate ? 4 : 3];
return opt.languages.indexOf(l) > -1 && (!opt.namespace || opt.namespace === n);
});
}
return downloads;
};

const filterDownloads = (opt, downloads) => {
if (opt.skipEmpty) return downloads.filter((d) => d.size > 2);
if (opt.skipEmpty) return filterDownloadsLanguages(opt, downloads.filter((d) => d.size > 2));
if (downloads.length < 1) return downloads;

const allNamespaces = [];
Expand Down Expand Up @@ -185,7 +199,7 @@ const filterDownloads = (opt, downloads) => {
});
});
});
return downloads;
return filterDownloadsLanguages(opt, downloads);
};

const download = (opt, cb) => {
Expand Down Expand Up @@ -216,7 +230,7 @@ const download = (opt, cb) => {

if (opt.version) {
url += '/' + opt.version;
if (opt.language) {
if (!opt.languages && opt.language) {
url += '/' + opt.language;
if (opt.namespace) {
url += '/' + opt.namespace;
Expand Down
9 changes: 7 additions & 2 deletions sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ const getDownloads = (opt, cb) => {
if (!res || !res[opt.version]) return handleError(new Error('Nothing found!'), cb);

const toDownload = [];
const lngsToCheck = opt.language ? [opt.language] : Object.keys(res[opt.version]);
const lngsToCheck = opt.language ? [opt.language] : (opt.languages && opt.languages.length > 0) ? opt.languages : Object.keys(res[opt.version]);
lngsToCheck.forEach((l) => {
if (opt.namespaces) {
opt.namespaces.forEach((n) => {
Expand Down Expand Up @@ -212,6 +212,7 @@ const downloadAll = (opt, remoteLanguages, omitRef, manipulate, cb) => {
opt.isPrivate = download.isPrivate;

if (opt.language && opt.language !== lng && lng !== opt.referenceLanguage) return clb(null);
if (opt.languages && opt.languages.length > 0 && opt.languages.indexOf(lng) < 0 && lng !== opt.referenceLanguage) return clb(null);
if (opt.namespace && opt.namespace !== namespace) return clb(null);
if (opt.namespaces && opt.namespaces.length > 0 && opt.namespaces.indexOf(namespace) < 0) return clb(null);

Expand Down Expand Up @@ -317,7 +318,7 @@ const update = (opt, lng, ns, shouldOmit, cb) => {
const cleanupLanguages = (opt, remoteLanguages) => {
if (opt.pathMask.lastIndexOf(path.sep) < 0) return;
const dirs = getDirectories(opt.path).filter((dir) => dir.indexOf('.') !== 0);
if (!opt.language && !opt.namespace && !opt.namespaces) {
if (!opt.language && (!opt.languages || opt.languages.length === 0) && !opt.namespace && !opt.namespaces) {
dirs
.filter((lng) => {
const lMask = `${opt.pathMaskInterpolationPrefix}language${opt.pathMaskInterpolationSuffix}`;
Expand All @@ -339,6 +340,7 @@ const cleanupLanguages = (opt, remoteLanguages) => {
}
remoteLanguages.forEach((lng) => {
if (opt.language && opt.language !== lng) return;
if (opt.languages && opt.languages.length > 0 && opt.languages.indexOf(lng) < 0) return;
const filledLngMask = opt.pathMask.replace(`${opt.pathMaskInterpolationPrefix}language${opt.pathMaskInterpolationSuffix}`, lng);
var lngPath;
if (filledLngMask.lastIndexOf(path.sep) > 0) {
Expand Down Expand Up @@ -583,6 +585,9 @@ const sync = (opt, cb) => {
if (opt.referenceLanguageOnly && opt.language && opt.referenceLanguage !== opt.language) {
opt.referenceLanguage = opt.language;
}
if (opt.referenceLanguageOnly && !opt.language && opt.languages && opt.languages.length > 0 && opt.languages.indexOf(opt.referenceLanguage) < 0) {
opt.referenceLanguage = opt.languages[0];
}

if (opt.referenceLanguageOnly) {
console.log(colors.grey(`checking local${opt.path !== process.cwd() ? ` (${opt.path})` : ''} only reference language...`));
Expand Down

0 comments on commit 71af654

Please sign in to comment.