Skip to content

Commit

Permalink
Add support to transform translated texta to allow create pseudolocales
Browse files Browse the repository at this point in the history
  • Loading branch information
juanluispaz committed Oct 2, 2021
1 parent 3cd3b05 commit 8b4af2e
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 8 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,10 @@ There are several configurations that you can specify to `gettext-webpack-plugin

**Note**: You can overload a function according to the number of parameters.

### Processing the translated text

- ``transformText``: (function, optional, default `undefined`) Function with signature `(text: string) => string` that allows to transform the transaled text. Providing this function you can, by example, implement a [pseudolocalization](https://en.wikipedia.org/wiki/Pseudolocalization) by transforming the text. In the provided example the [pseudolocale](https://www.npmjs.com/package/pseudolocale) to create the pseodolocalized texts.

### Other configurations

- **`includeFuzzy`**: (boolean, optional, default `false`) Include the fuzzy translations. Default: false, that means, the translation marked as 'Need works' will be ignored during the process.
Expand Down
14 changes: 14 additions & 0 deletions example/i18n/pseudo-en.po
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
msgid ""
msgstr ""
"Project-Id-Version: \n"
"POT-Creation-Date: 2019-08-10 18:09+0200\n"
"PO-Revision-Date: 2019-08-10 18:11+0200\n"
"Last-Translator: \n"
"Language-Team: \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"X-Generator: Poedit 2.0.6\n"
"X-Poedit-Basepath: .\n"
"Plural-Forms: nplurals=2; plural=(n != 1);\n"
"Language: en\n"
3 changes: 2 additions & 1 deletion example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
"gettext-webpack-plugin": "^2.0.0",
"html-webpack-plugin": "^5.3.2",
"intl-messageformat": "^9.9.2",
"pseudolocale": "^1.2.0",
"webpack": "^5.54.0",
"webpack-cli": "^4.8.0"
}
}
}
25 changes: 20 additions & 5 deletions example/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ const fs = require('fs');
const process = require('process');
const path = require('path');
const webpack = require('webpack');
const pseudolocale = require('pseudolocale');

pseudolocale.option.startDelimiter = '{';
pseudolocale.option.endDelimiter = '}';
pseudolocale.option.prepend = ''
pseudolocale.option.append = ''
const pseudolocalize = pseudolocale.str

if (!fs.existsSync('i18n')) {
console.error('No .po files found in the folder i18n');
Expand All @@ -25,9 +32,14 @@ if (poFiles.length <= 0) {
}

module.exports = poFiles.map(function(poFile) {
const language = path.parse(poFile).name.replace('_', '-');
const localeName = path.parse(poFile).name.replace('_', '-');
let language = localeName;
let isPseudo = language.startsWith('pseudo-');
if (isPseudo) {
language = language.substring('pseudo-'.length)
}
return {
name: language,
name: localeName,
mode: 'production',
entry: './src/index.js',
module: {
Expand All @@ -37,9 +49,12 @@ module.exports = poFiles.map(function(poFile) {
]
},
plugins: [
new GettextWebpackPlugin({ translation: poFile }),
new GettextWebpackPlugin({
translation: poFile,
transformText: isPseudo ? pseudolocalize : undefined
}),
new HtmlWebpackPlugin({
filename: language + '.index.html',
filename: localeName + '.index.html',
template: 'src/index.html',
minify: {
collapseWhitespace: true,
Expand All @@ -57,7 +72,7 @@ module.exports = poFiles.map(function(poFile) {
})
],
output: {
filename: language + '.[name].[contenthash].js'
filename: localeName + '.[name].[contenthash].js'
}
};
});
32 changes: 30 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ class GettextWebpackPlugin {
* function(context: string, text: string): string. Optional.
* - npgettextFunctionName: string - Npgettext function name. The signature of this function will be:
* function(context: string, singular: string, plural: string): string[]. Optional.
* - transformToJS: function(string | string[]): string - Function that transforms the translation result in
* - transformText: function(text: string): string - Funtion that allows to transform the transaled text; allowing
* by example implement a pseudolocalization by transforming the text. Optional.
* - transformToJS: function(translation: string | string[]): string - Function that transforms the translation result in
* JavaScript. By default JSON.stringify will be used. This function can change gettext results that can be
* different to string or string[]. This function is usefull, by example, if you whant to compile the
* translations into a function that handle the interpolations. Note: the result of this function must be
Expand All @@ -53,9 +55,35 @@ class GettextWebpackPlugin {
}

let transformToJS = options.transformToJS;
let transformText = options.transformText;
if (!transformToJS) {
if (transformText) {
transformToJS = (translation) => {
if (Array.isArray(translation)) {
translation = translation.map((text) => {
return transformText(text);
});
} else {
translation = transformText(translation);
}
return JSON.stringify(translation);
}
} else {
transformToJS = (translation) => {
return JSON.stringify(translation);
}
}
} else if (transformText) {
const initialTransformToJS = transformToJS;
transformToJS = (translation) => {
return JSON.stringify(translation);
if (Array.isArray(translation)) {
translation = translation.map((text) => {
return transformText(text);
});
} else {
translation = transformText(translation);
}
return initialTransformToJS(translation);
}
}

Expand Down

0 comments on commit 8b4af2e

Please sign in to comment.