#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2004
    Posts
    3
    Rep Power
    0

    Question placeholder replacement tool


    does anybody know a tool that can parse files in a certain directory for defined placeholders, replace them with the correct values and write the output files to another directory?
    The problem is, that we're trying to translate our website into different languages. Because of the software we're using and the many users viewing the page at the same time, we cannot just read the placeholder values from a database at runtime.
    So we want to make different directories for different languages. The idea is, that we're developing the project in one directory, where we're only using placeholders instead of output text.
    The tool we're searching for should then parse the files in this directory and replace the placeholders with a correct value (read from database or anywhere else). The changed files shall be written into another directory (i.e. the name of the language).
    I've already tried a google search, but I didn't find anything that could help.
    So maybe anybody of you knows such a tool?

    Frank.
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2004
    Posts
    3
    Rep Power
    0
    no idea? really nobody?
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    London, England
    Posts
    1,585
    Rep Power
    1373
    You could easily write a script to do what you want in a language such as Python, Perl, Ruby, PHP etc. My preference is for Python, but all of them would do the job.

    There are also a thousand and one template engines out there that will do what you want, most of them as extensions to various programming languages. My favourite is Cheetah (http://www.cheetahtemplate.org) which allows you to create an inheritance heirachy of templates. It is closely integrated with the Python language - the templates are compiled into Python classes that can then be called from other Python code.

    Cheetah is probably overkill in your case - a seasoned Python programmer could knock up a script to do it in no time at all.

    Hope this helps

    Dave - The Developers' Coach
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    London, England
    Posts
    1,585
    Rep Power
    1373

    Talking


    Originally Posted by DevCoach
    a seasoned Python programmer could knock up a script to do it in no time at all.
    ...and here it is. Since the specification of the input data was a bit vague, I am assuming the input is in a CSV file, with the first row as the label names. This could easily be adapted to read from a database or XML or Excel file, or pretty much any other data format.

    The template file needs to have markers in the form %(LABEL)s, where LABEL matches the column header of the CSV file. The CSV files needs to have a column called 'FILENAME' which is the name of the file to write out for each row.

    Here is the complete code. It is not very elegantly written, but I knocked it out in about 10 minutes:

    Code:
    # script to read data from a csv file and write the output to template files
    
    import csv, os, sys
    template = open('template.txt').read()
    outDir = 'out'
    
    csvFile = open(sys.argv[1], 'rb')
    headers = csv.reader(csvFile).next()
    csvDics = csv.DictReader(csvFile, headers)
    for dic in csvDics:
        txt = template % dic
        filename = os.path.join(outDir, dic['FILENAME'])
        f = open(filename, 'w')
        f.write(txt)
        f.close()

    Dave - The Developers' Coach
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2004
    Posts
    3
    Rep Power
    0
    thanks for your help, although I never worked with python.
    I never tried this programming language, I know C, C++, php, Visual Basic and Java. But I never tried python,

    I'll try to make sth. in C or php ...

    Nevertheless, thanks for your help
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    London, England
    Posts
    1,585
    Rep Power
    1373

    Smile


    Originally Posted by muf
    thanks for your help, although I never worked with python.
    I never tried this programming language, I know C, C++, php, Visual Basic and Java. But I never tried python,
    I can recommend giving Python a try - if you know those languages you will be able to pick up enough Python to be productive in a day or two. I believe that Python is one of the easiest to learn and most productive languages in existence.

    Don't take my word for it - here is an article by Eric Raymond (the author of The Cathedral and the Bazzar, and general open source guru) on how he fared learning Python. http://www.linuxjournal.com/article.php?sid=3882

    You can download Python for free from http://www.python.org.

    Dave - The Developers' Coach

IMN logo majestic logo threadwatch logo seochat tools logo