Files
product-slip/slips/static/slips/js/i18n.js

223 lines
8.2 KiB
JavaScript

const translations = {
'en': {
'app_title': 'Label Printer',
'data_entry': 'Data Entry',
'products': 'Products',
'print_queue': 'Print Queue',
'calibrate': 'Calibrate',
'new_order': 'New Order',
'product': 'Product',
'add_product': '+ Add New Product',
'edit_product': 'Edit Product',
'edit': 'Edit',
'backup': 'Backup',
'restore': 'Restore',
'no_products': 'No products yet',
'product_code': 'Product Code',
'description': 'Description',
'default_weight': 'Default Weight',
'save': 'Save',
'cancel': 'Cancel',
'batch_number': 'Batch #',
'date': 'Date',
'next_control': 'Next Control Date',
'net_weight': 'Net Weight (kg)',
'total_weight': 'Total Weight (kg)',
'canister_weight': 'Canister Weight (kg)',
'canisters_per_box': 'Canisters per Box',
'labels_preview': 'Labels Generated',
'canisters': 'Canisters',
'boxes': 'Boxes',
'canisters_unit': 'Canisters',
'boxes_unit': 'Boxes',
'pages_unit': 'Page(s)',
'preview_pages': 'pages',
'pages_needed': 'Pages Needed',
'total_pages': 'Pages',
'status_pending': 'Pending',
'add_to_queue': 'Add to Print Queue',
'recent_orders': 'Recent Orders',
'status': 'Status',
'no_orders': 'No orders yet',
'no_pending_orders': 'No pending orders to print.',
'print': 'Print',
'mark_done': 'Mark Done',
'undo_done': 'Undo',
'remove': 'Remove',
'print_history': 'Print History',
'printed_at': 'Printed At',
'no_history': 'No print history'
},
'fa': {
'app_title': 'پرینتر لیبل',
'data_entry': 'ثبت اطلاعات',
'products': 'محصولات',
'print_queue': 'صف چاپ',
'calibrate': 'کالیبراسیون',
'new_order': 'سفارش جدید',
'product': 'محصول',
'add_product': '+ افزودن محصول جدید',
'edit_product': 'ویرایش محصول',
'edit': 'ویرایش',
'backup': 'پشتیبان',
'restore': 'بازیابی',
'no_products': 'محصولی ثبت نشده',
'product_code': 'کد محصول',
'description': 'شرح',
'default_weight': 'وزن پیش‌فرض',
'save': 'ذخیره',
'cancel': 'لغو',
'batch_number': 'شماره بچ',
'date': 'تاریخ',
'next_control': 'تاریخ کنترل بعدی',
'net_weight': 'وزن خالص (کیلوگرم)',
'total_weight': 'وزن کل (کیلوگرم)',
'canister_weight': 'وزن گالن (کیلوگرم)',
'canisters_per_box': 'تعداد گالن در هر کارتن',
'labels_preview': 'برچسب‌های تولیدی',
'canisters': 'تعداد گالن',
'boxes': 'تعداد کارتن',
'canisters_unit': 'گالن',
'boxes_unit': 'کارتن',
'pages_unit': 'صفحه',
'preview_pages': 'صفحه',
'pages_needed': 'تعداد صفحات',
'total_pages': 'صفحات',
'status_pending': 'در انتظار',
'add_to_queue': 'افزودن به صف چاپ',
'recent_orders': 'سفارشات اخیر',
'status': 'وضعیت',
'no_orders': 'سفارشی ثبت نشده',
'no_pending_orders': 'سفارشی در انتظار چاپ نیست.',
'print': 'چاپ',
'mark_done': 'چاپ شد',
'undo_done': 'بازگردانی',
'remove': 'حذف',
'print_history': 'سابقه چاپ',
'printed_at': 'زمان چاپ',
'no_history': 'سابقه‌ای وجود ندارد'
}
};
let currentLang = localStorage.getItem('slip_lang') || 'fa';
let currentTheme = localStorage.getItem('slip_theme') || 'light';
// SVG flags: Windows often cannot render flag emojis (shows "IR"/"GB" instead)
const FLAG_SVG = {
fa: '<svg class="lang-flag" viewBox="0 0 9 6" aria-hidden="true"><rect width="9" height="2" fill="#239f40"/><rect y="2" width="9" height="2" fill="#fff"/><rect y="4" width="9" height="2" fill="#da0000"/></svg>',
en: '<svg class="lang-flag" viewBox="0 0 60 30" aria-hidden="true"><rect width="60" height="30" fill="#012169"/><path d="M0,0 60,30M60,0 0,30" stroke="#fff" stroke-width="6"/><path d="M0,0 60,30M60,0 0,30" stroke="#C8102E" stroke-width="2"/><path d="M30,0v30M0,15h60" stroke="#fff" stroke-width="10"/><path d="M30,0v30M0,15h60" stroke="#C8102E" stroke-width="6"/></svg>',
};
function applyTheme(theme) {
document.documentElement.setAttribute('data-theme', theme);
const label = document.getElementById('theme-label');
if (label) {
// show the theme you'll switch to
label.textContent = theme === 'light' ? '🌙' : '☀️';
}
}
function toggleTheme() {
currentTheme = currentTheme === 'light' ? 'dark' : 'light';
localStorage.setItem('slip_theme', currentTheme);
applyTheme(currentTheme);
}
function applyLanguage(lang) {
document.documentElement.lang = lang;
document.documentElement.dir = lang === 'fa' ? 'rtl' : 'ltr';
document.querySelectorAll('[data-i18n]').forEach(el => {
const key = el.getAttribute('data-i18n');
if (translations[lang][key]) {
el.textContent = translations[lang][key];
}
});
// Button shows the language you can switch to
const toggleBtn = document.getElementById('lang-label');
if (toggleBtn) {
const next = lang === 'en' ? 'fa' : 'en';
toggleBtn.innerHTML = FLAG_SVG[next] + ' ' + next.toUpperCase();
}
if (typeof window.updateLabelsPreview === 'function') {
window.updateLabelsPreview();
}
}
function toggleLanguage() {
currentLang = currentLang === 'en' ? 'fa' : 'en';
localStorage.setItem('slip_lang', currentLang);
applyLanguage(currentLang);
}
// Common Toast notification
function showToast(message, isError = false) {
const toast = document.getElementById('toast');
if (!toast) return;
toast.textContent = message;
if (isError) {
toast.classList.add('error');
} else {
toast.classList.remove('error');
}
toast.classList.add('show');
setTimeout(() => toast.classList.remove('show'), 3000);
}
async function adjustCount(id, field, delta) {
const spanId = field === 'canister_count' ? `cc-${id}` : `bc-${id}`;
const span = document.getElementById(spanId);
if (!span) return;
const next = parseInt(span.textContent, 10) + delta;
if (next < 1) return;
try {
const res = await fetch(`/api/orders/${id}`, {
method: 'PATCH',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ [field]: next })
});
if (!res.ok) {
showToast(currentLang === 'fa' ? 'خطا در ویرایش' : 'Error updating order', true);
return;
}
const order = await res.json();
const cc = document.getElementById(`cc-${id}`);
const bc = document.getElementById(`bc-${id}`);
if (cc) cc.textContent = order.canister_count;
if (bc) bc.textContent = order.box_count;
const tw = document.getElementById(`tw-${id}`);
if (tw) tw.textContent = `${order.total_weight} kg`;
const pages = document.getElementById(`pages-${id}`);
if (pages) pages.textContent = order.canister_pages + order.box_pages;
} catch (err) {
showToast('Network error', true);
}
}
async function deleteOrder(id) {
const msg = currentLang === 'fa' ? 'این سفارش حذف شود؟' : 'Remove this order?';
if (!confirm(msg)) return;
try {
const res = await fetch(`/api/orders/${id}`, { method: 'DELETE' });
if (res.ok) {
window.location.reload();
} else {
showToast(currentLang === 'fa' ? 'خطا در حذف' : 'Error removing order', true);
}
} catch (err) {
showToast('Network error', true);
}
}
// Initialize on load
document.addEventListener('DOMContentLoaded', () => {
applyTheme(currentTheme);
applyLanguage(currentLang);
});