Should You Use PHP’s Magic Methods or Not?
Are PHP’s magic methods worth using? When you use them, can you truly write testable code? Let’s explore one side of the argument today.
You may already be all over this one, but if you’re new to Oxid eSales or new to website internationalization, then today’s tip is one not to miss.
You may already be all over this one, but if you’re new to Oxid eSales or new to website internationalization, then today’s tip is one not to miss.
When you’re wanting to provide language translations across multiple languages, such as German, English, French, Polish (or whatever), there’s one tag you need to know: oxmultilang.
It’s a simple tag that requires next to no effort to use. So here’s how to get your content internationalized.
Under out/ you’ll find directories for the languages that your site supports. In the default, you’ll see ‘de‘ and ‘en‘, for German and English respectively. In these directories, you’ll see a file called** lang.php**. That’s the default set of Oxid translations.
What you need to do is, essentially, copy that file, naming it** cust_lang.php** and empty it out. After that, follow the original format, adding your translation tags that you’re going to use. For example, below is a sample of what an English version of cust_lang.php could look like when I want to use a tag called PLUS_SHIPPING2.
$sLangName = “English”;
$aLang = array(
‘PLUS_SHIPPING2′ => “Accessories”,
// the rest of the translations…
);
Once you’ve created them for your default language, copy and translate them (if you’re multilingual) or add the translations that someone’s provided to you in your other language files.
Do a double check to make sure that they’re all correct and then move on to step 2.
With the translation tags ready, open up your templates that you’ll be adding internationalized content to and start using the oxmultilang tag.
It’s a wonderful tag that’s painfully simple to use. Remembering the sample that we added in step one, in your template, start using it as in the following example:
[{ oxmultilang ident="PLUS_SHIPPING2" }]<br />
ident is the tag name that will be used, to pull text from your translation file and put it in the generated template when it’s rendered. See, nothing could be simpler.
Now that you’ve setup translations so that you can use them in your templates and you’ve added the oxmultilang tag to your templates, so that you are using the translations you’ve setup, there’s one step that remains.
Under the application root, you’ll see a directory called /tmp. It’s the base cache directory. It’s also, by default, the directory that the Smarty template system uses to cache it’s generated templates to.
So, delete all the files in that directory and reload your browser (on the page that you’ve added your translation too).
This time, you’ll see your internationalized string being displayed, not normal text. Of course, we need to fully test it.
This time, change to one of the other languages of your shop, which you’ve provided a translation for and check that the text changes to the translation in the translation file of your other language.
If you don’t see the text from your translation file displayed when you change languages firstly double check that you did clear out all the files in your /tmp directory.
Then check that you saved the translation files. This will sort out 99% of the issues. If you’ve done all this and it’s still not working, feel free to chat with me in the comments.
Are PHP’s magic methods worth using? When you use them, can you truly write testable code? Let’s explore one side of the argument today.
This is a post I’ve been meaning to write for a feels weeks now after I first started using Traits for simple reuse, as it solved a need I had at the time. After a while it seemed to be not too bad of a solution also. What Are Traits? If you’re not familiar with Traits the PHP manual describes them as: Traits are a mechanism for code reuse in single inheritance languages such as PHP.
In today’s post, we look at more HTML5 Form fields in Zend Framework 2: Month, Range, Color, Week and Number, as well as element properties and attributes. Come look around more of the great new elements available.
What does it take to be a PHP core contributor? What things do you have to do? Why would you do it? I’ve started on the journey. Here’s what I’m learning.
Please consider buying me a coffee. It really helps me to keep producing new tutorials.
Join the discussion
comments powered by Disqus