Skip to content

Commit

Permalink
Fixed #516.
Browse files Browse the repository at this point in the history
y18n language fallback does not work. In fact, the fallback code in commit
yargs/y18n@bf1ef9b
seems to have been deleted from the y18n repo.

Calling setLocale() also does not load the proper locale file, as advertised in
yargs/y18n#60.

So we're rolling our own.
  • Loading branch information
diamondap committed Apr 13, 2022
1 parent de1283b commit 0d78442
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 17 deletions.
37 changes: 36 additions & 1 deletion core/context.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
const { Config } = require('./config');
const fs = require('fs');
const { JsonStore } = require('./json_store');
const logger = require('../util/logger.js');
const os = require('os');
const osLocale = require('os-locale');
const path = require('path');
const { fstat } = require('fs');

const y18n = require('y18n')({
directory: path.join(__dirname, '..', 'locales'),
locale: osLocale.sync()
locale: getLocale(),
fallbackToLanguage: true,
updateFiles: false,
});

/**
Expand Down Expand Up @@ -183,6 +187,37 @@ class GlobalContext {
}
}

/**
* getLocale returns the user's current locale, or "en-US" if DART has
* no matching file for the user's actual locale.
*
* This fixes bug https:* github.com/APTrust/dart/issues/516
*
* This is actually a bug in y18n, which does not correctly fall back
* to language-only files. Creating file locales/en.json should cause
* any English locale to fall back to en.json if it doesn't have a specific
* territory file like en-IE or en-GB. However, in testing, fallback never
* works. Note that the os-locale package normalizes all locales to use
* dashes instead of underscores. So if system says locale is en_NZ, os-locale
* reports en-NZ.
*
* Anyhoo, since resetting the locale using y18n.setLocale()
* does not load the specified locale file as expected, we have to
* set this right from the get-go.
*
* When DART gets non-English translation files, we'll have to revisit this.
*
* @returns {string}
*/
function getLocale() {
let locale = osLocale.sync()
let localeFile = path.join(__dirname, '..', 'locales', locale + ".json")
if (!fs.existsSync(localeFile)) {
return "en-US"
}
return locale
}

const Context = new GlobalContext();
Object.freeze(Context);

Expand Down
16 changes: 0 additions & 16 deletions locales/en-US.json
Original file line number Diff line number Diff line change
Expand Up @@ -406,22 +406,6 @@
"Job_byteCount_help": "Job_byteCount_help",
"Job_workflowId_label": "Job_workflowId_label",
"Job_workflowId_help": "Job_workflowId_help",
"Job_fe079b4d-b897-42f1-8ee0-625f8b8be959_label": "Job_fe079b4d-b897-42f1-8ee0-625f8b8be959_label",
"Job_fe079b4d-b897-42f1-8ee0-625f8b8be959_help": "Job_fe079b4d-b897-42f1-8ee0-625f8b8be959_help",
"Job_5cfbf976-d23b-436e-833b-a7883ea479eb_label": "Job_5cfbf976-d23b-436e-833b-a7883ea479eb_label",
"Job_5cfbf976-d23b-436e-833b-a7883ea479eb_help": "Job_5cfbf976-d23b-436e-833b-a7883ea479eb_help",
"Job_4c281b30-a685-43f5-aa35-b5799144764b_label": "Job_4c281b30-a685-43f5-aa35-b5799144764b_label",
"Job_4c281b30-a685-43f5-aa35-b5799144764b_help": "Job_4c281b30-a685-43f5-aa35-b5799144764b_help",
"Job_8168a26b-8d38-4da3-a356-f0704313cef7_label": "Job_8168a26b-8d38-4da3-a356-f0704313cef7_label",
"Job_8168a26b-8d38-4da3-a356-f0704313cef7_help": "Job_8168a26b-8d38-4da3-a356-f0704313cef7_help",
"Job_66d19950-31fd-4f98-ae77-abe46d61e88d_label": "Job_66d19950-31fd-4f98-ae77-abe46d61e88d_label",
"Job_66d19950-31fd-4f98-ae77-abe46d61e88d_help": "Job_66d19950-31fd-4f98-ae77-abe46d61e88d_help",
"Job_0d276262-105b-4e36-83aa-bff05c03ac8b_label": "Job_0d276262-105b-4e36-83aa-bff05c03ac8b_label",
"Job_0d276262-105b-4e36-83aa-bff05c03ac8b_help": "Job_0d276262-105b-4e36-83aa-bff05c03ac8b_help",
"Job_956e8aa0-6d41-42db-9384-b50e83748eef_label": "Job_956e8aa0-6d41-42db-9384-b50e83748eef_label",
"Job_956e8aa0-6d41-42db-9384-b50e83748eef_help": "Job_956e8aa0-6d41-42db-9384-b50e83748eef_help",
"Job_4dfb0bfb-25c6-4dca-a468-301ecc35262b_label": "Job_4dfb0bfb-25c6-4dca-a468-301ecc35262b_label",
"Job_4dfb0bfb-25c6-4dca-a468-301ecc35262b_help": "Job_4dfb0bfb-25c6-4dca-a468-301ecc35262b_help",
"JobValidationOp_bagItProfileId_help": "JobValidationOp_bagItProfileId_help",
"Choose a profile against which to validate this bag. If you just want to validate against the BagIt specification, choose the Empty Profile.": "Choose a profile against which to validate this bag. If you just want to validate against the BagIt specification, choose the Empty Profile.",
"The bag is not valid according to the selected profile.": "The bag is not valid according to the selected profile.",
Expand Down
4 changes: 4 additions & 0 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const { JobLoader } = require('./util/job_loader');
const { JobRunner } = require('./workers/job_runner');
const minimist = require('minimist');
const process = require('process');
const osLocale = require('os-locale');

// Electron wants these vars to be global, so we defined them here.
// They will be assigned only if we're running in GUI mode.
Expand Down Expand Up @@ -86,6 +87,9 @@ function makey18nWriteSafe() {
Context.y18n.updateFiles = (process.DART_MODE == 'gui' && Context.isElectronDevMode());
Context.logger.info(Context.dartVersion());
Context.logger.info("Y18 updateFiles = " + Context.y18n.updateFiles);
if (Context.y18n.getLocale() != osLocale.sync()) {
Context.logger.info(`DART could not find a suitable translation file for locale ${osLocale.sync()} so it's currently using ${Context.y18n.getLocale()}`)
}
}

// The canonical way to read from STDIN in Node is:
Expand Down

0 comments on commit 0d78442

Please sign in to comment.