Skip to content

Instantly share code, notes, and snippets.

@gusrub
Created April 26, 2018 04:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gusrub/9b82f5d042b3f4507614a186ba63b4c0 to your computer and use it in GitHub Desktop.
Save gusrub/9b82f5d042b3f4507614a186ba63b4c0 to your computer and use it in GitHub Desktop.
Keyword arguments for translation PHP
<?php
putenv("APP_LANG=es_MX.utf8");
require_once('i18n.php');
class Person
{
const VERSION = '1.0.0';
public $firstName;
public $lastName;
public $dob;
function __construct($firstName, $lastName, $dob)
{
$this->firstName = $firstName;
$this->lastName = $lastName;
$this->dob = $dob;
}
public function printInfo()
{
$age = $this->calculateAge();
echo I18n::translate(
"Hello my name is {firstName} {lastName} and I'm {age}\n",
[
'firstName' => $this->firstName,
'lastName' => $this->lastName,
'age' => $age
]
);
}
public function goodBye()
{
echo I18n::translate("That's it, good bye!\n");
}
public function printVersion()
{
echo I18n::translate("\n(running version {version})\n",
[
'version' => self::VERSION
]
);
}
private function calculateAge()
{
$dob = date_create($this->dob);
$now = date_create();
$age = date_diff($dob, $now)->format("%y");
$response = null;
if($age < 18) {
$response = I18n::translate("{age} years which means I'm a teen",
[
'age' => $age
]
);
} else {
$response = I18n::translate("{age} years which means I'm a grown up",
[
'age' => $age
]
);
}
return $response;
}
}
$p = new Person(
'John',
'Wayne',
'1980-01-20'
);
$p->printInfo();
$p->goodBye();
$p->printVersion();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment