1

How can I retrive translations with country code from this class array and show it in my blade file? Lets say I have country code AF and then how I can return Afghanistan?

class TranslatedCountryCodes
{

    private        $translatedCodes = [];
    private static $instance;

    protected function __construct()
    {
        $this->translatedCodes = [
            'AF' => __('Afghanistan', 'countrycode', 'myproject'),
            'AX' => __('Aland Islands', 'countrycode', 'myproject')
        ];
    }

    public function commitTranslation()
    {
        return $this->translatedCodes;
    }

    /**
     * @return static
     */
    public static function getInstance()
    {
        if (is_null(static::$instance)) {
            static::$instance = new static();
        }

        return static::$instance;
    }
}

I have tried

TranslatedCountryCodes::commitTranslation('AF')

I've been looking up some helper functions but cant figure it out

2
  • 2
    commitTranslation() must be static as well when you call it statically. Commented Nov 12, 2022 at 12:19
  • 1
    Correct call is TranslatedCountryCodes::getInstance()->commitTranslation();. But why do you pass something? The method has no arguments. You need to change it. public function commitTranslation(string $country) { return $this->translatedCodes[$country] ?? 'not found'; }. Commented Nov 12, 2022 at 12:20

1 Answer 1

2

You could do:

TranslatedCountryCodes::getInstance()->commitTranslation()['AF']
Sign up to request clarification or add additional context in comments.

9 Comments

I can't return it in blade file. Shows critical error.
@in2d What does the 'critical error' state? In addition, in what namespace did you define this class? Lastly, what's your Laravel framework version?
The critical error is just from Wordpress. I am using namespace Database\Seeders. I am using Laravel 8.
@in2d Then, in your PHP blade file, try: {{ \Database\Seeders\TranslatedCountryCodes::getInstance()->commitTranslation()['AF'] }}
@in2d I can't assist you without seeing the 'critical error' you claim to receive on your end.😑 Sorry!
|

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.