refactor(ui): break down config.html into smaller pieces (#2491)
Co-authored-by: ReenigneArcher <42013603+ReenigneArcher@users.noreply.github.com>
This commit is contained in:
80
src_assets/common/assets/web/platform-i18n.js
Normal file
80
src_assets/common/assets/web/platform-i18n.js
Normal file
@@ -0,0 +1,80 @@
|
||||
import {inject} from 'vue'
|
||||
|
||||
class PlatformMessageI18n {
|
||||
/**
|
||||
* @param {string} platform
|
||||
*/
|
||||
constructor(platform) {
|
||||
this.platform = platform
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} key
|
||||
* @param {string} platform identifier
|
||||
* @return {string} key with platform identifier
|
||||
*/
|
||||
getPlatformKey(key, platform) {
|
||||
return key + '_' + platform
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} key
|
||||
* @param {string?} defaultMsg
|
||||
* @return {string} translated message or defaultMsg if provided
|
||||
*/
|
||||
getMessageUsingPlatform(key, defaultMsg) {
|
||||
const realKey = this.getPlatformKey(key, this.platform)
|
||||
const i18n = inject('i18n')
|
||||
let message = i18n.t(realKey)
|
||||
|
||||
if (message !== realKey) {
|
||||
// We got a message back, return early
|
||||
return message
|
||||
}
|
||||
|
||||
// If on Windows, we don't fallback to unix, so return early
|
||||
if (this.platform === 'windows') {
|
||||
return defaultMsg ? defaultMsg : message
|
||||
}
|
||||
|
||||
// there's no message for key, check for unix version
|
||||
const unixKey = this.getPlatformKey(key, 'unix')
|
||||
message = i18n.t(unixKey)
|
||||
|
||||
if (message === unixKey && defaultMsg) {
|
||||
// there's no message for unix key, return defaultMsg
|
||||
return defaultMsg
|
||||
}
|
||||
return message
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string?} platform
|
||||
* @return {PlatformMessageI18n} instance
|
||||
*/
|
||||
export function usePlatformI18n(platform) {
|
||||
if (!platform) {
|
||||
platform = inject('platform').value
|
||||
}
|
||||
|
||||
if (!platform) {
|
||||
throw 'platform argument missing'
|
||||
}
|
||||
|
||||
return inject(
|
||||
'platformMessage',
|
||||
() => new PlatformMessageI18n(platform),
|
||||
true
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} key
|
||||
* @param {string?} defaultMsg
|
||||
* @return {string} translated message or defaultMsg if provided
|
||||
*/
|
||||
export function $tp(key, defaultMsg) {
|
||||
const pm = usePlatformI18n()
|
||||
return pm.getMessageUsingPlatform(key, defaultMsg)
|
||||
}
|
||||
Reference in New Issue
Block a user