Giter Site home page Giter Site logo

scavengesurvive / language Goto Github PK

View Code? Open in Web Editor NEW
3.0 2.0 3.0 33 KB

Provides a simple API for multi-language systems.

License: MIT License

Pawn 98.62% Makefile 1.38%
pawn-package multi-language-systems language sa-mp sa-mp-library pawn-library

language's Introduction

language.inc

Provides a simple API for multi-language systems.

Currently undergoing refactoring after being extracted from the SS core codebase. Not prod-ready.

Installation

Simply install to your project:

sampctl package install ScavengeSurvive/language

Include in your code and begin using the library:

#include <language>

Usage

This package works by loading simple INI files. One file exists for each language in scriptfiles/languages/ and there is no file extension. So you may have scriptfiles/languages/English and scriptfiles/languages/Espanol (Pawn has issues with special characters in filenames unfortunately).

These files should have the same keys, see the scriptfiles/languages directory in the repo for an example.

Loading Languages

To load a language from a file, use InitLanguageFromFile with just the language name as the parameter, not the full file path. For example InitLanguageFromFile("English"); loads the scriptfiles/languages/English file.

You could also combine this with the fsutil plugin and iterate through the languages directory:

new
    Directory:dir = OpenDir("scriptfiles/languages"),
    entry[256],
    ENTRY_TYPE:type;
while(DirNext(dir, type, entry)) {
    if(type == E_REGULAR) {
        InitLanguageFromFile(entry);
    }
}
CloseDir(dir);

Using Language Strings

Now you've loaded languages, you can use strings from each language with the GetLanguageString(languageid, key[], bool:encode = false) function. The first parameter is the language ID, which you can obtain via name with GetLanguageID.

For example:

SendClientMessage(playerid, -1, GetLanguageString(GetLanguageID("English"), "WELCOME"));

Would send the string from English keyed by WELCOME.

Per-Player Language

You can store a language ID for each player with SetPlayerLanguage and retrieve it with GetPlayerLanguage. For example, your server could display a list of languages in a dialog with GetLanguageList and when the player selects a language, call SetPlayerLanguage with their selection and then in future you can use GetPlayerLanguage to obtain the language ID that player selected.

There is also a useful macro @L(playerid, key[]) which you can use to quickly get a string for a player using their assigned language ID:

SendClientMessage(playerid, -1, @L(playerid, "WELCOME"));

Testing

To test, simply run the package:

sampctl package run

language's People

Contributors

mysy00 avatar southclaws avatar tr1xy avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.