Skip to content
This repository has been archived by the owner on Jan 13, 2025. It is now read-only.

Commit

Permalink
Merge pull request #3 from Maximus7474/locale-integration
Browse files Browse the repository at this point in the history
Locale integration
  • Loading branch information
Maximus7474 authored Dec 29, 2024
2 parents 73500aa + abba0e5 commit e86203d
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 9 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,8 @@ It is highly recommended to alter the scripts creating the calls/dispatch events
```bash
# Set the default/fallback time (in seconds) for dispatch messages
setr lb-tablet-dispatch:default-time 100

# Set the default locale for the various translations
# Current locales: en
setr lb-tablet-dispatch:locale en
```
10 changes: 10 additions & 0 deletions locales/en.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"DEFAULT": {
"TITLE": "Dispatch Notification",
"LOCATION_LABEL": "Call Origin"
},
"RCORE": {
"SHOP_ROBBERY": "Shop Robbery",
"CAR_ROBBERY": "Car theft"
}
}
4 changes: 2 additions & 2 deletions server/ps-dispatch.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ RegisterNetEvent('ps-dispatch:server:notify', function (data)
exports["lb-tablet"]:AddDispatch({
priority = priority,
code = data?.code or '',
title = data?.codename or 'Dispatch Notification',
title = data?.codename or T("DEFAULT.TITLE"),
description = data.message or '',
location = {
label = data?.codename or "Call Origin",
label = data?.codename or T("DEFAULT.LOCATION_LABEL"),
coords = vector2(data?.coords.x or 0, data?.coords.y or 0)
},
time = CONFIG['default-time'],
Expand Down
4 changes: 2 additions & 2 deletions server/qs-dispatch.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ RegisterNetEvent('qs-dispatch:server:CreateDispatchCall', function (data)
exports["lb-tablet"]:AddDispatch({
priority = priority,
code = data?.callCode.code or '',
title = data?.callCode.snippet or 'Dispatch Notification',
title = data?.callCode.snippet or T("DEFAULT.TITLE"),
description = data.message or '',
location = {
label = data?.callCode.snippet or "Call Origin",
label = data?.callCode.snippet or T("DEFAULT.LOCATION_LABEL"),
coords = vector2(data?.callLocation.x or 0, data?.callLocation.y or 0)
},
time = data?.blip.time and math.floor(data.blip.time / 1000) or CONFIG['default-time'],
Expand Down
10 changes: 5 additions & 5 deletions server/rcore-dispatch.lua
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
RegisterNetEvent('rcore_dispatch:server:sendAlert', function(data)
local callTitles = {
default = "Dispatch Notification",
shop_robbery = "Shop Robbery",
car_robbery = "Car Robbery",
default = "DEFAULT.TITLE",
shop_robbery = "RCORE.SHOP_ROBBERY",
car_robbery = "RCORE.CAR_ROBBERY",
}
local type = callTitles[data?.type or "default"] or callTitles.default

exports["lb-tablet"]:AddDispatch({
priority = data.default_priority,
code = data?.code or '',
title = type,
title = T(type),
description = data.text or '',
location = {
label = data?.blip.text or "Call Origin",
label = data?.blip.text or T('DEFAULT.LOCATION_LABEL'),
coords = vector2(data?.coords.x or 0, data?.coords.y or 0)
},
time = CONFIG['default-time'],
Expand Down
1 change: 1 addition & 0 deletions utils/convars.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
CONFIG = {
['default-time'] = GetConvarInt('lb-tablet-dispatch:default-time', 100),
['locale'] = GetConvar('lb-tablet-dispatch:locale', 'en')
}
35 changes: 35 additions & 0 deletions utils/localeHandler.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
local locale = {}

local localeKey = CONFIG['locale']

local rawContent = LoadResourceFile(GetCurrentResourceName(), ('locales/%s.json'):format(localeKey))

if not rawContent then
lib.print.warn(('Unable to load %s locale as it doesn\'t exist in @%s/locales/'):format(localeKey, GetCurrentResourceName()))
lib.print.warn('Falling back to english locale.')
rawContent = LoadResourceFile(GetCurrentResourceName(), 'locales/en.json')
end

locale = json.decode(rawContent)[IsDuplicityVersion() and 'SERVER' or 'CLIENT']

function T(key, args)
local keys = {}
for str in string.gmatch(key, "([^%.]+)") do
keys[#keys+1] = str
end

local localizedString = locale
for _, keyPart in ipairs(keys) do
localizedString = localizedString[keyPart]
if localizedString == nil then
return key
end
end

local formatedString = localizedString:gsub("%b{}", function(match)
local key = match:sub(2, -2)
return args[key] or ""
end)

return formatedString
end

0 comments on commit e86203d

Please sign in to comment.