DADU
DADU

Reputation: 7048

What's the best format for a language file?

When you create a PHP script which outputs text and you want to be able to let the user of the script set the language, what's the best format for a language file?

  1. Database
  2. CSV file
  3. XML file
  4. JSON file
  5. Anything else?

Keep in mind that the user of the script will need to be able to change some words in the language file, so the easier the better.

Upvotes: 1

Views: 1460

Answers (3)

Arnaud Le Blanc
Arnaud Le Blanc

Reputation: 99909

gettext is often used for two reasons:

  • there are many tools to help translating with this format
  • gettext is available in many programming languages

The way to translate a string is to call the _() function like this: _("some text").

An other format that's easy to parse with PHP is ini files.

Upvotes: 2

Clint Miller
Clint Miller

Reputation: 15381

Don't forget YAML.

The answer is that it depends on your data. CSV is very convenient if you're outputting a list of records where each field in the record is a simple value. But CSV gets troublesome if records have hierarchy in them or if fields can have multiple values.

For representing hierarchy, relationships, or multiple values, XML, JSON, and YAML are the most appropriate. Of these, XML is the most verbose, which is why I tend not to like it. I find YAML fairly readable without being too verbose.

Upvotes: 1

Nikita Rybak
Nikita Rybak

Reputation: 68006

.properties file is probably the simplest kind of configuration, if I understand the question correctly.

E.g.,

language = en
time = 18:00

Lots of languages have standard libraries to read those.

Upvotes: 1

Related Questions