Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CRM_Core_I18n::setLocale should change $tsLocale for getLocale() to work correctly #13050

Merged
merged 2 commits into from
Nov 8, 2018

Conversation

mlutfy
Copy link
Member

@mlutfy mlutfy commented Nov 1, 2018

Overview

Adds a test for getLocale/setLocale, which I suspect is not working as intended, since setLocale does not change the tsLocale, but getLocale reads from it (it still changes the UI strings, because it changes the gettext configuration, but other things such as smarty templates checking against tsLocale might be confused).

@civibot
Copy link

civibot bot commented Nov 1, 2018

(Standard links)

@civibot civibot bot added the master label Nov 1, 2018
@mlutfy
Copy link
Member Author

mlutfy commented Nov 1, 2018

The test fails, as expected:

CRM_Core_I18n_LocaleTest::testI18nLocaleChange
Failed asserting that two strings are equal.
--- Expected
+++ Actual
@@ @@
-'en_US'
+'fr_CA'

/home/jenkins/bknix-dfl/build/core-13050-v1qx/sites/all/modules/civicrm/tests/phpunit/CRM/Core/I18n/LocaleTest.php:52
/home/jenkins/bknix-dfl/build/core-13050-v1qx/sites/all/modules/civicrm/tests/phpunit/CiviTest/CiviUnitTestCase.php:192
/home/jenkins/bknix-dfl/civicrm-buildkit/extern/phpunit5/phpunit5.phar:598

I'll add another commit that should fix it.

@mlutfy mlutfy changed the title WIP: Add phpunit test for getLocale/setLocale. CRM_Core_I18n::setLocale should change $tsLocale for getLocale() to work correctly Nov 1, 2018
@mattwire
Copy link
Contributor

mattwire commented Nov 6, 2018

@mlutfy Is there a simple way for me to test this in the UI? What would I expect to see?

@mlutfy
Copy link
Member Author

mlutfy commented Nov 6, 2018

Not easily. One use-case I had run into was with cdntaxreceipts, when I edited the cdntaxreceipt message template with, for example:

{if $tsLocale == "fr_FR"}
Bonjour,
{else}
Hello,
{/if}

(this is not a great practice, an alternative is to use {ts domain="mycustomextension"}, store a copy of the msg-template in it, and use civistrings to generate a gettext file that you can translate.. but that can be a lot of work)

and then:

  • set your default site language to English, enable multi-lingual, add French
  • create a contact whose language pref is fr_FR
  • set your interface UI to English
  • Generate a cdntaxreceipt for that contact, using the backend

Normally the cdntaxreceipt extension should setLocale to fr_FR, so that it generates a receipt in French for that contact. The PDF will be in French, but the email with the text based on tsLocale will be in English.

I was working on:

I was encountering situations where 3rd-party extensions, such as cdntaxreceipts, were generating UIs that are part English, part French.

setLocale isn't used much in CiviCRM. It mostly has two use-cases: when sending mailings in another language (on multilingual sites), and for cdntaxreceipts (although it should be used in more places, but it's somewhat recent, and users have the habit of working around those limitations, such as by switching the interface language when communicating to a user in their language).

setLocale would correctly switch the gettext language and db language, but by not changing the tsLocale, Smarty templates that were doing conditionals on that variable would not evaluate correctly.

@eileenmcnaughton
Copy link
Contributor

This is a simple change and it's well explained and makes a lot of technical sense.

The global $tsLocale var seems a bit smelly but since we use it making it right makes sense.

We have just cut an rc today so merging this now gives it 2 full months before release which seems good since I don't feel any testing we do is particularly meaningful. Merging

@eileenmcnaughton eileenmcnaughton merged commit 9bc750f into civicrm:master Nov 8, 2018
@eileenmcnaughton eileenmcnaughton deleted the i18n-getlocale branch November 8, 2018 05:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants