REBOL [ Title: "Language resources" File: %languages.r Date: 2000-06-18 Author: "Ingo Hohmann" Email: ingo@2b1.de Site: http://www.2b1.de/Rebol/ Purpose: {allows to bind UI strings to different languages via resource files} Usage: { add 'lang: 'your-language to system/user (e.g. 'de 'en ...), add 'language-file: %resources-file into the header of your files. create resourcefiles for your scripts via language-helper/create-res-file add language/bind [ &my &words ] in the start of your script. words starting with "&" will be bound to strings (&whatever) if no resourcefile can be found they will be set to a string representation of themselves, with "_" replaced by " ", e.g. &This_is_a_test will be set to "This is a test" I am using the "&" prefix, to easily identify strings in the script, and language/bind, so that I don't have to scan the whole file to bind strings to the words. I am not completely sure I like it the way it is, though, any better ideas? } Version: 0.0.2 changes: [ 0.0.2 {changed the parse rule, should get all words now, and only once} {initialization added} 0.0.1 {initial} ] ] ; ; initialilze langugae ; if not word? in system/user 'lang [ lang: ask "What language do you like to use? (en, de, ....) " do lang: compose/deep [system/user: make system/user [lang: (to-lit-word lang)]] lang: next mold lang lang: copy/part lang back tail lang print rejoin [ {Ff you don't want to answer this question again after the next startup, You can add "} lang {" into your user.r file.} ] ] language: make object! [ bind: func [ "Binds block of words to language strings" words [block!] "words to be bound to strings" /local resources word res language ] [ language: any [ all [word: in system/user 'lang get word] 'en ] either not error? try [ resources: load system/script/header/language-file ] [ lang: either found? find first resources language [ index? find first resources language ] [ 2 ] remove resources foreach word words [ foreach res resources [ if word = first res [ set word pick res lang if "" = get :word [set :word next replace/all to-string word "_" " "] break ] set :word next replace/all to-string word "_" " " ] ] ] [ foreach word words [ set :word next replace/all to-string word "_" " " ] ] ] ] language-helper: make object! [ language: any [ in get in system 'user 'lang 'en ] create-res-file: func [ "Creates a resourcefile, prints bind block" file [file!] "file to scan" resource-file [file!] "resource-file to create" languages [block!] "language in the file" /local resources rule res2 dir word start res3 ] [ resources: make string! 100 res2: make block! 20 rule: [ start: any [ to word! set word word! ( word: to-string word if #"&" = first word [ append res2 word ?? res2 ] ) ] :start any [ to block! into rule ] ] set [dir file] split-path clean-path join what-dir file resource-file: join dir second split-path resource-file file: join dir file if error? try [ file: read file ] [ print rejoin [ "Unable to read file " file ] exit ] insert languages 'languages append resources join mold languages newline parse load file rule res2: union res2 [] res3: rejoin [ "languages/bind [" newline ] foreach word res2 [ append resources rejoin [ "[" word tab ] append resources rejoin [ {"} next replace/all to-string word "_" " " {"} ] for i 3 length? languages 1 [ append resources {^-""} ] append resources rejoin [ "]" newline ] append res3 rejoin [" " word newline ] ] append res3 rejoin [ "]" newline newline ] insert resources res3 if error? try [ write resource-file resources ] [ print "Unable to write file" ] print rejoin [ newline {Now please check the file "} resource-file {", and move the "language/bind [ some words ]" part into your application file.} newline ] 'ok ] ]