use serde_json::json;
#[must_use]
pub fn default_timezone_for_locale(primary_language: &str) -> &'static str {
let lower = primary_language.to_ascii_lowercase();
let by_region = match lower.as_str() {
"en-us" => Some("America/New_York"),
"en-gb" => Some("Europe/London"),
"en-ca" | "fr-ca" => Some("America/Toronto"),
"en-au" => Some("Australia/Sydney"),
"en-in" | "hi-in" => Some("Asia/Kolkata"),
"de-de" | "de-at" => Some("Europe/Berlin"),
"fr-fr" => Some("Europe/Paris"),
"es-es" => Some("Europe/Madrid"),
"nl-nl" => Some("Europe/Amsterdam"),
"ja-jp" => Some("Asia/Tokyo"),
"zh-cn" => Some("Asia/Shanghai"),
_ => None,
};
if let Some(tz) = by_region {
return tz;
}
match lower.split(['-', '_']).next().unwrap_or(&lower) {
"de" => "Europe/Berlin",
"fr" => "Europe/Paris",
"es" => "Europe/Madrid",
"nl" => "Europe/Amsterdam",
"ja" => "Asia/Tokyo",
"zh" => "Asia/Shanghai",
"hi" => "Asia/Kolkata",
_ => "America/New_York",
}
}
#[must_use]
pub fn intl_spoof_js(primary_locale: &str, timezone: &str) -> String {
let locale = json!(primary_locale);
let tz = json!(timezone);
format!(
r#"
/* Intl + Date (R056): pin the persona's LOCALE and TIMEZONE so the browser
stops leaking the HOST locale/zone. Locale + zone are injected into the REAL
Intl constructors (format AND resolvedOptions genuinely use them), and every
offset/zone-name comes from the browser's OWN ICU for the target zone
(DST-correct for every date), so resolvedOptions / format / getTimezoneOffset
/ the Date+Number+String locale methods are mutually consistent (a partial
spoof would itself be a tell). */
try {{
const __tzseal = (typeof __seal === 'function') ? __seal : (f) => f;
const __LOCALE = {locale};
const __TZ = {tz};
const __OrigDTF = Intl.DateTimeFormat;
const __OrigDate = Date;
const __WD = ['Sun','Mon','Tue','Wed','Thu','Fri','Sat'];
const __MO = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'];
const __wallFmt = new __OrigDTF('en-US', {{ timeZone: __TZ, hourCycle: 'h23',
year: 'numeric', month: '2-digit', day: '2-digit',
hour: '2-digit', minute: '2-digit', second: '2-digit' }});
const __nameFmt = new __OrigDTF('en-US', {{ timeZone: __TZ, timeZoneName: 'long', year: 'numeric' }});
const __wallParts = (date) => {{
const o = {{}};
for (const p of __wallFmt.formatToParts(date)) if (p.type !== 'literal') o[p.type] = p.value;
return o;
}};
// getTimezoneOffset() === (UTC - local) in minutes (positive west of UTC).
const __offMin = (date) => {{
const t = date.getTime();
if (!isFinite(t)) return NaN;
const p = __wallParts(date);
const h = (p.hour === '24') ? 0 : +p.hour;
const asUTC = __OrigDate.UTC(+p.year, +p.month - 1, +p.day, h, +p.minute, +p.second);
return Math.round((t - asUTC) / 60000);
}};
const __zoneLong = (date) => {{
try {{ for (const p of __nameFmt.formatToParts(date)) if (p.type === 'timeZoneName') return p.value; }} catch (_) {{}}
return '';
}};
const __pad = (n) => String(Math.abs(n | 0)).padStart(2, '0');
Date.prototype.getTimezoneOffset = __tzseal(function getTimezoneOffset() {{ return __offMin(this); }}, 'getTimezoneOffset');
const __mkString = (date, withDate, withTime) => {{
const t = date.getTime();
if (!isFinite(t)) return 'Invalid Date';
const off = __offMin(date);
const w = new __OrigDate(t - off * 60000); // local wall clock, read via UTC getters
const datePart = __WD[w.getUTCDay()] + ' ' + __MO[w.getUTCMonth()] + ' ' + __pad(w.getUTCDate()) + ' ' + w.getUTCFullYear();
const sign = off <= 0 ? '+' : '-';
const gmt = 'GMT' + sign + __pad(Math.floor(Math.abs(off) / 60)) + __pad(Math.abs(off) % 60);
const name = __zoneLong(date);
const timePart = __pad(w.getUTCHours()) + ':' + __pad(w.getUTCMinutes()) + ':' + __pad(w.getUTCSeconds())
+ ' ' + gmt + (name ? ' (' + name + ')' : '');
if (withDate && withTime) return datePart + ' ' + timePart;
if (withDate) return datePart;
return timePart;
}};
Date.prototype.toString = __tzseal(function toString() {{ return __mkString(this, true, true); }}, 'toString');
Date.prototype.toDateString = __tzseal(function toDateString() {{ return __mkString(this, true, false); }}, 'toDateString');
Date.prototype.toTimeString = __tzseal(function toTimeString() {{ return __mkString(this, false, true); }}, 'toTimeString');
// Date.toLocale*: default BOTH the persona locale and zone when unspecified.
const __dateLocaleWrap = (name) => {{
const orig = Date.prototype[name];
if (typeof orig !== 'function') return;
Date.prototype[name] = __tzseal(function () {{
const args = Array.prototype.slice.call(arguments);
if (args[0] === undefined) args[0] = __LOCALE;
const opts = Object.assign({{}}, args[1] || {{}});
if (!opts.timeZone) opts.timeZone = __TZ;
return orig.call(this, args[0], opts);
}}, name);
}};
__dateLocaleWrap('toLocaleString');
__dateLocaleWrap('toLocaleDateString');
__dateLocaleWrap('toLocaleTimeString');
// Number.toLocaleString + String.localeCompare default the persona locale.
try {{
const __numOrig = Number.prototype.toLocaleString;
Number.prototype.toLocaleString = __tzseal(function toLocaleString() {{
const args = Array.prototype.slice.call(arguments);
if (args[0] === undefined) args[0] = __LOCALE;
return __numOrig.apply(this, args);
}}, 'toLocaleString');
}} catch (_) {{}}
try {{
const __scOrig = String.prototype.localeCompare;
String.prototype.localeCompare = __tzseal(function localeCompare() {{
const args = Array.prototype.slice.call(arguments);
if (args[1] === undefined) args[1] = __LOCALE;
return __scOrig.apply(this, args);
}}, 'localeCompare');
}} catch (_) {{}}
// Intl.DateTimeFormat: inject the persona LOCALE and ZONE into the REAL
// instance so format() AND resolvedOptions() genuinely use them.
const __DTF = function DateTimeFormat() {{
const args = Array.prototype.slice.call(arguments);
if (args.length === 0 || args[0] === undefined) args[0] = __LOCALE;
if (!(args[1] && args[1].timeZone)) {{
args[1] = Object.assign({{}}, args[1] || {{}});
args[1].timeZone = __TZ;
}}
return Reflect.construct(__OrigDTF, args, new.target || __DTF);
}};
__DTF.prototype = __OrigDTF.prototype;
try {{
__DTF.supportedLocalesOf = __tzseal(function supportedLocalesOf() {{
return __OrigDTF.supportedLocalesOf.apply(__OrigDTF, arguments);
}}, 'supportedLocalesOf');
}} catch (_) {{}}
Intl.DateTimeFormat = __tzseal(__DTF, 'DateTimeFormat');
// Repoint the (shared) prototype's `constructor` at the wrapper so
// `new Intl.DateTimeFormat().constructor === Intl.DateTimeFormat` and
// `Intl.DateTimeFormat.prototype.constructor === Intl.DateTimeFormat` hold,
// exactly as in a real engine. Without this the constructor still pointed
// at the captured original, a trivial one-line tampering tell. The native
// descriptor (writable, non-enumerable, configurable) is preserved.
try {{ Object.defineProperty(__DTF.prototype, 'constructor', {{ value: __DTF, writable: true, enumerable: false, configurable: true }}); }} catch (_) {{}}
// The other Intl constructors: default the persona LOCALE when unspecified.
const __wrapIntlLocale = (ctorName) => {{
const Orig = Intl[ctorName];
if (typeof Orig !== 'function') return;
const Wrapped = function () {{
const args = Array.prototype.slice.call(arguments);
if (args.length === 0 || args[0] === undefined) args[0] = __LOCALE;
return Reflect.construct(Orig, args, new.target || Wrapped);
}};
Wrapped.prototype = Orig.prototype;
try {{
Wrapped.supportedLocalesOf = __tzseal(function supportedLocalesOf() {{
return Orig.supportedLocalesOf.apply(Orig, arguments);
}}, 'supportedLocalesOf');
}} catch (_) {{}}
Intl[ctorName] = __tzseal(Wrapped, ctorName);
// Keep `Intl[ctorName].prototype.constructor === Intl[ctorName]` (real-
// engine invariant); otherwise it still points at the captured original
//: a trivial constructor-identity tell.
try {{ Object.defineProperty(Wrapped.prototype, 'constructor', {{ value: Wrapped, writable: true, enumerable: false, configurable: true }}); }} catch (_) {{}}
}};
['NumberFormat', 'Collator', 'RelativeTimeFormat', 'PluralRules', 'ListFormat'].forEach(__wrapIntlLocale);
}} catch (_) {{}}
"#,
locale = locale,
tz = tz,
)
}
#[cfg(test)]
#[path = "intl_js/tests.rs"]
mod tests;