Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added useTranslate docs and getVehicleModelInfo func to useVehicle #155

Merged
merged 1 commit into from
Oct 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions docs/shared/useTranslate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
---
order: 950
templating: false # to be able to use {{ variable }} in example.
---

# useTranslate

Translation module for the client, server and webview.

## Usage

Before using the translation module, you need to initialize translations for the language you want to use.

This file should be located at `<your_plugin>/translate/index.ts`.

```ts <your_plugin>/translate/index.ts
import { useTranslate } from '@Shared/translate';

const { setBulk } = useTranslate();

setBulk({
'en': {
'test': 'This is a test',
'test_with_variable': 'This is a test with a variable: {{ variable }}',
},
'de': {
'test': 'Dies ist ein Test',
'test_with_variable': 'Dies ist ein Test mit einer Variable: {{ variable }}',
},
});
```

After that, you can use the `useTranslate` function to get the translation for the language you want to use.

!!!warning Important note
The file you just created should be imported in place you want to use translations.
!!!


```ts <your_plugin>/server/index.ts
import { useTranslate } from '@Shared/translate';
import '../translate'; // Import the translation file you created earlier.


const { t } = useTranslate('de'); // It is 'en' by default if no language is provided.

console.log(t('test'));
// Will log 'Dies ist ein Test'

console.log(t('test_with_variable', { variable: 'test' }));
// Will log 'Dies ist ein Test mit einer Variable: test'
```
33 changes: 33 additions & 0 deletions docs/useRebar/useVehicle.md
Original file line number Diff line number Diff line change
Expand Up @@ -238,3 +238,36 @@ Toggle the lock as a player and check permission for vehicle.
```ts
rVehicle.toggleLockAsPlayer(player);
```

### getVehicleModelInfo

Get the model info of the vehicle

```ts
const modelInfo: alt.IVehicleModel = rVehicle.getVehicleModelInfo();
```

It will return the model info of the vehicle with the following interface:

```ts
export interface IVehicleModel {
readonly modelHash: number;
readonly title: string;
readonly type: shared.ModelType;
readonly wheelsCount: number;
readonly hasArmoredWindows: boolean;
readonly primaryColor: number;
readonly secondaryColor: number;
readonly pearlColor: number;
readonly wheelsColor: number;
readonly interiorColor: number;
readonly dashboardColor: number;
readonly hasAutoAttachTrailer: boolean;
readonly availableModkits: readonly boolean[];
hasExtra(extraId: number): boolean;
hasDefaultExtra(extraId: number): boolean;
readonly bones: readonly IBoneInfo[];
readonly canAttachCars: boolean;
readonly handlingNameHash: number;
}
```
10 changes: 10 additions & 0 deletions src/main/server/vehicle/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,15 @@ export function useVehicle(vehicle: alt.Vehicle) {
return Utility.vehicleHashes.getNameFromHash(vehicle.model);
}

/**
* Get the model info of the vehicle
*
* @returns {alt.IVehicleModel}
*/
function getVehicleModelInfo(): alt.IVehicleModel {
return alt.getVehicleModelInfoByHash(vehicle.model);
}

/**
* Set the RPM for the vehicle
*
Expand All @@ -524,6 +533,7 @@ export function useVehicle(vehicle: alt.Vehicle) {
bind,
create,
getVehicleModelName,
getVehicleModelInfo,
handling: useVehicleHandling(vehicle),
hasOwner,
isBound,
Expand Down
15 changes: 12 additions & 3 deletions src/main/shared/translate.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
const translations = {};

type TranslationContext = Record<string, string | number>;

export function useTranslate(lang: string = 'en') {
function replaceVariables(text: string, vars: { [key: string]: string }): string {
return text.replace(/{{\s*([^}]+)\s*}}/g, (_, key) => vars[key.trim()] || '');
/**
* Replace variables in the translation text
*
* @param {string} text The template text with variables {{ var }}
* @param {TranslationContext} vars The variables to replace in the text
* @returns {string} The text with the variables replaced
*/
function replaceVariables(text: string, vars: TranslationContext): string {
return text.replace(/{{\s*([^}]+)\s*}}/g, (_, key) => vars[key.trim()]?.toString() || '');
}

/**
Expand All @@ -12,7 +21,7 @@ export function useTranslate(lang: string = 'en') {
* @param context
* @return
*/
function t(key: string, context?: Record<string, any>) {
function t(key: string, context?: TranslationContext) {
if (!translations[lang]) {
return `${key} has no translation for '${lang}'`;
}
Expand Down