0

At the very moment I define my public static variable as below:

public static $closed = array(
    'nl' => 'Gesloten',
    'fr' => 'Fermer',
    'en' => 'Closed',
);

Is there any way to put an array formatted like below in a public static without much nesting?

$days['nl']['mon'] = 'Monday';
$days['fr']['tue'] = 'Tuesday';
$days['en']['wed'] = 'Wednesday';
3
  • 2
    I'm not really sure what you're trying to accomplish. What do you mean by "without much nesting"? Commented Apr 22, 2015 at 16:09
  • I think the user is referring to avoiding 'nl' => array('mon'=>'Monday') though I agree that I am having a hard time understanding what is meant to be accomplished by this. Commented Apr 22, 2015 at 16:12
  • IMO, for something like this, you're better off creating a translation file in something like XML or YAML which can then be parsed on demand. It allows you the ability to create a translation digest for your entire site in multiple languages (one language per file) without cluttering up your business logic with pure data. Commented Apr 22, 2015 at 17:07

1 Answer 1

1

There are many ways to build arrays (like you have done) but from the information you have presented above there isn't really a better or more compact way.

From what I see, it looks like you are doing translations, I highly recommend using gettext for that. With arrays as you add translations, your arrays will get larger and larger and take up more memory.

A basic example of how gettext works is the following:

echo __('Monday');

Anything passed to the __() function gets sent through gettext. Earlier on in your script you tell gettext what locale you want to use. You end up having locale files (.mo) that are easier to handle.

Sign up to request clarification or add additional context in comments.

1 Comment

Note that if you for some reason can't use the gettext extension for PHP, there exists several PHP native implementations.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.