0

I have written a simple Translate class that stores a constant associative array. Key is the source language and value is the translation in target language. How can I search for a given key in this array and return its value (translation here)?

namespace utils;

abstract class Translate
{
    private const WORDS = array (
        "Welcome" => "خوش آمدید", 
        "Access denied" => "دسترسی امکان پذیر نمی باشد",

    ) ;



    /**
     * Returns the translation of the given word. 
     * Returns the given word as is if $dont_translate is set to false
     * @param string $word
     * @param boolean $dont_translate
     */
    public static function get($word, $dont_translate = false){
        $data = WORDS;        
        foreach($row as $data) {

        }
    }
}

In my code, then, I use this function as follows:

if($loggedIn) {
    echo '<p class="center">'. Translate::get('Welcome ')  . $username;
}
else {
    die("<p class='center'>" . Translate::get('Access denied ') . " </p>");
}
Mehdi Haghgoo
  • 3,144
  • 7
  • 46
  • 91
  • You would benefit yourself (you gain rep by marking questions as solved) and others by going through some of your questions and accepting some answers - you seem to have a large number of open questions. – Nigel Ren Mar 03 '18 at 09:03

3 Answers3

2

I've changed the second parameter as it can become confusing to have double negative conditions (don't translate being false is IMHO less logical than translate being true)...

/**
 * Returns the translation of the given word.
 * Returns the given word as is if $translate is set to false
 * @param string $word
 * @param boolean $translate
 */
public static function get($word, $translate = true){
    $word = trim($word);
    if ( $translate )   {
        if ( isset(self::WORDS[$word]) )    {
            $trans = self::WORDS[$word];
        }
        else    {
            $trans = false;
        }
    }
    else    {
        $trans = $word;
    }
    return $trans;
}

This returns false if the translation word doesn't exist, it would be easy to change to return the original value.

Just added a trim() to the input as I noticed your examples have spaces.

Although I'm not a huge fan of this style of coding (it makes the code less legible), if your using PHP 7 null coalesce operator, you can use the slightly shorter...

public static function get($word, $translate = true){
    return ( $translate )?(self::WORDS[trim($word)]??false):trim($word);
}
Nigel Ren
  • 56,122
  • 11
  • 43
  • 55
  • Thanks. You are absolutely right about the default argument – Mehdi Haghgoo Mar 03 '18 at 08:37
  • 2
    Good answer, but it's better, if you would use early returns. For example: if (!$translate ) { return $word;} if (!isset(self::WORDS[$word])) { return $word } ... – FabianG Mar 03 '18 at 08:44
  • @FabianG - 'better' is a matter of opinion, https://stackoverflow.com/questions/30256157/best-practice-for-returning-in-php-function-method – Nigel Ren Mar 03 '18 at 08:47
1

You better start reading the manual for PHP arrays: http://php.net/manual/de/language.types.array.php

But nevermind here is a beginner friendly code snippet:

if (isset(self::WORDS['Access denied'])) {
    echo self::WORDS['Access denied'];
} else {
    echo 'No key found'
}
FabianG
  • 120
  • 1
  • 2
  • 10
1

try this

public static function get($word, $dont_translate = false){
    $data = WORDS;        
    $str = isset($data[$word]) ? $data[$word] : $word;
     return $str;
}
Abasaheb Gaware
  • 509
  • 4
  • 13