Skip to content
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

i18n + FR #1610

Merged
merged 51 commits into from
Apr 11, 2021
Merged
Show file tree
Hide file tree
Changes from 38 commits
Commits
Show all changes
51 commits
Select commit Hold shift + click to select a range
0e42520
add localization tags and 3 translation examples
pciavald Mar 23, 2021
79f5a25
build all locales, warn on missing translation
pciavald Mar 23, 2021
2991cd3
Version française V1.0 de la traduction
Mar 25, 2021
5f45a66
delete yarn.lock
pciavald Mar 25, 2021
42fd53a
update package.json with upstream
pciavald Mar 26, 2021
7fe0d10
re-add @angular/localize
pciavald Mar 26, 2021
d0f5b97
merge upstream
pciavald Mar 26, 2021
5a17b8d
fix merge
pciavald Mar 26, 2021
5455084
re-add extract command
pciavald Mar 26, 2021
58bfd22
add ids
pciavald Mar 26, 2021
e85b3ec
replace added spans by ng-containers
pciavald Mar 26, 2021
ff7648a
fix duplicate id
pciavald Mar 26, 2021
aca0c8b
replace spans by ng-containers
pciavald Mar 26, 2021
989c3a7
basic docs
pciavald Mar 27, 2021
50a6370
Prise en compte des review pour la traduction française V1.00
Mar 27, 2021
77fe58c
Merge branch 'i18n' of https://git.iostud.io/vs-projects/octodash int…
Mar 27, 2021
2032b28
remove irrelevant change
pciavald Mar 27, 2021
c25edcb
cleanup angular.json
pciavald Mar 27, 2021
225c7b3
fix FR translations and merge ids
pciavald Mar 27, 2021
a33ea6e
fix package-lock conflict
pciavald Mar 27, 2021
c3dcd51
revert to fr instead of fr-FR
pciavald Mar 27, 2021
165510f
revert fr-FR to fr
pciavald Mar 27, 2021
7394404
fix targets that were sources
pciavald Mar 27, 2021
e8552c8
revert out-file
pciavald Mar 27, 2021
c6673d5
revert package-lock
pciavald Mar 27, 2021
f5cb861
install angular i18n
pciavald Mar 27, 2021
e28498e
fix build issues
pciavald Mar 27, 2021
56cfc57
enable serving custom locales, build detects host lang
pciavald Mar 28, 2021
e4c400b
update command name
pciavald Mar 28, 2021
4bd4017
correct source language
pciavald Mar 28, 2021
97b8477
update locales
pciavald Mar 28, 2021
07e999e
add target language
pciavald Mar 28, 2021
e538f80
add missing semicolons
pciavald Mar 28, 2021
f9be03f
fix duplicate url setting
pciavald Mar 28, 2021
e5a3166
fix sourcelocale basehref
pciavald Mar 28, 2021
2e3105c
fix typo
pciavald Mar 28, 2021
c55ecf9
Nettoyage de coquilles de traduction fance V1.00
Mar 28, 2021
c988918
remove irrelevant files
pciavald Mar 28, 2021
4a7c8b3
Nettoyage de doublons de traduction France V1.00
Mar 29, 2021
7a97c24
Merge branch 'i18n' of https://git.iostud.io/vs-projects/octodash int…
Mar 29, 2021
15472b4
fix adjust screen
pciavald Apr 8, 2021
7e7b431
fix spacing
pciavald Apr 8, 2021
24760f4
comment fixes
pciavald Apr 9, 2021
38a2b08
Merge branch 'upstream' into i18n
pciavald Apr 9, 2021
66907de
translation adjustments
pciavald Apr 9, 2021
44991c3
please linter
pciavald Apr 9, 2021
a3de092
please linter
pciavald Apr 9, 2021
8f57b66
trailing whitespace
pciavald Apr 9, 2021
04d2608
lint autofix
pciavald Apr 9, 2021
1b49d59
use lodash and update package-lock.json
UnchartedBull Apr 11, 2021
156fd4f
revert lodash
UnchartedBull Apr 11, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
# General

If you can think of something nice to add or want to change / extend some of the functionality, feel free to create a Pull Request or an Issue. Please make sure to follow the code style (.prettierrc is included in the GitHub) and not break any of the existing functionality. Any help is greatly appreciated!
## Setting up the repository and build the package

To setup a local environment you need to do the following steps:
- Fork the repository to your GitHub account, so you can push your changes
Expand All @@ -7,3 +10,51 @@ To setup a local environment you need to do the following steps:
- Run the build `npm run build` (only required once to copy assets)
- Start the Live Server: `npm run start` or `npm run start:big`
- If you want to package OctoDash, so you can install it on your Raspberry Pi: `npm run pack`

# Languages

To launch the app in development mode in a specific locale instead of the source english locale, use
```
npm run start --serve="--configuration=<lang>"
```

## Add a new language

To add a language to the list of supported languages, follow these steps:
- find your language code in [this list](https://github.com/angular/angular/tree/master/packages/common/locales) (angular country codes are not all standard)
- in src/locale, duplicate an existing locale (or run `npm run locale:extract` to generate a new locale file)
- rename the file to include your language code in the format `messages.<languageCode>.xlf`
- in the `<file>` tag (second line of the xlf file) adapt `source-language="<languageCode>"` to your language code
- add the translations for your language between `target` tags
- add your language reference in `angular.json`
- `Projects.OctoDash.i18n.locales`
- add:
```
"<lang>": {
"translation": "src/locale/messages.<lang>.xlf",
"baseHref": ""
}
```
- `Projects.OctoDash.architect.build.configurations.production.localize`.
- add:
```
"<lang>"
```
- `Projects.OctoDash.architect.build.configurations`.
- add:
```
"<lang>": {
"localize": ["<lang>"]
},
```
- `Projects.OctoDash.architect.serve.configurations`.
- add:
```
"<lang>": {
"browserTarget": "OctoDash:build:<lang>"
},
```

## Update translations IDs and sources

To update translations with the latest codebase and IDs, run `npm run update:locales`. Only commit the files that do not have a date, these are only backups of the locale versions before the update.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ For more info have a look at the [wiki](https://github.com/UnchartedBull/OctoDas
- You can press multiple arrows directly after another in the control view. All actions will be executed in series, even if the prior didn't finish
- The six actions on the right in the control view can be customized. They can either send GCode commands to your printer, restart OctoPrint or your Pi and even open iFrames so you can view your camera
- You can adjust the temperatures and fan speed in the home screen by pressing on their icons, if you want to set them to zero, just tap the value once (v1.4.1 and up)
- OctoDash will start in your host's language if it is supported. You can start OctoDash in a different supported language by starting it with `LANG=fr_FR.UTF-8 octodash`.

## Demo

Expand Down
24 changes: 24 additions & 0 deletions angular.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,27 @@
"root": "",
"sourceRoot": "src",
"prefix": "app",
"i18n": {
"sourceLocale": {
"code": "en",
"baseHref": ""
},
"locales": {
"fr": {
"translation": "src/locale/messages.fr.xlf",
"baseHref": ""
}
}
},
"architect": {
"build": {
"builder": "@angular-devkit/build-angular:browser",
"options": {
"aot": true,
"outputPath": "dist",
"index": "src/index.html",
"i18nMissingTranslation": "warning",
"localize": ["en"],
"main": "src/main.ts",
"polyfills": "src/polyfills.ts",
"tsConfig": "tsconfig.app.json",
Expand All @@ -44,7 +58,14 @@
"scripts": []
},
"configurations": {
"fr": {
"localize": ["fr"]
},
"production": {
"localize": [
"en",
"fr"
],
"fileReplacements": [
{
"replace": "src/environments/environment.ts",
Expand Down Expand Up @@ -79,6 +100,9 @@
"browserTarget": "OctoDash:build"
},
"configurations": {
"fr": {
"browserTarget": "OctoDash:build:fr"
},
UnchartedBull marked this conversation as resolved.
Show resolved Hide resolved
"production": {
"browserTarget": "OctoDash:build:production"
}
Expand Down
12 changes: 4 additions & 8 deletions helper/listener.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,9 @@ function activateScreenSleepListener(ipcMain) {
ipcMain.on('screenControl', (_, screenCommand) => exec(screenCommand.command));
}

function activateReloadListener(ipcMain, window, dev) {
function activateReloadListener(ipcMain, window, url) {
ipcMain.on('reload', () => {
if (dev) {
window.loadURL('http://localhost:4200');
} else {
window.loadURL('app://.');
}
window.loadURL(url);
});
}

Expand All @@ -44,11 +40,11 @@ function activateConfigListener(ipcMain, window) {
ipcMain.on('checkConfig', (_, config) => checkConfig(window, config));
}

function activateListeners(ipcMain, window, app, dev) {
function activateListeners(ipcMain, window, app, url) {
activateConfigListener(ipcMain, window);
activateAppInfoListener(ipcMain, window, app);
activateScreenSleepListener(ipcMain);
activateReloadListener(ipcMain, window, dev);
activateReloadListener(ipcMain, window, url);
activateUpdateListener(ipcMain, window);
activateDiscoverListener(ipcMain, window);
}
Expand Down
94 changes: 94 additions & 0 deletions helper/locale.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
module.exports = {

// returns the correct supported locale given the LANG variable
getLocale() {
const angularConf = require('../angular.json');
const i18n = angularConf.projects.OctoDash.i18n;
let lang;
try {
// Detect if environment LANG is set
// if it is defined, convert LANG from ie 'en_US.UTF-8' to 'en-US'
// if not defined, set it to the source locale
lang = process.env['LANG']
? process.env['LANG'].split('.')[0].replace('_', '-')
: i18n.sourceLocale.code;
} catch(e) {
// LANG was populated with something else than a standard locale code
lang = i18n.sourceLocale.code;
}
// Check if exact LANG exists in our locales
const exactLocale = Object.keys(i18n.locales).includes(lang) && lang;
// Make the short version, convert ie 'en-US' to 'en'
const shortLang = lang.split('-')[0];
// Check if short LANG exists in our locales
// This matches 'fr-CA' to 'fr' if 'fr-CA' is not defined but 'fr' is
const approximateLocale = Object.keys(i18n.locales).includes(shortLang) && shortLang;
// Define locale by either the exact, approximate, or source locale in this order
const locale = exactLocale || approximateLocale || i18n.sourceLocale.code;
UnchartedBull marked this conversation as resolved.
Show resolved Hide resolved
console.info('selected language: ' + locale);
UnchartedBull marked this conversation as resolved.
Show resolved Hide resolved
return locale;
},

// updates all supported locales consuming an updated messages.xlf
updateLocales() {
const fs = require('fs');
const xliff = require('xliff');

// list all existing locales
// transtaledXLFs = [{
// filename: 'messages.fr.xlf',
// lang: 'fr',
// }]
pciavald marked this conversation as resolved.
Show resolved Hide resolved
let translatedXLFs = [];
const filenames = fs.readdirSync('./src/locale');
for (filename of filenames) {
const match = filename.match(/messages.(..|..-..).xlf/);
if (match) {
const lang = match[1];
translatedXLFs.push({ filename, lang });
}
}

// get extracted messages
const extractedXLF = fs.readFileSync('./src/locale/messages.xlf').toString();
xliff.xliff12ToJs(extractedXLF, (err, extracted) => {
if (err) throw new Error(err.message);

// for each supported locale
for (translatedXLFRef of translatedXLFs) {
const translatedXLF = fs.readFileSync(`./src/locale/${translatedXLFRef.filename}`).toString();

// load this locale
xliff.xliff12ToJs(translatedXLF, (err, translated) => {
if (err) throw new Error(err.message);

// hard copy of messages.xlf
const newTranslation = JSON.parse(JSON.stringify(extracted));
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The project also has lodash installed, maybe use deepCopy from lodash, might be a bit faster: https://lodash.com/docs/4.17.15#cloneDeep

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will do

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

using const _ = require('lodash-es'); i get

internal/modules/cjs/loader.js:1015
      throw new ERR_REQUIRE_ESM(filename, parentPath, packageJsonPath);
      ^

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /home/peac/Documents/vsp/octodash/node_modules/lodash-es/lodash.js
require() of ES modules is not supported.
require() of /home/peac/Documents/vsp/octodash/node_modules/lodash-es/lodash.js from /home/peac/Documents/vsp/octodash/helper/locale.js is an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which defines all .js files in that package scope as ES modules.
Instead rename lodash.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from /home/peac/Documents/vsp/octodash/node_modules/lodash-es/package.json.

    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1015:13)
    at Module.load (internal/modules/cjs/loader.js:863:32)
    at Function.Module._load (internal/modules/cjs/loader.js:708:14)
    at Module.require (internal/modules/cjs/loader.js:887:19)
    at require (internal/modules/cjs/helpers.js:74:18)
    at Object.<anonymous> (/home/peac/Documents/vsp/octodash/helper/locale.js:1:11)
    at Module._compile (internal/modules/cjs/loader.js:999:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)
    at Module.load (internal/modules/cjs/loader.js:863:32)
    at Function.Module._load (internal/modules/cjs/loader.js:708:14) {
  code: 'ERR_REQUIRE_ESM'
}

using import _ from 'lodash-es'; i get

/home/peac/Documents/vsp/octodash/helper/locale.js:1
import _ from 'lodash-es';
^^^^^^

SyntaxError: Cannot use import statement outside a module
    at wrapSafe (internal/modules/cjs/loader.js:915:16)
    at Module._compile (internal/modules/cjs/loader.js:963:27)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)
    at Module.load (internal/modules/cjs/loader.js:863:32)
    at Function.Module._load (internal/modules/cjs/loader.js:708:14)
    at Module.require (internal/modules/cjs/loader.js:887:19)
    at require (internal/modules/cjs/helpers.js:74:18)
    at [eval]:1:1
    at Script.runInThisContext (vm.js:120:18)
    at Object.runInThisContext (vm.js:309:38)

Is it worth investigating further and probably packaging a new module ?

newTranslation.targetLanguage = translated.targetLanguage;
// transfer the locale's translations to the copy of the extracted locale
for (id in newTranslation.resources['ng2.template']) {
const source = translated.resources['ng2.template'];
const target = newTranslation.resources['ng2.template'];
// only copy if the translation has a target
if (source[id]) {
target[id].target = source[id].target;
}
}
// backup the previous version of the locale and write the new locale
xliff.jsToXliff12(newTranslation, (err, result) => {
if (err) throw new Error(err.message);
const now = new Date();
fs.renameSync(
`./src/locale/${translatedXLFRef.filename}`,
`./src/locale/messages.${translatedXLFRef.lang}-${now.toISOString()}.xlf`
);
console.info(`updating ${translatedXLFRef.filename}...`);
fs.writeFileSync(`./src/locale/${translatedXLFRef.filename}`, result);
})
})
}
// remove extracted messages
fs.unlinkSync('./src/locale/messages.xlf');
})
}
};
15 changes: 10 additions & 5 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,22 @@ const dev = args.some(val => val === '--serve');

const activateListeners = require('./helper/listener');

let window;
let locale;
let url;

if (!dev) {
const createProtocol = require('./helper/protocol');
const scheme = 'app';

protocol.registerSchemesAsPrivileged([{ scheme: scheme, privileges: { standard: true } }]);
createProtocol(scheme, path.join(__dirname, 'dist'));

locale = require('./helper/locale.js').getLocale();
}

app.commandLine.appendSwitch('touch-events', 'enabled');

let window;

function createWindow() {
const _store = new Store();

Expand Down Expand Up @@ -57,14 +61,15 @@ function createWindow() {
});

if (dev) {
window.loadURL('http://localhost:4200');
url = 'http://localhost:4200'
window.webContents.openDevTools();
} else {
window.loadURL('app://.');
url = `file://${__dirname}/dist/${locale}/index.html`
window.setFullScreen(true);
}

activateListeners(ipcMain, window, app, dev);
window.loadURL(url);
activateListeners(ipcMain, window, app, url);

window.on('closed', () => {
window = null;
Expand Down
Loading