Implement localization

This commit is contained in:
jultty 2024-05-20 10:53:26 -03:00
commit 506c0203d9
12 changed files with 218 additions and 36 deletions

View file

@ -1,11 +1,23 @@
function get_data() {
return {
posts: [
{
title: "Scripts em OCaml",
slug: "scripts-em-ocaml",
},
]
pt: {
posts: [
{
title: "Scripts em OCaml",
slug: "scripts-em-ocaml",
date: "23/03/2024",
},
]
},
en: {
posts: [
{
title: "Hello, Worldling",
slug: "hello-worldling",
date: "May 20, 2024",
},
]
}
}
}

View file

@ -1,13 +1,26 @@
const data = get_data()
const post_list = document.getElementById("posts")
const post_list = document.getElementById('posts')
document.addEventListener("DOMContentLoaded", function() {
data.posts.forEach(post => {
const item = document.createElement("li")
const anchor = document.createElement("a")
const userLang = navigator.language || navigator.userLanguage
let lang = userLang.includes('pt') ? 'pt' : 'en'
document.addEventListener('DOMContentLoaded', function() {
data[lang].posts.forEach(post => {
const item = document.createElement('li')
const anchor = document.createElement('a')
const date = document.createElement('span')
anchor.innerText = post.title
anchor.href = 'posts/' + post.slug + '.html'
date.innerText = `${parse_date(post.date)} - `
item.appendChild(date)
item.appendChild(anchor)
post_list.appendChild(item)
})
})
function parse_date(date) {
current_year = new Date().getFullYear()
return date.replace(`, ${current_year}`, '')
.replace(`/${current_year}`, '')
}

9
assets/js/post-l10n.js Normal file
View file

@ -0,0 +1,9 @@
const userLang = navigator.language || navigator.userLanguage
let lang = userLang.includes('pt') ? 'pt' : 'en'
console.log('loaded post-l10n.js', lang)
if (lang != 'pt') {
const footer_back = document.getElementById('footer-back-link')
console.log(footer_back)
footer_back.innerText = 'Back'
}