Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
CiviCRM Core
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Container Registry
Model registry
Analyze
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Development
CiviCRM Core
Commits
9b73c1a2
Commit
9b73c1a2
authored
2 years ago
by
totten
Browse files
Options
Downloads
Patches
Plain Diff
#3979
- Make an educated guess about how to set UF locale. Add test-case.
parent
ba2c89ef
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
Civi/Core/Locale.php
+8
-0
8 additions, 0 deletions
Civi/Core/Locale.php
tests/phpunit/E2E/Api4/LocaleTest.php
+60
-4
60 additions, 4 deletions
tests/phpunit/E2E/Api4/LocaleTest.php
with
68 additions
and
4 deletions
Civi/Core/Locale.php
+
8
−
0
View file @
9b73c1a2
...
...
@@ -203,6 +203,14 @@ class Locale {
$validDbLocales
=
\Civi
::
settings
()
->
get
(
'languageLimit'
);
$locale
->
db
=
static
::
pickFirstLocale
(
array_keys
(
$validDbLocales
),
$fallbacks
)
?:
$systemDefault
;
}
// Determine locale for UF APIs: This next bit is a little bit wrong.
// We should have something like `$validUfLanguages` and pick the closest match.
// Or perhaps each `CRM_Utils_System_{$UF}` should have a `negotiate()` helper.
// But it's a academic... D7/D8/BD are the only UF's which implement `setUFLocale`/`getUFLocale`,
// and they drop the country-code - which basically addresses the goal (falling back to a more generic locale).
$locale
->
uf
=
$locale
->
ts
;
return
$locale
;
}
...
...
This diff is collapsed.
Click to expand it.
tests/phpunit/E2E/Api4/LocaleTest.php
+
60
−
4
View file @
9b73c1a2
...
...
@@ -2,22 +2,78 @@
namespace
E2E\Api4
;
use
Civi\API\Event\RespondEvent
;
/**
* Class LocaleTest
*
* @package E2E\Api4
* @group e2e
*/
class
LocaleTest
extends
\CiviEndToEndTestCase
{
public
function
getLanguageExamples
():
array
{
$results
=
[];
switch
(
CIVICRM_UF
)
{
case
'Drupal'
:
case
'Drupal8'
:
case
'Backdrop'
:
$results
[]
=
[
'de_DE'
,
'de_CH'
,
't'
,
'Yes'
,
'Ja'
];
// That's weird -- you install Drupal's "de", and you do `setUFLocale('de_DE')`, and
// the result is... to report back as 'de_DE'. Weird. But the actual string is OK...
break
;
case
'WordPress'
:
$results
[]
=
[
'de_DE'
,
'de_DE'
,
'__'
,
'Yes'
,
'Ja'
];
break
;
case
'Joomla'
:
default
:
$this
->
fail
(
'Test not implemented for '
.
CIVICRM_UF
);
}
return
$results
;
}
/**
* Ensure that
* APIv4 allows you to request that operations be processed in a specific language.
*
* @param string $civiLocale
* The locale to specify in Civi. (eg Civi\Api4\Foo::bar()->setLanguage($civiLocale))
* Ex: 'de_DE'
* @param string $expectUfLocale
* The corresponding locale in the UF.
* Ex: 'de'
* @param string|array $translator
* The user-framework's translation method.
* Ex: 't' (Drupal) or '__' (WordPress)
* @param string $inputString
* The string to translate.
* Ex: 'Yes', 'Login', 'Continue'
* @param string $expectString
* The string that should be returned.
* Ex: 'Ja', 'Anmeldung', 'Fortsetzen'
*
* @dataProvider getLanguageExamples
*/
public
function
testSetLanguage
()
{
public
function
testSetLanguage
(
$civiLocale
,
$expectUfLocale
,
$translator
,
$inputString
,
$expectString
)
{
$actualStrings
=
[];
$actualLocales
=
[];
\Civi
::
dispatcher
()
->
addListener
(
'civi.api.respond'
,
function
(
RespondEvent
$e
)
use
(
&
$actualStrings
,
&
$actualLocales
,
$translator
,
$inputString
,
$civiLocale
)
{
$isTranslatedRequest
=
(
$e
->
getApiRequest
()
->
getLanguage
()
===
$civiLocale
);
if
(
$isTranslatedRequest
)
{
$actualLocales
[]
=
\CRM_Utils_System
::
getUFLocale
();
$actualStrings
[]
=
call_user_func
(
$translator
,
$inputString
);
}
},
-
100
);
$contacts
=
civicrm_api4
(
'Contact'
,
'get'
,
[
'limit'
=>
25
,
'language'
=>
'en_US'
,
'language'
=>
$civiLocale
,
]);
$this
->
assertTrue
(
count
(
$contacts
)
>
0
);
$this
->
assertTrue
(
count
(
$contacts
)
>
0
,
'The API call should return successfully.'
);
$this
->
assertEquals
(
1
,
count
(
$actualLocales
),
'We should observed one UF locale.'
);
$this
->
assertEquals
(
1
,
count
(
$actualStrings
),
'We should observed one UF translation.'
);
$this
->
assertEquals
(
$expectUfLocale
.
':'
.
$expectString
,
$actualLocales
[
0
]
.
':'
.
$actualStrings
[
0
],
"Expected UF to report locale as '
$expectUfLocale
:
$expectString
'."
);
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment