Skip to content

Commit 9cb3088

Browse files
authored
Merge pull request #52 from wikimedia/use-all-domains
Change Intuition::msg() to check all domains for messages
2 parents 6aecfee + 3043e33 commit 9cb3088

6 files changed

Lines changed: 52 additions & 22 deletions

File tree

README.md

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ A Symfony 4 bundle that provides some common parts of web-based tools in Wikimed
66
Features:
77

88
* OAuth user authentication against [Meta Wiki](https://meta.wikimedia.org/).
9-
* Internationalization with the Intuition and jQuery.i18n libraries.
9+
* Internationalization with the [Intuition](https://intuition.toolforge.org/) and jQuery.i18n libraries.
1010
* Interface to connect and query the [replica databases](https://wikitech.wikimedia.org/wiki/Wiki_replicas)
1111
* PHP Code Sniffer ruleset
1212
* Base Wikimedia UI stylesheet (LESS)
@@ -93,10 +93,10 @@ to the full URL to `Special:OAuth`.
9393
Add a login link to the relevant Twig template (often `base.html.twig`), e.g.:
9494

9595
{% if logged_in_user() %}
96-
{{ msg( 'logged-in-as', [ logged_in_user().username ] ) }}
97-
<a href="{{ path('toolforge_logout') }}">{{ msg('logout') }}</a>
96+
{{ msg( 'toolforge-logged-in-as', [ logged_in_user().username ] ) }}
97+
<a href="{{ path('toolforge_logout') }}">{{ msg('toolforge-logout') }}</a>
9898
{% else %}
99-
<a href="{{ path('toolforge_login') }}">{{ msg('login') }}</a>
99+
<a href="{{ path('toolforge_login') }}">{{ msg('toolforge-login') }}</a>
100100
{% endif %}
101101

102102
The internationalization parts of this are explained below.
@@ -140,6 +140,11 @@ ultimately be redirected back to `https://my-tool.toolforge.org/my-page?foo=bar`
140140

141141
### Internationalization (Intuition and jQuery.i18n)
142142

143+
Internationalization is handled similarly to how it is done in MediaWiki,
144+
with translated strings being stored in `i18n/` directories.
145+
The bundle comes with some strings of its own, all prefixed with `toolforge_`;
146+
it is recommended that these are used where possible because it reduces the work for translators.
147+
143148
#### 1. PHP
144149

145150
In PHP, set your application's i18n 'domain' with the following in `config/packages/toolforge.yaml`:
@@ -148,9 +153,9 @@ In PHP, set your application's i18n 'domain' with the following in `config/packa
148153
intuition:
149154
domain: 'app-name-here'
150155

151-
You can inject Intuition into your controllers via type hinting, e.g.:
156+
You can inject (the bundle's subclass of) Intuition into your controllers via type hinting, e.g.:
152157

153-
public function indexAction( Request $request, Intuition $intuition ) { /*...*/ }
158+
public function indexAction( Request $request, \Wikimedia\ToolforgeBundle\Service\Intuition $intuition ) { /*...*/ }
154159

155160
The following Twig functions and filters are available:
156161

@@ -182,7 +187,7 @@ In Javascript, you need to do three things to enable internationalisation:
182187
<script type="text/javascript" src="https://tools-static.wmflabs.org/cdnjs/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
183188
{% include '@toolforge/i18n.html.twig' %}
184189

185-
(The jQuery can of course be left out if you're already loading that through other means.)
190+
(The jQuery can be left out if you're already loading that through other means.)
186191

187192
3. And symlink your `i18n/` directory from `public/i18n/`,
188193
so that the language files can be loaded by from Javascript.

Resources/i18n/de.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
{
2-
"logout": "Abmelden"
2+
"toolforge-logout": "Abmelden"
33
}

Resources/i18n/en.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
{
2-
"logout": "Logout"
2+
"toolforge-login": "Log in",
3+
"toolforge-logout": "Log out",
4+
"toolforge-logged-in-as": "You are logged in as '$1'"
35
}

Resources/i18n/qqq.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
{
2-
"logout": "Text for logout link."
2+
"toolforge-login": "Text for the log in link.",
3+
"toolforge-logout": "Text for the log out link.",
4+
"toolforge-logged-in-as": "Tooltip text showing the name of the current user.\n\nParameters:\n\n* $1 — the username of the logged-in user"
35
}

Service/Intuition.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,14 @@ public static function serviceFactory(
5555

5656
return $intuition;
5757
}
58+
59+
/**
60+
* Get names of all registered domains.
61+
*
62+
* @return string[]
63+
*/
64+
public function getDomains(): array
65+
{
66+
return array_keys($this->domainInfos);
67+
}
5868
}

Twig/Extension.php

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44

55
namespace Wikimedia\ToolforgeBundle\Twig;
66

7-
use Krinkle\Intuition\Intuition;
87
use NumberFormatter;
98
use Symfony\Component\HttpFoundation\Session\Session;
109
use Symfony\Component\Process\Process;
1110
use Twig\Extension\AbstractExtension;
1211
use Twig\TwigFilter;
1312
use Twig\TwigFunction;
13+
use Wikimedia\ToolforgeBundle\Service\Intuition;
1414

1515
class Extension extends AbstractExtension
1616
{
@@ -86,35 +86,46 @@ public function msgIfExists(string $message = '', array $vars = [])
8686
return $message;
8787
}
8888

89-
9089
/**
9190
* See if a given i18n message exists.
9291
* If this returns false it means msg() would return "[message-key]"
93-
* Parameters the same as msg(), except $fail which is overwritten.
92+
*
9493
* @param string $message The message.
9594
* @param string[] $vars
9695
* @return bool
9796
*/
9897
public function msgExists(string $message = '', array $vars = []): bool
9998
{
100-
return $this->intuition->msgExists($message, [
101-
'domain' => $this->domain,
102-
'variables' => is_array($vars) ? $vars : [],
103-
]);
99+
foreach ($this->intuition->getDomains() as $domain) {
100+
$exists = $this->intuition->msgExists($message, [
101+
'domain' => $domain,
102+
'variables' => is_array($vars) ? $vars : [],
103+
]);
104+
if ($exists) {
105+
return true;
106+
}
107+
}
108+
return false;
104109
}
105110

106111
/**
107-
* Get an i18n message.
112+
* Get an i18n message, searching all registered domains.
108113
* @param string $message
109114
* @param string[] $vars
110115
* @return mixed|null|string
111116
*/
112117
public function msg(string $message = '', array $vars = [])
113118
{
114-
return $this->intuition->msg($message, [
115-
'domain' => $this->domain,
116-
'variables' => $vars,
117-
]);
119+
// Check all domains for this message.
120+
foreach ($this->intuition->getDomains() as $domain) {
121+
if ($this->intuition->msgExists($message, ['domain' => $domain])) {
122+
return $this->intuition->msg($message, [
123+
'domain' => $domain,
124+
'variables' => $vars,
125+
]);
126+
}
127+
}
128+
return $this->intuition->bracketMsg($message);
118129
}
119130

120131
/**

0 commit comments

Comments
 (0)