0

I have various very large JavaScript files, and need to extract only certain strings. The input would be hundreds of lines like this:

function foo("ignore this string"){
var n = "copy this";
}
function bar("copy this string"){
var i = "ignore this string";
}
var globalX ="copy if over a certain length";
var globalY ="copy if within 2 lines of 'globalX'";

The output would be a simple list of strings. Writing the rules would not be hard, but can JavaScript read 100 pages of code and treat it like a string for parsing purposes? if not, what other tools should I use? Any suggestions are welcome!

Chris Tolworthy
  • 2,066
  • 4
  • 20
  • 24
  • Your question is not at all clear. – Matt Ball Sep 14 '11 at 13:15
  • Sorry. And thanks for the quick reply. I'm making a simple JavaScript Based adventure game engine. I want the games to be available in other languages. All the translation tools I can find assume a fairly static web page. So I plan to take my raw code and produce a list of strings that I can give to a translator. I hope to find a tool that allows sophisticated find and replace with macros, but I don't know where to start. – Chris Tolworthy Sep 14 '11 at 13:26

1 Answers1

0

Based on your comment above - what you need is a JavaScript localization mechanism. There are several options that depend on the way you build and package your application.

Here are some of the mechanisms:

1) Create a separate JS file for each language and load the correct one depending on the client (by retrieving his information from the request).

Quite poor - a lot of duplication and overhead.

2) A bit improved method is to represent each string by an object:

var localeStrings =
{
    'some message':
    {
        'en/US':'Some text',
        'ru/RU':'Какой-то текст'
    }
}

than i your code you retrieve the real message by calling localeStrings['some message']['en/US'], again, retrieving the language from request header and using just as I used 'en/US'.

Also poor - since, again, a lot of unnecessary data is loaded to the client.

3) Some form of dynamic replacement upon request.

Generally speaking the idea is that you have some for of a server-side component (say, servlet) that replaces tokens (let's say `##{some token}) with the data from the server side file(s) that contains the translations.

This one may cause user responses to be a bit delayed, but this is only an assumption and may be negligible.

Pick your poison.

I am sure there are more, and am anxious to see them in answers. I couldn't find something I could use out-of-the-box.

EDIT: forgot to point you to a related conversation.

Community
  • 1
  • 1
ZenMaster
  • 12,363
  • 5
  • 36
  • 59
  • Thanks. The replacing part isn't a problem - I would go with something like option 2. The problem is that the initial code (including strings) is written in English. Before getting to your option 2, I would need to read the code, ignore string 'some message' (an internal variable), and copy the string 'some text.' Then the resulting file is sent to a friendly French person. – Chris Tolworthy Sep 14 '11 at 14:03
  • You are missing the point, I think. Or may be I am. You externalize your strings from your code (for example using option 2). I suspect you will have to do it by hand, unless you have a set of rules that will allow for an automatic parsing. If you do have something like that - you have to run the process only once through whatever parsing/regex-matching engine you can find, replace all the strings that the parser found with keys (again, with something like `'some message'` from option 2), put the `localeStrings` object in a separate JS file and give it to the friendly French person. – ZenMaster Sep 14 '11 at 14:10
  • Yes, the parsing/regex-matching engine is what I need. I want something where I can write a macro once and then use it in every future game I create. I previously used Adventure Game Studio, and it included a one-click button for extracting every string that might be needed by a translator. I want to create a similar tool, but add in some rules of my own. – Chris Tolworthy Sep 14 '11 at 14:22
  • Hmm. First, for future strings - don't use the engine again, just create the strings already externalized and localized. How did the previous engine new which strings are needed for translation? ...anyway, as for parsing and assuming that you do know how to define patterns that you need to extract - perhaps something custom, written in Java, or Perl or whatever? – ZenMaster Sep 14 '11 at 14:32
  • re: creating strings already externalized: that's not an option because of the kind of games I make, but that's another story. Re: custom in Java or Perl - I will probably end up doing that. But I wanted to avoid learning a whole new programming language just to do a single small job. Re: the previous engine - it was written in C, which is happy to save text files to hard disk. – Chris Tolworthy Sep 14 '11 at 15:11