言語切替 |
日本語と英語の切り替えサンプル |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
const story = {
ja: {
d1: `
こんにちは
`,
d2: `
おや、どうしたんだい?
`,
d3: `
お腹へっちゃって
`,
d4: `
野菜はどう?
`,
d5: `
それは、ちょっと
`,
d6: `
あぁ、そういえばこれ拾ったんだった。<br>
(どんぐりを差し出す)
`,
d7: `
いいの?<br>
ありがとう
`,
d8: `
どういたしまして<br>
(こうしてくまさん森へ帰っていきました。)
`
},
en: {
d1: `
Hello
`,
d2: `
Hey, what's wrong?
`,
d3: `
I'm hungry
`,
d4: `
What about vegetables?
`,
d5: `
That's a bit
`,
d6: `
Ah, that reminds me, I picked this up. <br>
(Hands over the acorn)
`,
d7: `
Is that okay?<br>
Thank you
`,
d8: `
You're welcome.<br>
(And so the bear returned to the forest.)
`
}
};
const text = {
ja: {
n1:"戻る",
n2:"言語切替",
n3:"日本語と英語の切り替えサンプル",
n4:"[サンプル]",
n5:"戻る",
},
en: {
n1:"back",
n2:"change language",
n3:"change japanese and english sample",
n4:"[sample]",
n5:"back",
}
};
// まとめて反映したいとき
function applyStoryAll(lang, divIds) {
divIds.forEach((id) => applyStory(lang, id));
}
function applyTextAll(lang, divIds) {
divIds.forEach((id) => applyText(lang, id));
}
// (1) 取得:langとdivのidを渡してHTML文字列を返す
function getStory(lang, divId) {
// 言語がなければ日本語にフォールバック、該当idがなければ空文字
return (story[lang] && story[lang][divId]) ?? (story.ja && story.ja[divId]) ?? '';
}
// (2) 反映:langとdivのidを渡してDOMに流し込む
function applyStory(lang, divId) {
const el = document.getElementById(divId);
if (!el) return;
el.innerHTML = getStory(lang, divId);
}
// (1) 取得:langとdivのidを渡してHTML文字列を返す
function getText(lang, divId) {
// 言語がなければ日本語にフォールバック、該当idがなければ空文字
return (text[lang] && text[lang][divId]) ?? (text.ja && text.ja[divId]) ?? '';
}
// (2) 反映:langとdivのidを渡してDOMに流し込む
function applyText(lang, divId) {
const el = document.getElementById(divId);
if (!el) return;
el.innerText = getText(lang, divId);
}
//ストーリー
const ary = ["d1", "d2", "d3", "d4", "d5", "d6", "d7", "d8"];
//各説明
const ary2 = ["n1", "n2", "n3", "n4", "n5"];
const story = {
ja: {
d1: `
こんにちは
`,
d2: `
おや、どうしたんだい?
`,
d3: `
お腹へっちゃって
`,
d4: `
野菜はどう?
`,
d5: `
それは、ちょっと
`,
d6: `
あぁ、そういえばこれ拾ったんだった。<br>
(どんぐりを差し出す)
`,
d7: `
いいの?<br>
ありがとう
`,
d8: `
どういたしまして<br>
(こうしてくまさん森へ帰っていきました。)
`
},
en: {
d1: `
Hello
`,
d2: `
Hey, what's wrong?
`,
d3: `
I'm hungry
`,
d4: `
What about vegetables?
`,
d5: `
That's a bit
`,
d6: `
Ah, that reminds me, I picked this up. <br>
(Hands over the acorn)
`,
d7: `
Is that okay?<br>
Thank you
`,
d8: `
You're welcome.<br>
(And so the bear returned to the forest.)
`
}
};
const text = {
ja: {
n1:"戻る",
n2:"言語切替",
n3:"日本語と英語の切り替えサンプル",
n4:"[サンプル]",
v5:"戻る",
},
en: {
n1:"back",
n2:"change language",
n3:"change japanese and english sample",
n4:"[sample]",
n5:"back",
}
};
// When you want to reflect all at once
function applyStoryAll(lang, divIds) {
divIds.forEach((id) => applyStory(lang, id));
}
function applyTextAll(lang, divIds) {
divIds.forEach((id) => applyText(lang, id));
}
// (1) Get: Pass lang and div id to return HTML string
function getStory(lang, divId) {
// If no language is available, it will fall back to Japanese. If no corresponding ID is available, it will be empty.
return (story[lang] && story[lang][divId]) ?? (story.ja && story.ja[divId]) ?? '';
}
// (2) Reflect: Pass lang and div id and insert into DOM
function applyStory(lang, divId) {
const el = document.getElementById(divId);
if (!el) return;
el.innerHTML = getStory(lang, divId);
}
// (1) Get: Pass lang and div id to return HTML string
function getText(lang, divId) {
// If no language is available, it will fall back to Japanese. If no corresponding ID is available, it will be empty.
return (text[lang] && text[lang][divId]) ?? (text.ja && text.ja[divId]) ?? '';
}
// (2) Reflect: Pass lang and div id and insert into DOM
function applyText(lang, divId) {
const el = document.getElementById(divId);
if (!el) return;
el.innerText = getText(lang, divId);
}
//story
const ary = ["d1", "d2", "d3", "d4", "d5", "d6", "d7", "d8"];
//Each explanation
const ary2 = ["n1", "n2", "n3", "n4", "n5"];
const story = {
ja: {
d1: `
こんにちは
`,
d2: `
おや、どうしたんだい?
`,
...
},
en: {
d1: `
Hello
`,
...
`,
d8: `
You're welcome.<br>
(And so the bear returned to the forest.)
`
}
};
function getStory(lang, divId) {
// 言語がなければ日本語にフォールバック、該当idがなければ空文字
return (story[lang] && story[lang][divId]) ?? (story.ja && story.ja[divId]) ?? '';
}
function applyStory(lang, divId) {
const el = document.getElementById(divId);
if (!el) return;
el.innerHTML = getStory(lang, divId);
}
function getStory(lang, divId) {
// If no language is available, it will fall back to Japanese. If no corresponding ID is available, it will be empty.
return (story[lang] && story[lang][divId]) ?? (story.ja && story.ja[divId]) ?? '';
}
function applyStory(lang, divId) {
const el = document.getElementById(divId);
if (!el) return;
el.innerHTML = getStory(lang, divId);
}
| [URL]イラスト:農民イラスト | |
| URL[https://nawmin.stores.jp][クリックすると開きます]) |
| [URL]illustration by nawmin.com | |
| URL[https://nawmin.stores.jp][click to open the homepage]) |
| ホームページおよプリ等に掲載されている情報等については、いかなる保障もいたしません。 ホームページおよびアプリ等を通じて入手したいかなる情報も複製、販売、出版または使用させたり、 または公開したりすることはできません。 当方は、ホームペーよびアプリ利用したいかなる理由によっての障害等が発生しても、 その結果ホームページおよびアプリ等を利用された本人または他の第三者が被った損害について 一切の責任を負わないものとします。 |