-
-
Notifications
You must be signed in to change notification settings - Fork 419
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
Can use different files for the same language? #423
Comments
Any update on this? |
loading from multiple sources is not (yet) supported |
I had the same requirement. function addLocaleFile(locale, file) {
// get i18n catalog instance
let catalog = i18n.getCatalog();
if (!catalog) {
return;
}
// load file
let localeFile = fs.readFileSync(file);
let newLocale = JSON.parse(localeFile);
// merge existing with new locales
catalog[locale] = catalog[locale] ? {...catalog[locale], ...newLocale } : newLocale;
}; Make sure you call i18n.configure with your base locales first. addLocaleFile('es', './locales/es-some-type-of-content.json');
addLocaleFile('es', './locales/es-other-type-of-content.json'); So I used this to merge a hierarchy of locales |
besides the solution mentioned by @Khaos66 earlier you might take a look at https://github.com/mashpie/i18n-node#some-words-on-staticcatalog-option or https://github.com/mashpie/i18n-node#some-words-on-staticcatalog-option to implement your own custom storage with |
well it depends on your needs, but given your namespaces I'd consider the following as starting point: files setup like so:
// common-en.json {
"greeting": "Hello"
} // invoice-en.json {
"balance": "Balance"
} // index.js const fs = require('fs')
const i18n = require('i18n')
/**
* very simple file loader treating each file as namespace
*/
const fileLoader = (locale, files) => {
const translations = {}
files.forEach((file) => {
try {
translations[file] = JSON.parse(fs.readFileSync(`./locales/${file}-${locale}.json`))
} catch (err) {
console.error(err);
}
})
return translations
}
/**
* Load translations from files
* with each file representing a namespace
*/
i18n.configure({
defaultLocale: 'en',
objectNotation: true,
updateFiles: false,
staticCatalog: {
en: fileLoader('en', ['common', 'invoice'])
}
})
/**
* debug loaded catalog
*/
console.log(i18n.getCatalog('en')) // --> { common: { greeting: 'Hello' }, invoice: { balance: 'Balance' } }
console.log(i18n.__('common.greeting')) // --> Hello
console.log(i18n.__('invoice.balance')) // --> Balance
|
Right now i'm using express and i have two files in my "locales" folder.
is there a possibility to use different files for the same language (to have a more organized files). Example:
This is the configuration used in the app.js file
The text was updated successfully, but these errors were encountered: