From 131e1dd6856b992779857ea32d8a40320ab3dc42 Mon Sep 17 00:00:00 2001 From: James Lyne Date: Thu, 20 May 2021 16:13:25 +0100 Subject: [PATCH] Make LiveAtlas messages translatable --- index.html | 52 ++++++++++++++--------- src/api.ts | 39 ++++++++++------- src/components/ChatBox.vue | 32 ++++++++------ src/components/chat/ChatMessage.vue | 8 ++-- src/components/map/layer/PlayersLayer.vue | 4 +- src/components/sidebar/PlayerList.vue | 2 +- src/components/sidebar/ServerList.vue | 4 +- src/components/sidebar/WorldList.vue | 2 +- src/dynmap.d.ts | 15 +------ src/index.d.ts | 19 +++++++++ src/store/mutations.ts | 7 ++- src/store/state.ts | 28 ++++++------ 12 files changed, 124 insertions(+), 88 deletions(-) diff --git a/index.html b/index.html index 632819a..6e95f3d 100644 --- a/index.html +++ b/index.html @@ -32,37 +32,37 @@ creative: { label: 'Creative', dynmap: { - configuration: 'http://dynmap.local/standalone/creative/MySQL_configuration.php', - update: 'http://dynmap.local/standalone/creative/MySQL_update.php?world={world}&ts={timestamp}', - sendmessage: 'http://dynmap.local/standalone/creative/MySQL_sendmessage.php', - login: 'http://dynmap.local/standalone/creative/MySQL_login.php', - register: 'http://dynmap.local/standalone/creative/MySQL_register.php', - tiles: 'http://dynmap.local/standalone/creative/MySQL_tiles.php?tile=', - markers: 'http://dynmap.local/standalone/creative/MySQL_markers.php?marker=' + configuration: 'http://dynmap.local/creative/MySQL_configuration.php', + update: 'http://dynmap.local/creative/MySQL_update.php?world={world}&ts={timestamp}', + sendmessage: 'http://dynmap.local/creative/MySQL_sendmessage.php', + login: 'http://dynmap.local/creative/MySQL_login.php', + register: 'http://dynmap.local/creative/MySQL_register.php', + tiles: 'http://dynmap.local/creative/MySQL_tiles.php?tile=', + markers: 'http://dynmap.local/creative/MySQL_markers.php?marker=' } }, survival: { label: 'Survival', dynmap: { - configuration: 'http://dynmap.local/standalone/survival/MySQL_configuration.php', - update: 'http://dynmap.local/standalone/survival/MySQL_update.php?world={world}&ts={timestamp}', - sendmessage: 'http://dynmap.local/standalone/survival/MySQL_sendmessage.php', - login: 'http://dynmap.local/standalone/survival/MySQL_login.php', - register: 'http://dynmap.local/standalone/survival/MySQL_register.php', - tiles: 'http://dynmap.local/standalone/survival/MySQL_tiles.php?tile=', - markers: 'http://dynmap.local/standalone/survival/MySQL_markers.php?marker=' + configuration: 'http://dynmap.local/survival/MySQL_configuration.php', + update: 'http://dynmap.local/survival/MySQL_update.php?world={world}&ts={timestamp}', + sendmessage: 'http://dynmap.local/survival/MySQL_sendmessage.php', + login: 'http://dynmap.local/survival/MySQL_login.php', + register: 'http://dynmap.local/survival/MySQL_register.php', + tiles: 'http://dynmap.local/survival/MySQL_tiles.php?tile=', + markers: 'http://dynmap.local/survival/MySQL_markers.php?marker=' } }, build: { label: 'Build', dynmap: { - configuration: 'http://dynmap.local/standalone/build/MySQL_configuration.php', - update: 'http://dynmap.local/standalone/build/MySQL_update.php?world={world}&ts={timestamp}', - sendmessage: 'http://dynmap.local/standalone/build/MySQL_sendmessage.php', - login: 'http://dynmap.local/standalone/build/MySQL_login.php', - register: 'http://dynmap.local/standalone/build/MySQL_register.php', - tiles: 'http://dynmap.local/standalone/build/MySQL_tiles.php?tile=', - markers: 'http://dynmap.local/standalone/build/MySQL_markers.php?marker=' + configuration: 'http://dynmap.local/build/MySQL_configuration.php', + update: 'http://dynmap.local/build/MySQL_update.php?world={world}&ts={timestamp}', + sendmessage: 'http://dynmap.local/build/MySQL_sendmessage.php', + login: 'http://dynmap.local/build/MySQL_login.php', + register: 'http://dynmap.local/build/MySQL_register.php', + tiles: 'http://dynmap.local/build/MySQL_tiles.php?tile=', + markers: 'http://dynmap.local/build/MySQL_markers.php?marker=' } }, test: { @@ -78,6 +78,16 @@ markers: 'http://dynmap.local:8123/tiles/' } }, + }, + + messages: { + headingServers: 'Servers', + chatNoMessages: 'No chat messages yet...', + chatLogin: 'Please {{link}} to send chat messages', + chatLoginLink: 'login', + chatSend: 'Send', + chatErrorUnknown: 'Unexpected error while sending chat message', + chatErrorDisabled: 'Chat is not enabled', } }; diff --git a/src/api.ts b/src/api.ts index cd1c8ca..1ff14bf 100644 --- a/src/api.ts +++ b/src/api.ts @@ -24,7 +24,6 @@ import { DynmapMarker, DynmapMarkerSet, DynmapMarkerSetUpdates, - DynmapMessageConfig, DynmapPlayer, DynmapServerConfig, DynmapTileUpdate, @@ -36,6 +35,7 @@ import { } from "@/dynmap"; import {useStore} from "@/store"; import ChatError from "@/errors/ChatError"; +import {LiveAtlasMessageConfig} from "@/index"; const titleColours = /ยง[0-9a-f]/ig; @@ -58,17 +58,26 @@ function buildServerConfig(response: any): DynmapServerConfig { }; } -function buildMessagesConfig(response: any): DynmapMessageConfig { +function buildMessagesConfig(response: any): LiveAtlasMessageConfig { + const liveAtlasMessages = window.liveAtlasConfig ? window.liveAtlasConfig.messages || {} : {}; + return { - chatNotAllowed: response['msg-chatnotallowed'] || '', - chatRequiresLogin: response['msg-chatrequireslogin'] || '', - chatCooldown: response.spammessage || '', - mapTypes: response['msg-maptypes'] || '', - players: response['msg-players'] || '', - playerJoin: response.joinmessage || '', - playerQuit: response.quitmessage || '', - anonymousJoin: response['msg-hiddennamejoin'] || '', - anonymousQuit: response['msg-hiddennamequit'] || '', + chatPlayerJoin: response.joinmessage || '', + chatPlayerQuit: response.quitmessage || '', + chatAnonymousJoin: response['msg-hiddennamejoin'] || '', + chatAnonymousQuit: response['msg-hiddennamequit'] || '', + chatLogin: liveAtlasMessages.chatLogin || '', + chatLoginLink: liveAtlasMessages.chatLoginLink || '', + chatNoMessages: liveAtlasMessages.chatNoMessages || '', + chatSend: liveAtlasMessages.chatSend || '', + chatErrorNotAllowed: response['msg-chatnotallowed'] || '', + chatErrorRequiresLogin: response['msg-chatrequireslogin'] || '', + chatErrorCooldown: response.spammessage || '', + chatErrorDisabled: liveAtlasMessages.chatErrorDisabled || '', + chatErrorUnknown: liveAtlasMessages.chatErrorUnknown || '', + headingWorlds: response['msg-maptypes'] || '', + headingPlayers: response['msg-players'] || '', + headingServers: liveAtlasMessages.headingServers || '', } } @@ -721,7 +730,7 @@ export default { const store = useStore(); if (!store.state.components.chatSending) { - return Promise.reject(new ChatError("Chat is not enabled")); + return Promise.reject(store.state.messages.chatErrorDisabled); } return fetch(useStore().getters.serverConfig.dynmap.sendmessage, { @@ -732,7 +741,7 @@ export default { }) }).then((response) => { if (response.status === 403) { //Rate limited - throw new ChatError(store.state.messages.chatCooldown + throw new ChatError(store.state.messages.chatErrorCooldown .replace('%interval%', store.state.components.chatSending!.cooldown.toString())); } @@ -743,11 +752,11 @@ export default { return response.json(); }).then(response => { if (response.error !== 'none') { - throw new ChatError(store.state.messages.chatNotAllowed); + throw new ChatError(store.state.messages.chatErrorNotAllowed); } }).catch(e => { if (!(e instanceof ChatError)) { - console.error('Unexpected error while sending chat message'); + console.error(store.state.messages.chatErrorUnknown); console.trace(e); } diff --git a/src/components/ChatBox.vue b/src/components/ChatBox.vue index a6612c4..98d7cbf 100644 --- a/src/components/ChatBox.vue +++ b/src/components/ChatBox.vue @@ -17,18 +17,16 @@ @@ -53,14 +51,14 @@ && !store.state.loggedIn; }), sendingEnabled = computed(() => store.state.components.chatSending && !loginRequired.value), - messageMaxLength = computed(() => store.state.components.chatSending?.maxLength), + maxMessageLength = computed(() => store.state.components.chatSending?.maxLength), chatInput = ref(null), enteredMessage = ref(""), sendingMessage = ref(false), sendingError = ref(null), - messages = computed(() => { + chatMessages = computed(() => { if(componentSettings.value!.messageHistory) { return store.state.chat.messages.slice(0, componentSettings.value!.messageHistory); } else { @@ -68,8 +66,13 @@ } }), + messageSend = computed(() => store.state.messages.chatSend), + messageNoMessages = computed(() => store.state.messages.chatNoMessages), + messageLogin = computed(() => store.state.messages.chatLogin.replace( + '{{link}}', store.state.messages.chatLoginLink)), + sendMessage = async () => { - const message = enteredMessage.value.trim().substring(0, messageMaxLength.value); + const message = enteredMessage.value.trim().substring(0, maxMessageLength.value); if(!message) { return; @@ -86,7 +89,7 @@ if(e instanceof ChatError) { sendingError.value = e.message; } else { - sendingError.value = `An unexpected error occurred. See console for details.`; + sendingError.value = store.state.messages.chatErrorUnknown; } } finally { sendingMessage.value = false; @@ -105,12 +108,15 @@ chatInput, enteredMessage, sendMessage, - messages, + chatMessages, loginRequired, sendingEnabled, sendingMessage, sendingError, - messageMaxLength + maxMessageLength, + messageLogin, + messageSend, + messageNoMessages, } } }) diff --git a/src/components/chat/ChatMessage.vue b/src/components/chat/ChatMessage.vue index 2e38b5d..e3e3590 100644 --- a/src/components/chat/ChatMessage.vue +++ b/src/components/chat/ChatMessage.vue @@ -47,17 +47,17 @@ return props.message.message; case 'playerjoin': if(props.message.playerName) { - return store.state.messages.playerJoin + return store.state.messages.chatPlayerJoin .replace('%playername%', props.message.playerName); } else { - return store.state.messages.anonymousJoin; + return store.state.messages.chatAnonymousJoin; } case 'playerleave': if(props.message.playerName) { - return store.state.messages.playerQuit + return store.state.messages.chatPlayerQuit .replace('%playername%', props.message.playerName); } else { - return store.state.messages.anonymousQuit; + return store.state.messages.chatAnonymousQuit; } } }) diff --git a/src/components/map/layer/PlayersLayer.vue b/src/components/map/layer/PlayersLayer.vue index 2a4afcf..3f2ebd1 100644 --- a/src/components/map/layer/PlayersLayer.vue +++ b/src/components/map/layer/PlayersLayer.vue @@ -61,12 +61,12 @@ export default defineComponent({ this.leaflet.getLayerManager().addLayer( this.layerGroup, true, - useStore().state.messages.players, + useStore().state.messages.headingPlayers, this.componentSettings!.layerPriority); } else { this.leaflet.getLayerManager().addHiddenLayer( this.layerGroup, - useStore().state.messages.players, + useStore().state.messages.headingPlayers, this.componentSettings!.layerPriority); } }, diff --git a/src/components/sidebar/PlayerList.vue b/src/components/sidebar/PlayerList.vue index 8ed143e..2e2cb56 100644 --- a/src/components/sidebar/PlayerList.vue +++ b/src/components/sidebar/PlayerList.vue @@ -38,7 +38,7 @@ export default defineComponent({ computed: { heading() { - return useStore().state.messages.players; + return useStore().state.messages.headingPlayers; }, players() { diff --git a/src/components/sidebar/ServerList.vue b/src/components/sidebar/ServerList.vue index 7b1d7f9..4aea11f 100644 --- a/src/components/sidebar/ServerList.vue +++ b/src/components/sidebar/ServerList.vue @@ -16,7 +16,7 @@