Skip to content

Commit

Permalink
add language: english and vietnamese
Browse files Browse the repository at this point in the history
  • Loading branch information
NhanPT committed Nov 19, 2022
1 parent 795bfd0 commit a4999fb
Show file tree
Hide file tree
Showing 15 changed files with 432 additions and 113 deletions.
73 changes: 73 additions & 0 deletions assets/js/alpinejs-i18n.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// packages/main/src/index.ts
var localeChange = new Event("alpine-i18n:locale-change");
var i18nReady = new Event("alpine-i18n:ready");
var AlpineI18n = {
version: "2.3.0",
set locale(name) {
this.checkLocale(name);
this.currentLocale = name;
document.dispatchEvent(localeChange);
},
get locale() {
return this.currentLocale;
},
currentLocale: "",
messages: {},
fallbackLocale: "",
options: {},
create(locale, messages, options) {
this.messages = messages;
this.checkLocale(locale);
this.locale = locale;
this.options = options;
},
checkLocale(locale) {
if (!Object.keys(this.messages).includes(locale)) {
throw new Error(`Alpine I18n: The locale ${this.locale} does not exist.`);
}
},
t(name, vars) {
let message = "";
try {
message = name.split(".").reduce((o, i) => o[i], this.messages[this.locale]);
if (!message)
throw "";
} catch {
}
if (!message && this.fallbackLocale.length) {
message = name.split(".").reduce((o, i) => o[i], this.messages[this.fallbackLocale]);
} else if (!message) {
return this.options?.debug ? `???${name}` : name;
}
for (const key in vars) {
if (Object.prototype.hasOwnProperty.call(vars, key)) {
const val = vars[key];
let regexp = new RegExp("{s*(" + key + ")s*}", "g");
message = message.replaceAll(regexp, val);
}
}
return this.options?.debug ? `[${message}]` : message;
}
};
function src_default(Alpine) {
window.AlpineI18n = Alpine.reactive(AlpineI18n);
document.dispatchEvent(i18nReady);
Alpine.magic("locale", (el) => {
return (locale) => {
if (!locale)
return window.AlpineI18n.locale;
window.AlpineI18n.locale = locale;
};
});
Alpine.magic("t", (el) => {
return (name, vars) => {
return window.AlpineI18n.t(name, vars);
};
});
}

// packages/main/builds/module.js
var module_default = src_default;
export {
module_default as default
};
21 changes: 15 additions & 6 deletions assets/js/mountain.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ function showToast(message, title) {
toastContainer.innerHTML += `
<div id="t${id}" class="toast fade show" role="alert" aria-live="assertive" aria-atomic="true">
<div class="toast-header">
<strong class="me-auto">${title ?? 'Something went wrong'}</strong>
<strong class="me-auto">${title ?? $t(toast.title)}</strong>
<button type="button" class="btn-close" data-bs-dismiss="toast" aria-label="Close"></button>
</div>
<div class="toast-body">${message}</div>
Expand Down Expand Up @@ -182,7 +182,7 @@ function goto(path) {
})
}
if (breadcrumbs.length > 0)
breadcrumbs[0].name = `<i class="fa fa-solid fa-home"></i> ${breadcrumbs[0].name}`
breadcrumbs[0].name = `${$t('files.icons.home')} ${breadcrumbs[0].name}`
this.breadcrumbs = breadcrumbs
})
.finally(() => {
Expand Down Expand Up @@ -281,7 +281,7 @@ function upload() {
if (result.ok)
setTimeout(async () => showToast(await result.text(), "Upload completed"), 10);
else
setTimeout(() => showToast("Some files were not uploaded."), 10);
setTimeout(() => showToast($t('toast.error.upload')), 10);
this.goto(this.path)
})
};
Expand Down Expand Up @@ -325,7 +325,7 @@ function createFile() {
if (!newFileName || !newFileContent) return

if (newFileName.value.trim().length <= 0) {
showToast("File name can't be empty")
showToast($t('toast.error.fileNameEmpty'))
return
}

Expand All @@ -350,7 +350,7 @@ function createFolder() {
if (!newFolderInput) return
const text = newFolderInput.value
if (text.length <= 0) {
showToast("Folder name can't be empty")
showToast($t('toast.error.folderNameEmpty'))
return
}

Expand Down Expand Up @@ -444,4 +444,13 @@ function startInstance() {
// Reload page on go back
window.onhashchange = () => {
if (getHashPath() !== currentPath) window.location.reload();
};
}

// AlpineJS i18n
document.addEventListener('alpine-i18n:ready', async function () {
const en = await get('/assets/languages/en.json')
const choosen = await get('/assets/languages/vi.json')

window.AlpineI18n.create('en', { ...en, ...choosen });
window.AlpineI18n.fallbackLocale = 'en';
});
105 changes: 105 additions & 0 deletions assets/languages/en.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
{
"en": {
"header": {
"sort": {
"title": "Sort",
"name": "Name",
"date": "Date modified",
"type": "Type",
"asc": "Ascending",
"des": "Descending",
"group": "Group by type"
},
"view": {
"title": "View",
"list": "List",
"tiles": "Tiles",
"navigation": "Navigation panel",
"preview": "Preview panel",
"extension": "Name extension"
},
"select": {
"title": "Select",
"all": "Select all",
"none": "Select none",
"invert": "Invert selected"
},
"about": {
"title": "About",
"shortcuts": "Shortcuts",
"documentation": "Documentation",
"premium": "Premium",
"update": "Check for update"
}
},
"search": {
"title": "Search",
"placeholder": "Looking for..."
},
"controls": {
"download": "Download",
"rename": "Rename",
"copy": "Copy",
"move": "Move",
"delete": "Delete",
"upload": "Upload",
"create": "Create new ...",
"newFolder": "📁 Folder",
"newFile": "📄 Text file"
},
"files": {
"icons": {
"file": "📄",
"folder": "📁",
"home": "<i class='fa fa-solid fa-home'></i>"
},
"empty": "This folder is empty"
},
"toast": {
"title": "Something went wrong",
"error": {
"upload": "Some files were not uploaded.",
"fileNameEmpty": "File name can't be empty",
"folderNameEmpty": "Folder name can't be empty"
}
},
"modals": {
"close": "Close",
"delete": {
"title": "Delete files",
"description": "Those files will be deleted and can't be restore, are you really REALLY sure?",
"submit": "Delete"
},
"newFile": {
"title": "Create new file",
"name": "File name",
"namePlaceholder": "File name...",
"content": "Content",
"contentPlaceholder": "Some text...",
"submit": "Create"
},
"newFolder": {
"title": "Create new folder",
"namePlaceholder": "Folder name or path...",
"submit": "Create"
},
"rename": {
"title": "Rename files",
"submit": "Rename"
},
"selectDestination": {
"title": "Choose destination",
"copy": "Copy",
"move": "Move"
},
"premium": {
"title": "Mountain premium 👑",
"description": "",
"submit": "Buy Premium"
},
"shortcuts": {
"operations": "Operations"
}
}
}
}
105 changes: 105 additions & 0 deletions assets/languages/vi.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
{
"vi": {
"header": {
"sort": {
"title": "Sắp xếp",
"name": "Tên",
"date": "Ngày thay đổi",
"type": "Dạng file",
"asc": "Tăng dần",
"des": "Giảm dần",
"group": "Nhóm theo dạng file"
},
"view": {
"title": "Hiển thị",
"list": "Dạng dọc",
"tiles": "Dạng lưới",
"navigation": "Bảng điều hướng",
"preview": "Bảng xem nhanh",
"extension": "Định dạng file"
},
"select": {
"title": "Chọn",
"all": "Chọn tất cả",
"none": "Bỏ chọn tất cả",
"invert": "Đảo ngược lựa chọn"
},
"about": {
"title": "Giới thiệu",
"shortcuts": "Phím tắt",
"documentation": "Hướng dẫn sử dụng",
"premium": "Bản cao cấp",
"update": "Kiểm tra bản mới"
}
},
"search": {
"title": "Tìm kiếm",
"placeholder": "Nhập thông tin muốn tìm..."
},
"controls": {
"download": "Tải về",
"rename": "Đổi tên",
"copy": "Sao chép",
"move": "Di chuyển",
"delete": "Xoá",
"upload": "Tải lên",
"create": "Tạo mới ...",
"newFolder": "📁 Thư mục",
"newFile": "📄 File"
},
"files": {
"icons": {
"file": "📄",
"folder": "📁",
"home": "<i class='fa fa-solid fa-home'></i>"
},
"empty": "Thư mục này trống"
},
"toast": {
"title": "Có lỗi xảy ra",
"error": {
"upload": "Một số file tải lên không thành công",
"fileNameEmpty": "Tên file không thể để trống",
"folderNameEmpty": "Tên thư mục không thể để trống"
}
},
"modals": {
"close": "Đóng",
"delete": {
"title": "Xoá dữ liệu",
"description": "Những dữ liệu này sẽ bị xoá vĩnh viễn, bạn có chắn chắn không homie?",
"submit": "Xoá"
},
"newFile": {
"title": "Tạo file mới",
"name": "Tên file",
"namePlaceholder": "abc.txt",
"content": "Nội dung",
"contentPlaceholder": "Một con vịt xoè ra hai cái cánh...",
"submit": "Tạo"
},
"newFolder": {
"title": "Tạo thư mục mới",
"namePlaceholder": "Tên thư mục hoặc đường dẫn...",
"submit": "Tạo"
},
"rename": {
"title": "Đổi tên",
"submit": "Đổi tên"
},
"selectDestination": {
"title": "Chọn điểm đến",
"copy": "Sao chép",
"move": "Di chuyển"
},
"premium": {
"title": "Bản cao cấp 👑",
"description": "",
"submit": "Mua ngay"
},
"shortcuts": {
"operations": "Xử lý"
}
}
}
}
15 changes: 9 additions & 6 deletions templates/modals/delete.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,26 @@
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Delete files</h5>
<h5 class="modal-title" x-text="$t('modals.delete.title')"></h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<div class="row fs-6 m-1">Those files will be deleted and can't be restore, are you really REALLY sure?
</div>
<div class="row fs-6 m-1" x-text="$t('modals.delete.description')"></div>
<div class="row">
<template x-for="file in files.filter(f=>f.selected)">
<div class="col-6 mt-3">
<div x-text="`${file.directory ? '📁' : '📄'} ${file.name}`"></div>
<div
x-text="`${$t(file.directory ? 'files.icons.folder' : 'files.icons.file')} ${file.name}`">
</div>
</div>
</template>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
<button type="submit" class="btn btn-danger" @click="deleteSelected()">Delete</button>
<button type="button" x-text="$t('modals.close')" class="btn btn-secondary"
data-bs-dismiss="modal"></button>
<button type="submit" x-text="$t('modals.delete.submit')" class="btn btn-danger"
@click="deleteSelected()"></button>
</div>
</div>
</div>
Expand Down
Loading

0 comments on commit a4999fb

Please sign in to comment.