How can PHP beginners effectively utilize classes for language translation in menus?

To effectively utilize classes for language translation in menus, PHP beginners can create a Translation class that stores key-value pairs of translations for different languages. This class can have a method to retrieve the translation based on the provided key. By using this Translation class in menu generation functions, beginners can easily switch between languages without hardcoding translations in the menu code.

class Translation {
    private $translations = array(
        'english' => array(
            'home' => 'Home',
            'about' => 'About',
            'services' => 'Services',
            'contact' => 'Contact'
        ),
        'spanish' => array(
            'home' => 'Inicio',
            'about' => 'Acerca de',
            'services' => 'Servicios',
            'contact' => 'Contacto'
        )
    );

    public function getTranslation($key, $lang) {
        return $this->translations[$lang][$key];
    }
}

// Example of how to use the Translation class for menu generation
$translation = new Translation();
$lang = 'english'; // or 'spanish'
$menuItems = array('home', 'about', 'services', 'contact');

foreach($menuItems as $item) {
    echo '<li>' . $translation->getTranslation($item, $lang) . '</li>';
}