Nikuichi / tools /patch_mainjs.py
Aisnsio's picture
30 languages
df42693 verified
Raw History Blame Contribute Delete
3.27 kB
import re,sys
p='js/main.js'; s=open(p,encoding='utf-8').read()
assert 'NK_T' not in s
lines=s.split('\n')
JP=re.compile(r'[぀-ヿ一-鿿]')
lit=re.compile(r"'((?:[^'\\\n]|\\.)*)'")
out=[]; skip=False
for i,l in enumerate(lines):
st=l.strip()
if st.startswith('function note()') or st.startswith('function message()'): skip=True
if st.startswith('function showErr'): skip=False
if skip or st.startswith('var WD =') or st.startswith('var fmtDate') or st.startswith('/*') or st.startswith('*') or st.startswith('//') or 'calTitle' in l:
out.append(l); continue
out.append(lit.sub(lambda m: "t('%s')"%m.group(1) if JP.search(m.group(1)) else m.group(0), l))
s='\n'.join(out)
helpers=""" var $ = function (id) { return document.getElementById(id); };
/* i18n: NK_T comes from <lang>/i18n.js; Japanese pages have none. Messages sent to the shop stay Japanese. */
var LANG = window.NK_LANG || 'ja', JA = LANG === 'ja', T = window.NK_T || {};
var t = function (k) { return Object.prototype.hasOwnProperty.call(T, k) ? T[k] : k; };
var tf = function (k, v) { return t(k).replace(/\\{(\\w+)\\}/g, function (_, n) { return v[n]; }); };"""
s=s.replace(" var $ = function (id) { return document.getElementById(id); };",helpers,1)
R=[
(" var fmtDate = function (d) { return d.getFullYear() + '年' + (d.getMonth() + 1) + '月' + d.getDate() + '日(' + WD[d.getDay()] + ')'; };",
" var fmtDate = function (d) { return d.getFullYear() + '年' + (d.getMonth() + 1) + '月' + d.getDate() + '日(' + WD[d.getDay()] + ')'; };\n var fmtDateUI = JA ? fmtDate : function (d) { try { return d.toLocaleDateString(LANG, { year: 'numeric', month: 'long', day: 'numeric', weekday: 'short' }); } catch (e) { return iso(d); } };\n var WDUI = JA ? WD : WD.map(function (_, i) { try { return new Date(2023, 0, 1 + i).toLocaleDateString(LANG, { weekday: 'short' }); } catch (e) { return WD[i]; } });"),
(" $('calTitle').textContent = y + '年 ' + (m + 1) + '月';",
" $('calTitle').textContent = JA ? y + '年 ' + (m + 1) + '月' : new Date(y, m, 1).toLocaleDateString(LANG, { year: 'numeric', month: 'long' });"),
(" WD.forEach(function (w, i) {"," WDUI.forEach(function (w, i) {"),
("label = fmtDate(dt);","label = fmtDateUI(dt);"),
("o.textContent = c.label;","o.textContent = t(c.label);"),
(" $('party').textContent = S.party + t('名');"," $('party').textContent = tf('{n}名', { n: S.party });"),
("(S.party >= pb.max ? t('Web予約は') + pb.max + t('名様までです。') : '') + t('20名様以上のご宴会・貸切(50名様〜)はお電話でご相談ください:') + st.tel",
"(S.party >= pb.max ? tf('Web予約は{n}名様までです。', { n: pb.max }) : '') + t('20名様以上のご宴会・貸切(50名様〜)はお電話でご相談ください:') + st.tel"),
(" if (S.date) rows.push([t('日時'), fmtDate(r.date"," if (S.date) rows.push([t('日時'), fmtDateUI(r.date"),
(" rows.push([t('人数'), (r.party || S.party) + t('名')], [t('コース'), courseLabel()]);",
" rows.push([t('人数'), tf('{n}名', { n: r.party || S.party })], [t('コース'), t(courseLabel())]);"),
]
for a,b in R:
assert s.count(a)==1,a
s=s.replace(a,b)
open(p,'w',encoding='utf-8').write(s)