Skip to content

Instantly share code, notes, and snippets.

@mrcgrtz
Last active June 6, 2021 15:42
Show Gist options
  • Save mrcgrtz/c9c39a84b1b21b7ba8a643ba01082156 to your computer and use it in GitHub Desktop.
Save mrcgrtz/c9c39a84b1b21b7ba8a643ba01082156 to your computer and use it in GitHub Desktop.
User greeting in a random languages, using RUBY-based markup if needed.
<?php
function greeting(string $userName, string $tagName = 'span'): ?string
{
if (!isset($userName)) {
return null;
}
$greetings = [
[
'lang' => 'de',
'title' => 'Deutsch',
'text' => 'Hallo',
],
[
'lang' => 'en',
'title' => 'Englisch',
'text' => 'Hello',
],
[
'lang' => 'ru',
'title' => 'Russisch',
'text' => 'Привет',
],
[
'lang' => 'nl',
'title' => 'Niederländisch',
'text' => 'Hoi',
],
[
'lang' => 'ja',
'title' => 'Japanisch',
'text' => ['今日', 'は'],
'ruby' => ['Konnichi' , 'wa'],
],
[
'lang' => 'zh',
'title' => 'Chinesisch',
'text' => ['你', '好'],
'ruby' => ['Ní', 'hao'],
],
[
'lang' => 'fr',
'title' => 'Französisch',
'text' => 'Salut',
],
[
'lang' => 'iu',
'title' => 'Inuktitut',
'text' => 'Aksunai',
],
[
'lang' => 'da',
'title' => 'Dänisch',
'text' => 'God Dag',
],
[
'lang' => 'sr',
'title' => 'Serbisch',
'text' => 'Здраво',
],
[
'lang' => 'nv',
'title' => 'Navajo',
'text' => 'Yáʼátʼééh',
],
[
'lang' => 'sa',
'title' => 'Sanskrit',
'text' => 'Namaste',
],
[
'lang' => 'ar',
'title' => 'Arabisch',
'text' => 'As-salāmu ʿalaikum',
],
[
'lang' => 'es',
'title' => 'Spanisch',
'text' => '¡Hola',
],
[
'lang' => 'tr',
'title' => 'Türkisch',
'text' => 'Merhaba',
],
[
'lang' => 'eo',
'title' => 'Esperanto',
'text' => 'Saluton',
],
[
'lang' => 'mi',
'title' => 'Maori',
'text' => 'Kia ora',
],
[
'lang' => 'he',
'title' => 'Hebräisch',
'text' => 'Shalom',
],
[
'lang' => 'zu',
'title' => 'Zulu',
'text' => 'Sawubona',
],
[
'lang' => 'sv',
'title' => 'Schwedisch',
'text' => 'Hej',
],
[
'lang' => 'pl',
'title' => 'Polnisch',
'text' => 'Cześć',
],
];
$random = \rand(0, \count($greetings) - 1);
$greeting = $greetings[$random];
if (\is_array($greeting['text'])) {
$rubyText = \implode(\array_map(function($text, $ruby) {
$markup = '<ruby>';
$markup .= $text;
$markup .= '<rp>(</rp><rt>' . $ruby. '</rt><rp>)</rp>';
$markup .= '</ruby>';
return $markup;
}, $greeting['text'], $greeting['ruby']));
return \sprintf('<%5$s lang="%1$s" title="Das ist %2$s.">%3$s, %4$s!</%5$s>', $greeting['lang'], $greeting['title'], $rubyText, $userName, $tagName);
}
return \sprintf('<%5$s lang="%1$s" title="Das ist %2$s.">%3$s, %4$s!</%5$s>', $greeting['lang'], $greeting['title'], $greeting['text'], $userName, $tagName);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment