Internationalize Bible UI and add locale-aware translation fetch
This commit is contained in:
@@ -1,4 +1,21 @@
|
||||
const BIBLE_TOKEN_REGEX = /@bible:([^\s]+)/gi;
|
||||
const DEFAULT_TRANSLATION = "web";
|
||||
|
||||
const getNormalizedLocale = (locale = "") => {
|
||||
return String(locale || "").toLowerCase().replace("_", "-").trim();
|
||||
};
|
||||
|
||||
const getTranslationForLocale = (locale = "") => {
|
||||
const normalized = getNormalizedLocale(locale);
|
||||
if (!normalized) return "kjv";
|
||||
if (normalized.startsWith("en-gb")) return "webbe";
|
||||
if (normalized.startsWith("en")) return "kjv";
|
||||
if (normalized.startsWith("zh")) return "cuv";
|
||||
if (normalized.startsWith("cs")) return "bkr";
|
||||
if (normalized.startsWith("pt")) return "almeida";
|
||||
if (normalized.startsWith("ro")) return "rccv";
|
||||
return DEFAULT_TRANSLATION;
|
||||
};
|
||||
|
||||
const normalizeReference = (value = "") => {
|
||||
try {
|
||||
@@ -45,16 +62,29 @@ export const stripBibleTokens = (content = "") => {
|
||||
.trim();
|
||||
};
|
||||
|
||||
export const fetchBiblePassage = async (reference = "") => {
|
||||
export const fetchBibleReference = async (reference = "", locale = "") => {
|
||||
const safeReference = normalizeReference(reference);
|
||||
const preferredTranslation = getTranslationForLocale(locale);
|
||||
const preferredUrl = `https://bible-api.com/${encodeURIComponent(safeReference)}?translation=${preferredTranslation}`;
|
||||
const fallbackUrl = `https://bible-api.com/${encodeURIComponent(safeReference)}?translation=${DEFAULT_TRANSLATION}`;
|
||||
|
||||
let response = await fetch(preferredUrl);
|
||||
if (!response.ok && preferredTranslation !== DEFAULT_TRANSLATION) {
|
||||
response = await fetch(fallbackUrl);
|
||||
}
|
||||
if (!response.ok) throw new Error("Failed to load Bible passage");
|
||||
|
||||
const payload = await response.json();
|
||||
if (payload?.error) throw new Error(payload.error);
|
||||
return payload;
|
||||
};
|
||||
|
||||
export const fetchBiblePassage = async (reference = "", locale = "") => {
|
||||
const safeReference = normalizeReference(reference);
|
||||
if (!safeReference) {
|
||||
throw new Error("Missing Bible reference");
|
||||
}
|
||||
const response = await fetch(`https://bible-api.com/${encodeURIComponent(safeReference)}`);
|
||||
if (!response.ok) {
|
||||
throw new Error("Failed to load Bible passage");
|
||||
}
|
||||
const payload = await response.json();
|
||||
const payload = await fetchBibleReference(safeReference, locale);
|
||||
return {
|
||||
reference: payload?.reference || safeReference,
|
||||
text: (payload?.text || "").trim(),
|
||||
@@ -62,6 +92,11 @@ export const fetchBiblePassage = async (reference = "") => {
|
||||
};
|
||||
};
|
||||
|
||||
export const fetchBibleChapter = async (chapterReference = "", locale = "") => {
|
||||
const safeReference = normalizeReference(chapterReference);
|
||||
return fetchBibleReference(safeReference, locale);
|
||||
};
|
||||
|
||||
export const parseBibleReference = (reference = "") => {
|
||||
const normalized = normalizeReference(reference);
|
||||
const match = normalized.match(/^(.*)\s+(\d+)(?::(\d+)(?:-\d+)?)?$/);
|
||||
|
||||
Reference in New Issue
Block a user