2018-02-16 02:19:25 +00:00
const util = require ( 'util' ) ;
const path = require ( 'path' ) ;
const fs = require ( 'fs' ) ;
const yaml = require ( 'js-yaml' ) ;
2018-02-16 22:15:45 +00:00
const colors = require ( 'colors/safe' ) ;
2018-02-16 02:19:25 +00:00
const readdir = util . promisify ( fs . readdir ) ;
const readFile = util . promisify ( fs . readFile ) ;
const writeFile = util . promisify ( fs . writeFile ) ;
const localesDir = path . resolve ( _ _dirname , '../src/locales' ) ;
const loadLocales = async ( ) => {
const languages = ( await readdir ( localesDir ) ) . filter ( name => name !== 'index.js' ) ;
let localeData = { } ;
await Promise . all ( languages . map ( async lang => {
const langDir = path . resolve ( localesDir , lang ) ;
const namespaces = ( await readdir ( langDir ) )
. filter ( file => / \ . yaml$ / . test ( file ) ) ;
localeData [ lang ] = { } ;
await Promise . all ( namespaces . map ( async ns => {
const nsFile = path . resolve ( langDir , ns ) ;
localeData [ lang ] [ ns . replace ( '.yaml' , '' ) ] = yaml . safeLoad ( await readFile ( nsFile ) ) ;
} ) ) ;
} ) ) ;
return localeData ;
} ;
const saveLocales = async locales => {
await Promise . all ( Object . keys ( locales ) . map ( async langName => {
const lang = locales [ langName ] ;
await Promise . all ( Object . keys ( lang ) . map ( async nsName => {
const nsFile = path . resolve ( localesDir , langName , ` ${ nsName } .yaml ` ) ;
const yamlDump = yaml . safeDump ( lang [ nsName ] , {
sortKeys : true
} ) ;
await writeFile ( nsFile , yamlDump ) ;
} ) ) ;
} ) ) ;
} ;
loadLocales ( )
. then ( async locales => {
2018-02-16 22:15:45 +00:00
const sourceLocale = locales . en . translation ;
const requiredKeys = Object . keys ( sourceLocale ) ;
2018-02-16 02:19:25 +00:00
const languages = Object . keys ( locales ) . filter ( lang => lang !== 'en' ) ;
languages . forEach ( langName => {
const lang = locales [ langName ] ;
2018-05-31 22:23:04 +00:00
const presentKeys = Object . keys ( lang ) . filter ( nsName => nsName !== 'missing' ) . reduce ( ( list , nsName ) => {
2018-02-16 22:15:45 +00:00
return list . concat ( Object . keys ( lang [ nsName ] ) ) ;
} , [ ] ) ;
const missingKeys = requiredKeys . filter ( key => ! presentKeys . includes ( key ) ) ;
const extraKeys = presentKeys . filter ( key => ! requiredKeys . includes ( key ) ) ;
2018-02-16 02:19:25 +00:00
if ( ! lang . translation ) {
lang . translation = { } ;
}
if ( ! lang . missing ) {
lang . missing = { } ;
}
2018-02-16 22:15:45 +00:00
missingKeys . forEach ( key => {
2018-05-31 22:23:04 +00:00
if ( lang . missing [ key ] ) {
return ;
}
console . log ( colors . yellow . bold ( 'MISSING:' ) , ` ${ langName } needs value for " ${ colors . bold ( key ) } ". ` ) ; //eslint-disable-line no-console
2018-02-16 22:15:45 +00:00
lang . missing [ key ] = sourceLocale [ key ] ;
} ) ;
2018-05-31 22:23:04 +00:00
presentKeys . forEach ( key => {
if ( ! lang . missing [ key ] ) {
return ;
}
console . log ( colors . yellow . bold ( 'DEFINED:' ) , ` Removing " ${ colors . bold ( key ) } " from ${ langName } .missing. It is defined elsewhere. ` ) ; // eslint-disable-line no-console
delete lang . missing [ key ] ;
} ) ;
2018-02-16 22:15:45 +00:00
extraKeys . forEach ( key => {
console . log ( colors . yellow . bold ( 'EXTRA:' ) , ` ${ langName } has extra key for " ${ colors . bold ( key ) } ". It should be removed. ` ) ; // eslint-disable-line no-console
2018-02-16 02:19:25 +00:00
} ) ;
} ) ;
2018-02-16 22:15:45 +00:00
return locales ;
2018-02-16 02:19:25 +00:00
} )
2018-02-16 22:15:45 +00:00
. then ( saveLocales )
2018-02-16 02:19:25 +00:00
. then ( ( ) => {
console . log ( 'Done updating locales' ) ; // eslint-disable-line no-console
} )
. catch ( e => {
2018-02-16 22:15:45 +00:00
console . error ( colors . red . bold ( 'FAILED:' ) , e ) ; // eslint-disable-line no-console
2018-02-16 02:19:25 +00:00
process . exit ( 1 ) ;
} ) ;