Untitled
Anonymous
Plain Text
09/22/2025 4:31 AM
10.9 KB
18
Indexable
Never
)<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width,initial-scale=1" />
<title>International Calculator</title>
<style>
:root{
--bg:#0f1724; --card:#0b1220; --accent:#06b6d4; --text:#e6eef6; --muted:#9fb4c9;
font-family: Inter, system-ui, -apple-system, 'Segoe UI', Roboto, 'Noto Sans', Arial;
}
*{box-sizing:border-box}
body{margin:0; min-height:100vh; display:flex; align-items:center; justify-content:center; background:linear-gradient(180deg,#031028 0%, #05263a 100%); color:var(--text)}
.calc{width:360px; max-width:95vw; background:var(--card); border-radius:14px; padding:18px; box-shadow:0 8px 30px rgba(2,6,23,.6)}
.top{display:flex; gap:8px; align-items:center; justify-content:space-between; margin-bottom:12px}
.brand{font-weight:600}
.controls{display:flex; gap:8px}
select, button.lang{background:transparent; color:var(--muted); border:1px solid rgba(255,255,255,0.04); padding:6px 8px; border-radius:8px}
.display{background:rgba(255,255,255,0.02); padding:14px; border-radius:10px; min-height:72px; display:flex; flex-direction:column; justify-content:center; align-items:flex-end}
.expression{color:var(--muted); font-size:14px; min-height:18px}
.result{font-size:24px; font-weight:600}
.pad{display:grid; grid-template-columns:repeat(4,1fr); gap:10px; margin-top:12px}
button.key{padding:16px; border-radius:8px; border:0; font-size:18px; background:linear-gradient(180deg, rgba(255,255,255,0.02), rgba(255,255,255,0.01)); color:var(--text)}
button.op{background:linear-gradient(180deg,var(--accent), rgba(3,105,123,0.9)); color:#012; font-weight:700}
button.wide{grid-column:span 2}
footer{margin-top:10px; color:var(--muted); font-size:13px; text-align:center}
.locale-info{font-size:12px; color:var(--muted)}
</style>
</head>
<body>
<main class="calc" role="application" aria-label="International Calculator">
<div class="top">
<div class="brand">đ <span id="app-title">International Calculator</span></div>
<div class="controls">
<select id="lang">
<option value="en">English</option>
<option value="bn">āĻŦāĻžāĻāϞāĻž</option>
<option value="es">EspaÃąol</option>
<option value="fr">Français</option>
<option value="de">Deutsch</option>
</select>
<select id="locale"></select>
</div>
</div><div class="display" id="display">
<div class="expression" id="expr"> </div>
<div class="result" id="res">0</div>
</div>
<div class="pad" role="group" aria-label="calculator keys">
<button class="key" data-action="clear">C</button>
<button class="key" data-action="back">âĢ</button>
<button class="key" data-action="percent">%</button>
<button class="key op" data-value="/">Ãˇ</button>
<button class="key" data-value="7">7</button>
<button class="key" data-value="8">8</button>
<button class="key" data-value="9">9</button>
<button class="key op" data-value="*">Ã</button>
<button class="key" data-value="4">4</button>
<button class="key" data-value="5">5</button>
<button class="key" data-value="6">6</button>
<button class="key op" data-value="-">â</button>
<button class="key" data-value="1">1</button>
<button class="key" data-value="2">2</button>
<button class="key" data-value="3">3</button>
<button class="key op" data-value="+">+</button>
<button class="key wide" data-value="0">0</button>
<button class="key" data-value=".">.</button>
<button class="key op" data-action="equals">=</button>
</div>
<footer>
<div class="locale-info" id="localeInfo">Locale: â</div>
<div style="margin-top:6px">Tip: You can paste numbers or expressions (e.g. 12.5*4) into the display. Keyboard supported.</div>
</footer>
</main> <script>
// International Calculator: single-file, copy-paste runnable
const translations = {
en: {title:'International Calculator', clear:'C', equals:'=', localeLabel:'Locale'},
bn: {title:'āĻāύā§āϤāϰā§āĻāĻžāϤāĻŋāĻ āĻā§āϝāĻžāϞāĻā§āϞā§āĻāϰ', clear:'āϏāĻžāĻĢ', equals:'=', localeLabel:'āϞā§āĻā§āϞ'},
es: {title:'Calculadora Internacional', clear:'C', equals:'=', localeLabel:'ConfiguraciÃŗn regional'},
fr: {title:'Calculatrice Internationale', clear:'C', equals:'=', localeLabel:'Paramètres rÊgionaux'},
de: {title:'Internationaler Taschenrechner', clear:'C', equals:'=', localeLabel:'Gebietsschema'},
};
const langSel = document.getElementById('lang');
const localeSel = document.getElementById('locale');
const exprDiv = document.getElementById('expr');
const resDiv = document.getElementById('res');
const localeInfo = document.getElementById('localeInfo');
const appTitle = document.getElementById('app-title');
// Fill locale options (popular list + auto-detect)
const sampleLocales = ['en-US','en-GB','bn-BD','bn-IN','es-ES','es-MX','fr-FR','de-DE','zh-CN','ja-JP'];
function buildLocaleOptions() {
localeSel.innerHTML = '';
const detected = Intl.DateTimeFormat().resolvedOptions().locale || navigator.language || 'en-US';
const autoOpt = document.createElement('option'); autoOpt.value='auto'; autoOpt.textContent = `Auto (${detected})`;
localeSel.appendChild(autoOpt);
sampleLocales.forEach(l=>{
const o=document.createElement('option'); o.value=l; o.textContent=l; localeSel.appendChild(o);
})
localeSel.value = 'auto';
}
buildLocaleOptions();
// Application state
let expression = '';
let currentLocale = 'auto';
let currentLang = 'en';
function t(k){ return translations[currentLang][k] ?? translations['en'][k] }
function updateUI(){
appTitle.textContent = translations[currentLang].title;
localeInfo.textContent = `${t('localeLabel')}: ${getActiveLocale()}`;
exprDiv.textContent = expression || '\u00A0';
}
function getActiveLocale(){
if(localeSel.value && localeSel.value !== 'auto') return localeSel.value;
return Intl.DateTimeFormat().resolvedOptions().locale || 'en-US';
}
function formatNumberForLocale(n){
try{
const fmt = new Intl.NumberFormat(getActiveLocale(), {maximumFractionDigits:12});
return fmt.format(n);
}catch(e){ return String(n) }
}
// Simple expression evaluator using shunting-yard algorithm (supports + - * / % and parentheses and decimals)
function tokenize(input){
const tokens = [];
const re = /\s*([0-9]*\.?[0-9]+|[()+\-*/%])\s*/g;
let m; while((m=re.exec(input))){ tokens.push(m[1]); }
return tokens;
}
function precedence(op){ if(op==='+'||op==='-') return 1; if(op==='*'||op==='/'||op==='%') return 2; return 0 }
function toRPN(tokens){
const out=[]; const ops=[];
for(const t of tokens){
if(/^[0-9]/.test(t)) out.push(t);
else if(t==='(') ops.push(t);
else if(t===')'){ while(ops.length && ops[ops.length-1] !== '(') out.push(ops.pop()); ops.pop(); }
else { while(ops.length && precedence(ops[ops.length-1]) >= precedence(t)) out.push(ops.pop()); ops.push(t); }
}
while(ops.length) out.push(ops.pop());
return out;
}
function evalRPN(rpn){
const st=[];
for(const tok of rpn){
if(/^[0-9]/.test(tok)) st.push(parseFloat(tok));
else{
const b = st.pop(); const a = st.pop();
if(tok==='+') st.push(a+b);
else if(tok==='-') st.push(a-b);
else if(tok==='*') st.push(a*b);
else if(tok==='/') st.push(a/b);
else if(tok==='%') st.push(a % b);
}
}
return st[0];
}
function safeEvaluate(input){
try{
const tokens = tokenize(input);
if(tokens.length===0) return 0;
const rpn = toRPN(tokens);
const v = evalRPN(rpn);
if(!isFinite(v)) return 'Error';
return v;
}catch(e){ return 'Error' }
}
// Button handling
document.querySelectorAll('.pad button').forEach(btn=>{
btn.addEventListener('click', ()=>{
const val = btn.dataset.value;
const action = btn.dataset.action;
if(action==='clear'){ expression=''; resDiv.textContent='0'; updateUI(); return }
if(action==='back'){ expression = expression.slice(0,-1); updateUI(); return }
if(action==='percent'){ // transform last number into percent
const match = expression.match(/([0-9]*\.?[0-9]+)$/);
if(match){ const num = parseFloat(match[1]); expression = expression.slice(0, -match[1].length) + (num/100); }
updateUI(); return
}
if(action==='equals'){ const v = safeEvaluate(expression); resDiv.textContent = (typeof v==='number')? formatNumberForLocale(v) : v; return }
if(val) { expression += val; updateUI(); }
})
});
// Keyboard & paste support
window.addEventListener('keydown', (e)=>{
if(e.key === 'Enter') { e.preventDefault(); document.querySelector('[data-action="equals"]').click(); }
else if(e.key === 'Backspace') document.querySelector('[data-action="back"]').click();
else if(e.key === 'Escape') document.querySelector('[data-action="clear"]').click();
else if(/[0-9.+\-*/()%]/.test(e.key)) { expression += e.key; updateUI(); }
});
window.addEventListener('paste', (e)=>{
const txt = (e.clipboardData || window.clipboardData).getData('text');
// Accept only safe characters
if(/^[0-9+\-*/().,%\s]+$/.test(txt)){
expression += txt.replace(/,/g, '.'); // try to normalize comma decimals
updateUI();
e.preventDefault();
}
});
// Locale & language selectors
langSel.addEventListener('change', ()=>{ currentLang = langSel.value; updateUI(); });
localeSel.addEventListener('change', ()=>{ currentLocale = localeSel.value; updateUI(); });
// Initial UI
updateUI();
// Optional: auto-format partial result as user types (non-invasive)
let autofmtTimer;
const showPartialResult = ()=>{
clearTimeout(autofmtTimer);
autofmtTimer = setTimeout(()=>{
const value = safeEvaluate(expression);
if(typeof value === 'number') resDiv.textContent = formatNumberForLocale(value);
}, 250);
}
// Observe expression changes to show partial
const exprObserver = new MutationObserver(showPartialResult);
exprObserver.observe(exprDiv, {childList:true, characterData:true, subtree:true});
// Make expression update also call partial result
const origUpdateUI = updateUI;
updateUI = function(){ origUpdateUI(); showPartialResult(); }
// Detect user's preferred language and locale on load
(function detectPrefs(){
const userLang = (navigator.language || navigator.userLanguage || 'en').slice(0,2);
if(translations[userLang]){ currentLang = userLang; langSel.value = userLang }
// Set locale dropdown to auto (already set) and update UI
updateUI();
})();
</script></body>
</html>Editor is loading...
Leave a Comment