Skip to content

criticalmanufacturing/dev-i18n-transform

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

57 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Translation Resources Transform

Build Status Coverage Status Codacy Badge

i18n transform is a CMF developer tool that easily converts the development translation resources (in typescript) into the gettext PO files and vice-versa.

Transform TS -> PO

Typescript

// Get parser for TS files
let parser = ParserFactory.getParser(
    __dirname, // path to package
    [
        // these files are your ts files, relative to your package
        path.join(__dirname, "test/mocks/multilevelExample/mock.default.ts"),
        path.join(__dirname, "test/mocks/multilevelExample/mock.pt-PT.ts")
    ]
);
// Run parser and get package
let pack = parser.run();

// Get writer for "pt-PT" language
let writer = WriterFactory.getWriter(pack, "pt-PT", "po");

// Run writer and get buffer files
let buffer = writer.run();

// Write buffer (create files)
fs.writeFileSync("output/i18n.po", buffer[0].content);

Gulp

var i18n = require("cmf.dev.i18n").gulp;

gulp.task('i18n-ts-to-po', function() {
    return gulp
        .src([
            // these files are your ts files, relative to your package
            // in this example, __dirname is the package root dir
            path.join(__dirname, "test/**/*.ts"),
            "!**/*.d.ts" // remember to remove *.d.ts files
        ], { cwd: __dirname }) // very important
        .pipe(i18n({
            base: __dirname, // your package root dir
            languages: ["en-EN", "pt-PT", ...], // 
            dest: "po" // destination format, "ts" or "po"
        }))
        .pipe(gulp.dest("output")); // this will create 1 file for each language in dest folder
});

Transform PO -> TS

Typescript

// Get parser for PO files
let parser = ParserFactory.getParser(
    __dirname, // path to package
    [
        // these files are your po files, relative to your package
        path.join(__dirname, "output/i18n.po"),
    ]
);
// Run parser and get package
let pack = parser.run();

// Get writer for "pt-PT" language
let writer = WriterFactory.getWriter(pack, "pt-PT", "ts");

// Run writer and get buffer files
let buffer = writer.run();

// Write buffer (create files)
for(let file of buffer) {
    fs.writeFileSync(path.join("output", file.file), file.content);
}

Gulp

var i18n = require("cmf.dev.i18n").gulp;

gulp.task('i18n-po-to-ts', function() {
    return gulp
        .src([
            // these files are your po files, relative to your package
            // in this example, __dirname is the package root dir
            path.join(__dirname, "output/*.po")
        ], { cwd: __dirname }) // very important
        .pipe(i18n({
            base: __dirname, // your package root dir
            languages: ["en-EN", "pt-PT", ...], // 
            dest: "ts" // destination format, "ts" or "po"
        }))
        .pipe(gulp.dest("output")); // this will create 1 file for each language in dest folder
});

Additional Information

This package was developed during the UX-FAB: Universal Experience for Advanced Fabs project.

Portugal2020