# Localization

NOVA Framework has built-in multi-language support. Currently supports **Portuguese (PT)** and **English (EN)**.

## Core Localization

### Using Translations

In any core module or script that depends on nova\_core:

```lua
-- Server-side or shared (inside nova_core)
local text = _L('player_loaded')
local formatted = _L('admin_give_item', 'bread', 5, 'PlayerName')
```

### Adding Core Translations

**Portuguese:** `nova_core/config/locales/pt.lua`

```lua
NovaLocale.RegisterLocale('pt', {
    ['my_key'] = 'Minha tradução',
    ['welcome_msg'] = 'Bem-vindo ao servidor, %s!',
})
```

**English:** `nova_core/config/locales/en.lua`

```lua
NovaLocale.RegisterLocale('en', {
    ['my_key'] = 'My translation',
    ['welcome_msg'] = 'Welcome to the server, %s!',
})
```

### Setting the Language

In `nova_core/config/main.lua`:

```lua
Config.Locale = 'pt'  -- or 'en'
```

## Script Localization

Each launch script has its own `locales.lua` file with a self-contained system.

### Structure

```lua
-- nova_bank/locales.lua

local Locales = {
    ['pt'] = {
        ['not_enough_cash'] = 'Dinheiro insuficiente!',
        ['deposit_success'] = 'Depósito de $%s realizado!',
        -- NUI labels
        ['nui_deposit'] = 'Depositar',
        ['nui_withdraw'] = 'Levantar',
    },
    ['en'] = {
        ['not_enough_cash'] = 'Not enough cash!',
        ['deposit_success'] = '$%s deposited successfully!',
        -- NUI labels
        ['nui_deposit'] = 'Deposit',
        ['nui_withdraw'] = 'Withdraw',
    },
}

local currentLocale = nil

local function getLocale()
    if not currentLocale then
        local ok, cfg = pcall(function()
            return exports['nova_core']:GetConfig()
        end)
        currentLocale = (ok and cfg and cfg.Locale) or 'pt'
    end
    return currentLocale
end

function BankL(key, ...)
    local lang = getLocale()
    local str = (Locales[lang] and Locales[lang][key])
        or (Locales['pt'] and Locales['pt'][key])
        or key
    if select('#', ...) > 0 then
        return string.format(str, ...)
    end
    return str
end

function BankGetAllStrings()
    local lang = getLocale()
    return Locales[lang] or Locales['pt'] or {}
end
```

### Using in Lua

```lua
-- In server or client scripts
local msg = BankL('not_enough_cash')
local msg2 = BankL('deposit_success', 5000)
```

### Using in NUI (JavaScript)

Pass all strings to the NUI when opening:

```lua
-- client/main.lua
SendNUIMessage({
    action = 'open',
    locale = BankGetAllStrings(),
    -- other data...
})
```

Then apply in JavaScript:

```javascript
var locale = {};

window.addEventListener('message', function(event) {
    var d = event.data;
    if (d.action === 'open') {
        locale = d.locale || {};
        applyLocale();
    }
});

function applyLocale() {
    var map = {
        'i18n-deposit': 'nui_deposit',
        'i18n-withdraw': 'nui_withdraw',
    };
    for (var id in map) {
        var el = document.getElementById(id);
        if (el && locale[map[id]]) {
            el.textContent = locale[map[id]];
        }
    }
}
```

## Adding a New Language

1. Add translations to `nova_core/config/locales/` (e.g., `es.lua` for Spanish)
2. Add translations to each script's `locales.lua`
3. Set `Config.Locale = 'es'` in the core config

```lua
-- nova_core/config/locales/es.lua
NovaLocale.RegisterLocale('es', {
    ['player_loaded'] = '¡Personaje cargado!',
    -- ... all keys
})
```
