Raised This Month: $ Target: $400
 0% 

How to adding translations function?


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
highcoder
Member
Join Date: Jun 2018
Old 06-04-2018 , 08:41   How to adding translations function?
Reply With Quote #1

Hello to everyone!

What should we do to translate the messages of the plugin into different languages? Shortly, how is multi-language function added to the plugin? Could you give an example using the following simple plugin?

Code:
#include <amxmodx>
#include <amxmisc>

#define PLUGIN "Sample Plugin"
#define VERSION "1.0"
#define AUTHOR "John Doe"


public plugin_init() {

	register_plugin(PLUGIN, VERSION, AUTHOR)
	register_clcmd("say", "sample")
	register_clcmd("say_team", "sample")
	
}

public sample(id) {

	client_print_color(id, id, "^4[INFO] ^3This is a sample text.")
	
}
highcoder is offline
Napoleon_be
Veteran Member
Join Date: Jul 2011
Location: Belgium
Old 06-04-2018 , 14:16   Re: How to adding translations function?
Reply With Quote #2

source: https://wiki.alliedmods.net/Advanced...ng_(AMX_Mod_X)

Next time search.

Multi-Lingual Support

Adding multi-lingual support to a plugin can be difficult, but it's usually worth it if you have clients who are willing to translate your strings into their native language.

The first step is to identify what needs to be translated. Say you have a call like this:

Code:
new score = get_score() client_print(id, print_chat, "[AMXX] Your score is %d", score)

This is a good candidate for being multi-lingual. First, create a .txt file (preferrably named after your plugin) and store it in addons\amxmodx\data\lang\. Let's use "myplugin.txt" for the example. For each language, add an entry to the file. Entries are set up as 'keys', which are matched to 'translation strings'. Observe:

(addons\amxmodx\data\lang\myplugin.txt)

Code:
[en]
SCORE_MSG = Your score is %d

[de]
SCORE_MSG = Ihr Spielergebnis ist %d

[es]
SCORE_MSG = Su cuenta es %d

[fr]
SCORE_MSG = Votre score est %d
Then, in plugin_init(), you must register the language keys:

Code:
public plugin_init() {     ...     //assumes placed in amxmodx\data\lang     register_dictionary("myplugin.txt") }

Now, here comes the hard part. AMX Mod X's Multi-Lingual API is built into the format() style routines. For anything that looks like or uses format()-style strings, you can use the ML API.

Code:
client_print(id, print_chat, "[AMXX] %L", id, "SCORE_MSG", get_score())

Let's break this down. For each %L that appears, we need at least two parameters. The first parameter is the TARGET. This must be a player id, LANG_SERVER (meaning show in the server's native language), or LANG_PLAYER. LANG_PLAYER is a special modifier that should only be used when sending a message to all players - it means "show in every player's native language". The second parameter is the key string that identifies the language phrase to translate. Lastly, if the translated string requires any parameters itself (ours needs %d, one integer), that must be added as well.

You can get very complicated designs with this, but it's recommended that you keep things simple for clarity. Here is a final example using a global message to all players, assuming the key HELLO is properly translated in all the languages available:

Code:
client_print(0, print_chat, "[AMXX] %L", LANG_PLAYER, "HELLO")
__________________

Last edited by Napoleon_be; 06-13-2018 at 06:27.
Napoleon_be is offline
Send a message via Skype™ to Napoleon_be
highcoder
Member
Join Date: Jun 2018
Old 06-05-2018 , 02:09   Re: How to adding translations function?
Reply With Quote #3

Hi Napoleon_be,

Thanks for your detailed explain. There is one more thing I want to ask. I understand the use of this %L parameter. Sometimes they use %s, sometimes use %d and sometimes %i parameters. How to determine which one to use? Is the following usage correct?


Code:
client_print(id, print_chat, "[INFO] Name: %s | Kills: %s | Score: %s", NAME, KILLS, SCORE)
highcoder is offline
instinctpt1
Senior Member
Join Date: Dec 2016
Location: Chandigarh, India
Old 06-05-2018 , 07:06   Re: How to adding translations function?
Reply With Quote #4

%s - To retrieve a string ( words / sentences )
%i or %d - To retrieve an integer value ( ex : ... 1 / 2 / 3 ... )
%f - For Floats value ( ex : 0.525000 )
instinctpt1 is offline
highcoder
Member
Join Date: Jun 2018
Old 06-05-2018 , 08:17   Re: How to adding translations function?
Reply With Quote #5

Quote:
Originally Posted by instinctpt1 View Post
%s - To retrieve a string ( words / sentences )
%i or %d - To retrieve an integer value ( ex : ... 1 / 2 / 3 ... )
%f - For Floats value ( ex : 0.525000 )
Thanks for your explain. Also, I saw the %L parameter is using for translation. Is there a document that contains what you're explaining?
highcoder is offline
instinctpt1
Senior Member
Join Date: Dec 2016
Location: Chandigarh, India
Old 06-05-2018 , 08:30   Re: How to adding translations function?
Reply With Quote #6

%L is used to get strings from LangFile

Ex func :
PHP Code:
client_print(0print_chat"[AMXX] %L"LANG_PLAYER"SCORE_MSG"
Now if my plugin uses a dictionary ( lang file )
Then that file should look like this
HTML Code:
[en]
SCORE_MSG = Your score is %d

[de]
SCORE_MSG = Ihr Spielergebnis ist %d

[es]
SCORE_MSG = Su cuenta es %d

[fr]
SCORE_MSG = Votre score est %d
Which means it will depend on user's language and will automatically take translations you provide in your dictionary ( lang file )
instinctpt1 is offline
highcoder
Member
Join Date: Jun 2018
Old 06-05-2018 , 14:48   Re: How to adding translations function?
Reply With Quote #7

Hi instinctpt1, thanks for your explain. I want to get two %L from the language file. What should I do? Also, is there a document that contains what you're explaining? I want to make two different translations for the "[INFO] and "This is the test message." below.

Code:
client_print(0, print_chat, "[INFO] This is the test message.")
highcoder is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 06-05-2018 , 22:47   Re: How to adding translations function?
Reply With Quote #8

Quote:
Originally Posted by highcoder View Post
is there a document that contains what you're explaining?
Did you look at the link or anything posted by Napoleon_be? He literally gave you the entire documentation for the Multi-lingual feature.

Quote:
Originally Posted by highcoder View Post
I want to get two %L from the language file. What should I do?
I believe you just repeat what you do for one %L. Alternatively, you can format them separately and then combine them afterwards with add().

Quote:
Originally Posted by highcoder View Post
I want to make two different translations for the "[INFO] and "This is the test message." below.
Code:
client_print(0, print_chat, "[INFO] This is the test message.")
This question doesn't make sense. Isn't this just the entire point of using the multi-lingual functionality?
__________________

Last edited by fysiks; 06-05-2018 at 22:50.
fysiks is offline
highcoder
Member
Join Date: Jun 2018
Old 06-06-2018 , 02:28   Re: How to adding translations function?
Reply With Quote #9

Quote:
Originally Posted by fysiks View Post
Did you look at the link or anything posted by Napoleon_be? He literally gave you the entire documentation for the Multi-lingual feature.
I couldnt find it.

Quote:
Originally Posted by fysiks View Post
I believe you just repeat what you do for one %L. Alternatively, you can format them separately and then combine them afterwards with add().
It may be possible.

Quote:
Originally Posted by fysiks View Post
This question doesn't make sense. Isn't this just the entire point of using the multi-lingual functionality?
You're not helping. I will try to solve it myself.
highcoder is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 06-06-2018 , 22:22   Re: How to adding translations function?
Reply With Quote #10

Quote:
Originally Posted by highcoder View Post
I couldnt find it.
He posted the entire section for Multi-Lingual support in his post. Also, apparently, the link he posted is broken. The real link is here.



Quote:
Originally Posted by highcoder View Post
You're not helping.
Most often, the quality of a response reflects that of the original question.
__________________

Last edited by fysiks; 06-12-2018 at 20:29. Reason: fixed broken link
fysiks is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 04:43.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode