Forum
How to "localize" (translate) your program - Printable Version

+- Forum (https://www.boriel.com/forum)
+-- Forum: Compilers and Computer Languages (https://www.boriel.com/forum/forumdisplay.php?fid=12)
+--- Forum: ZX Basic Compiler (https://www.boriel.com/forum/forumdisplay.php?fid=11)
+---- Forum: How-To & Tutorials (https://www.boriel.com/forum/forumdisplay.php?fid=13)
+---- Thread: How to "localize" (translate) your program (/showthread.php?tid=540)



How to "localize" (translate) your program - boriel - 04-26-2013

Yes, we are in the information era, Internet was a dream in the 80s, but a reality nowadays 8)
And we are in sort of Babel tower here. This tutorial proposes a simple but effective way to make your program multilingual.

Needless to say 48Kb (actually less than 40Kb) are too little room to store all localized program texts simultaneously. My proposal is simply a structure to make our programs easily translatable to other languages. Here we go:

ZXBASIC allows commandline defines now (1.2.3-s1022), so it's possible to do something like this:

FILE: LOCALIZE.BAS
This file will include the desired language

Code:
#ifndef LANG
' if no LANG defined, fallback to English as default
#define LANG en
#endif

' Now include the desired file

#if LANG==en
#include <english.bas>
#endif

#if LANG==es
#include <spanish.bas>
#endif

#if LANG==ru
#include <russian.bas>
#endif
The above file will include the desired language definition file.

FILE: ENGLISH.BAS
This defines strings in English, using #define directive
Code:
' Just HELLO WORLD
#define HELLO_WORLD "HELLO WORLD"
FILE: SPANISH.BAS
This defines strings in Spanish, using #define directive
Code:
' Just HELLO WORLD
#define HELLO_WORLD "HOLA MUNDO"
FILE: hello.bas
A demo example, which will be localized.
Code:
#include <localize.bas>

10 REM This is the MAIN code
20 PRINT HELLO_WORLD
Now, to generate for English, compile as:
Code:
zxb -TaB -o hello_en.tzx -D LANG=en hello.bas
To generate the same program translated into Spanish, do:
Code:
zxb -TaB -o hello_es.tzx -D LANG=es hello.bas
NOTICE:
  • Use -o parameter to specify a different output file for each language
    (by default ZX Basic uses just the .bas name, but will replace .bas extension with .tzx or .tap accordingly)
  • Use -D <macro> = <value> to define macro values from the command line.

We all are different native-language speakers here, so if we open-source or .bas listings, some of us would be willing to translate into our mother tongue.

What do you think of this proposal?


Re: How to "localize" (translate) your program - LCD - 05-01-2013

I suppose, it is possible to define multiple macros, right?
-D LANG=de -D VERSION=hard -D RATING=adult


Re: How to "localize" (translate) your program - boriel - 05-01-2013

LCD Wrote:I suppose, it is possible to define multiple macros, right?
-D LANG=de -D VERSION=hard -D RATING=adult
Yes, or that's the idea. Tongue

Note this is currently *beta* in 1.3.0.
The next version will have it more tested, since macros now allows also #if directives. e.g.
Code:
#if LANG==de && VERSION==hard || RATING=adult
PRINT "bla bla"
#else
PRINT "ble ble"
#endif
In a similar (but limited) way, as explained here: <!-- m --><a class="postlink" href="http://gcc.gnu.org/onlinedocs/cpp/If.html#If">http://gcc.gnu.org/onlinedocs/cpp/If.html#If</a><!-- m -->


Re: How to "localize" (translate) your program - LCD - 05-02-2013

boriel Wrote:
LCD Wrote:I suppose, it is possible to define multiple macros, right?
-D LANG=de -D VERSION=hard -D RATING=adult
Yes, or that's the idea. Tongue

Note this is currently *beta* in 1.3.0.
The next version will have it more tested, since macros now allows also #if directives. e.g.
Code:
#if LANG==de && VERSION==hard || RATING=adult
PRINT "bla bla"
#else
PRINT "ble ble"
#endif
In a similar (but limited) way, as explained here: <!-- m --><a class="postlink" href="http://gcc.gnu.org/onlinedocs/cpp/If.html#If">http://gcc.gnu.org/onlinedocs/cpp/If.html#If</a><!-- m -->

Oh, that is great and I can imagine some uses for it, apart from Languages.
So this is C Plusplus syntax...

I think, I will try to use it without splitting into multiple sources:
Code:
#ifndef LANG
' if no LANG defined, fallback to English as default
#define LANG en
#endif

#if LANG==en
#define HELLO_WORLD "HELLO WORLD"
#define OTHER_TEXT "OTHER TEXT"
#endif

#if LANG==de
#define HELLO_WORLD "HALLO WELT"
#define OTHER_TEXT "ANDERER TEXT"
#endif


PRINT OTHER_TEXT
PrintString(@Message1)
END
Message1:
ASM
  DEFB 16,7
  DEFM HELLO_WORLD
END ASM



Re: How to "localize" (translate) your program - boriel - 05-02-2013

LCD Wrote:Oh, that is great and I can imagine some uses for it, apart from Languages.
So this is C Plusplus syntax...

I think, I will try to use it without splitting into multiple sources:
Right!
But if you plan to give the lang source to other people to translate to their native language, I think it would be better to give them only a separated translation include file (e.g. because you don't want to give them the complete source).


Re: How to "localize" (translate) your program - LCD - 05-02-2013

boriel Wrote:Right!
But if you plan to give the lang source to other people to translate to their native language, I think it would be better to give them only a separated translation include file (e.g. because you don't want to give them the complete source).
In this case you are absolutly right...


Re: How to "localize" (translate) your program - JBGV - 05-06-2013

boriel Wrote:What do you think of this proposal?

It is a very good idea !

Translation is a headache for me :oops:


Re: How to "localize" (translate) your program - cheveron - 05-29-2014

Just a couple of things to throw out there. As well as language specific strings, ZXodus uses language specific compression code (digraphs) and character sets.


Re: How to "localize" (translate) your program - boriel - 05-30-2014

cheveron Wrote:Just a couple of things to throw out there. As well as language specific strings, ZXodus uses language specific compression code (digraphs) and character sets.
You could also use conditional #ifdef directives, for example, and use #incbin with them.