Implemented the translation.js module. Implemented the 'TRANSLATION_DEBUG' environment variable. Updated the README.md to include the Translations chapter. Moved en.json to en/email.json to better utilize Crowdin

This commit is contained in:
Glenn de Haan
2024-10-09 11:37:47 +02:00
parent 79b75cd56c
commit 1b1a934f87
6 changed files with 89 additions and 21 deletions

View File

@@ -123,6 +123,8 @@ services:
SMTP_PASSWORD: ''
# Sets the application Log Level (Valid Options: error|warn|info|debug|trace)
LOG_LEVEL: 'info'
# Enables/disables translation debugging, when enabled only translation keys are shown
TRANSLATION_DEBUG: 'false'
```
### Home Assistant Add-on
@@ -454,6 +456,29 @@ Once the SMTP environment variables are configured, the email feature will be av
![Example Email](.docs/images/email_example.png)
## Translations
The UniFi Voucher Site supports multiple languages, and we're actively working to expand the list of available translations. To facilitate this, we use **Crowdin**, a platform that allows people from around the world to help translate and improve the localization of the project.
### How You Can Help
If you'd like to contribute by translating the UniFi Voucher Site into your language or improve existing translations, you're welcome to join our project on Crowdin. Even small contributions can make a big difference!
Simply visit our Crowdin project page by clicking the badge below:
[![Crowdin](https://badges.crowdin.net/unifi-voucher-site/localized.svg)](https://crowdin.com/project/unifi-voucher-site)
Once you're there, you can choose your language and start contributing immediately. Crowdin provides an intuitive interface to help you suggest translations, review them, or vote on others' contributions.
### Getting Started
1. **Create a Crowdin account** (if you don't have one already).
2. **Join the UniFi Voucher Site project** by visiting our [Crowdin page](https://crowdin.com/project/unifi-voucher-site).
3. Choose the language you want to contribute to or suggest improvements for.
4. Start translating or reviewing!
Your contributions will be automatically included in the next release after review.
## Screenshots
### Login (Desktop)

View File

@@ -34,3 +34,4 @@ services:
SMTP_USERNAME: ''
SMTP_PASSWORD: ''
LOG_LEVEL: 'info'
TRANSLATION_DEBUG: 'false'

View File

@@ -1,21 +0,0 @@
{
"email": {
"title": "WiFi Voucher Code",
"preHeader": "Your WiFi Voucher Code",
"greeting": "Hi there",
"intro": "Someone generated a WiFi Voucher, please use this code when connecting",
"connect": "Connect to",
"password": "Password",
"or": "or",
"scan": "Scan to connect",
"details": "Voucher Details",
"type": "Type",
"multiUse": "Multi-use",
"singleUse": "Single-use",
"duration": "Duration",
"dataLimit": "Data Limit",
"downloadLimit": "Download Limit",
"uploadLimit": "Upload Limit",
"poweredBy": "Powered by"
}
}

19
locales/en/email.json Normal file
View File

@@ -0,0 +1,19 @@
{
"title": "WiFi Voucher Code",
"preHeader": "Your WiFi Voucher Code",
"greeting": "Hi there",
"intro": "Someone generated a WiFi Voucher, please use this code when connecting",
"connect": "Connect to",
"password": "Password",
"or": "or",
"scan": "Scan to connect",
"details": "Voucher Details",
"type": "Type",
"multiUse": "Multi-use",
"singleUse": "Single-use",
"duration": "Duration",
"dataLimit": "Data Limit",
"downloadLimit": "Download Limit",
"uploadLimit": "Upload Limit",
"poweredBy": "Powered by"
}

43
modules/translation.js Normal file
View File

@@ -0,0 +1,43 @@
/**
* Import base packages
*/
const fs = require('fs');
/**
* Import own modules
*/
const variables = require('./variables');
/**
* Translation returns translator function
*
* @param language
* @param module
* @return {(function(key: string): (string))}
*/
module.exports = (language = 'en', module) => {
// Check if translation file exists
if(!fs.existsSync(`${__dirname}/../locales/${language}/${module}.json`)) {
throw new Error(`[Translation] Missing translation file: ${__dirname}/../locales/${language}/${module}.json`);
}
// Get locales mapping
const locales = JSON.parse(fs.readFileSync(`${__dirname}/../locales/_locales.json`, 'utf-8'));
// Get translation file
const translations = JSON.parse(fs.readFileSync(`${__dirname}/../locales/${language}/${module}.json`, 'utf-8'));
// Return translate function
return (key) => {
if(key === '_locales') {
return locales;
}
// Check if key exists within translation file
if(typeof translations[key] === 'undefined') {
throw new Error(`[Translation][${language}] Missing for key: ${key}`);
}
// Check if debugging is enabled. If enabled only return key
return variables.translationDebug ? key : translations[key];
};
};

View File

@@ -41,6 +41,7 @@ module.exports = {
smtpUsername: config('smtp_username') || process.env.SMTP_USERNAME || '',
smtpPassword: config('smtp_password') || process.env.SMTP_PASSWORD || '',
logLevel: config('log_level') || process.env.LOG_LEVEL || 'info',
translationDebug: config('translation_debug') || (process.env.TRANSLATION_DEBUG === 'true') || false,
gitTag: process.env.GIT_TAG || 'master',
gitBuild: fs.existsSync('/etc/unifi_voucher_site_build') ? fs.readFileSync('/etc/unifi_voucher_site_build', 'utf-8') : 'Development'
};