Joomla get translation - custom - joomla

I have a custom component generating a email newsletter. Now I want to generate the html in the language of the user.
But how can I access the user language file within the code?
using
$lang->setLanguage( $language );
$lang->load();
does not wrok
Thanks

If the email is generated in front-end while user is navigating your site, you can get the language user is using:
$lang = JFactory::getLanguage();
$langTag = $lang->getTag();
echo $langTag; // for example, "en-GB" for English.
$langTag could be different if your site is a multilingual site and user is browsing in a different language, not English.
When you get the language tag, you can generate your newsletter in that language and send.
If you send newsletter from back-end, you can get user's language like this
$user = JFactory::getUser(USER ID HERE); // $user = JFactory::getUser(123);
$langTag $user->getParam('language', 'en-GB'));
echo $langTag;
User can change his/her language in user profile, if user uses the default language, $langTag will be empty, so we use 'en-GB' as fallback.
I hope this helps you.

include
protected $autoloadLanguage = true;
inside your component class in php. And use language files.

Related

Consistency in Laravel localization

I have a laravel appication that I am building. I am building a multi language application which would have french and spanish and its url would be
www.example.com/fr/route/slug for french
www.example.com/es/route/slug for spanish
www.example.com/route/slug for english which is the main one
But I am very confused as to how I should go about maintaining the consistency from one url to another within the same language i.e. when I click on a link which is under french, what should be returned should still be french. e.g:
from
www.example.com/fr/route/slug1 to
www.example.com/fr/route/another_path to
www.example.com/fr/route/final_path which would be maintaing same language path
www.example.com/es/route/slug1 to
www.example.com/es/route/another_path to
www.example.com/es/route/final_path for spanish
www.example.com/route/slug1 to
www.example.com/route/another_path to
www.example.com/route/final_path for english
Also when I change from e.g. french to english the page must be consistent e.g.
www.example.com/fr/route/slug1 to
www.example.com/fr/route/another_path in french to english
www.example.com/route/another_path
What are the steps I should take? Any help would be appreciated. Thanks in advance.
To maintain this you can follow the below steps :
At change point of language at your website ( eg. Change language from dropdown ) , store selected value in session and then your page should be reload
Then you have to create one middelware to manage the localization.
You just have to simply write few line of code in handle method of the Middleware.
$lang = Session::get("language");
App::setLocale($lang);
Thats it, this will manage your consistency.
You should add this group for all your route
Route::group(['middlewareGroups' => 'web', 'prefix' => '{local}'], function () {
and then make middleware where you catch the local and then :
public function handle($request, Closure $next)
{
App::setLocale($locale);
}
And apply it by adding it in your $middleware array located in the Http/kernel.php file

Joomla Error Messages for Specific User Group

I'm trying to set up customer error messages for different user groups in Joomla 3.6. We have locked content on the front-end of our site, currently there is a generic message that tells the user to login to see it, however we recently adjusted our user groups to create multiple levels of access so even after logging in there may still be content the user can't see. In those instances I need to be able to show a message specific to that user group rather than a generic "please login".
I have used Language Overrides for custom messages before but there's no option I've found to have it show to specific groups. I don't know a lot about code, and Google revealed no existing answers for this.
This might be something to get you going:
$groups = JUserHelper::getUserGroups((int) $user->user_id);
if(in_array(8, $groups))
{
$level = 'admin';
} elseif (in_array(4, $groups)) {
$level = 'globalmoderator';
} elseif (in_array(3, $groups)) {
$level = 'moderator';
} elseif (in_array(2, $groups)) {
$level = 'user';
} else {
$level = 'guest';
}
Without some programming it'll not be easy for you to achieve. But in short, you can create a pretty simple plugin, which will capture certain Joomla events.
For example, Joomla has onUserLogin even, which allows you to do different stuff, when user logs in. Your custom plugin can capture this even, check for some parameters and then add a group specific message to the queue. More over, you can get current message queue, parse or empty it and then add group specific message.
You can do the same for any other Joomla event like onUserLogout, onUserLoginFailure and so on. There're plenty events to choose from.
As for message specific groups, you can add your own generic messages similar to this:
COM_USERS_ERROR_LOGIN_GUEST
COM_USERS_ERROR_LOGIN_REGISTERED
COM_USERS_ERROR_LOGIN_ADMIN
Then you just take "COM_USERS_ERROR_LOGIN_" string and add a group name (or ID, when language is non-English) to it. That's it.
The task doesn't seem too complex, but you still have to know some coding to create a plugin, which will capture Joomla events. There's no other way to do this.
Try to use articles for messages and filter it with permissions for user groups.

Jhipster update i18n language for a user

I have a Jhipster application and already changed the default language in the app to use french.
In app.js:
$translateProvider.preferredLanguage('fr');
But I can't change the language that was set for a User. I am using the default User account created with the application: "user".
When I logged in the application and change the language to French and save the User information, the default User table is not changed. So when I logged in again, the language is English.
The lang_key field is still en in the table.
Do you know how to change the language for a User?
Thanks,
It looks like this is not implemented in Jhipster. So the lang_key is only set when you create a new user.
I added it to my application by doing the following:
NOTE: This is changing the language of the current user by setting it by the current language in use in the app.
UserService.java
Add the langKey to the method:
public void updateUserInformation(String firstName, String lastName, String email, String langKey) {
...
currentUser.setLangKey(langKey);
...
}
AccountResource.java
In the Post Request (add the langKey by getting it from the userDTO):
userService.updateUserInformation(userDTO.getFirstName(), userDTO.getLastName(), userDTO.getEmail(), userDTO.getLangKey());
settings.controller.js
Finally in the setting controller set the language in use (just after the account is fetched)
$scope.settingsAccount = account;
$scope.settingsAccount.langKey = $translate.use();
You will need to add $translate to the function parameter of the controller.
If you want set language for each user, you can set it in liquibase, users.csv. And in this file find column - "lang_key". and set language. For default it is "en".
I did, that always was only one language. In any roles and for any users.
Find this block in app.js:
$translateProvider.useLoader('$translatePartialLoader', {
urlTemplate: 'i18n/{lang}/{part}.json'
});
and change line
urlTemplate: 'i18n/{lang}/{part}.json'
to
urlTemplate: 'i18n/fr/{part}.json'
In this case always will be french language

Different URL keys for different language CMS pages

I'm currently setting up a Magento shop that will support a few different languages.
One issue that I ran into is that I can't find out how to link two CMS pages together, so that when a user switches their language, they are automatically forwarded to the current CMS page but in their preferred language. One option would be to use the same URL key for both pages, but that wouldn't be very user friendly as some users would then see URL keys not in their native language.
Let me give you an example:
I have an "About us" page. In the English version of the store, the URL of that page is /about-us. Now a German user lands on that page and switches his language. But because the German equivalent to "About us" is "Über uns", the German version of that page is at /ueber-uns, so the user would be presented a 404 page because no German version of /about-us exists.
Does anybody know how to solve this issue?
Update: Did some more research and found nothing. I can't believe I am the only one with this problem? The go-to solution, using the same URL key for all languages, seems very ugly and not very user friendly!
So, the only solution that I found was to manually create a redirect for each page in the Magento Rewrite Rules. Do do that, go to Catalog -> URL Rewrite Management and add each page in the following format:
So if a user is using the Francais store view and requests /url-in-english, the redirect will kick in and redirect the user to /url-in-french.
This is of course not an ideal solution, it would be preferred if two pages could be "linked" directly, but I suppose I will have to use this for the moment. If anybody comes up with a better solution feel free to add yours!
I have seen this bug in Magento CE 1.8.0.0. The problem here was a wrong assignment in \app\code\core\Mage\Core\Model\Url\Rewrite\Request.php.
To solve this problem it is sufficient to change the assignment of $fromStore in the protected function _rewriteDb() within the Mage_Core_Model_Url_Rewrite_Request class from
$fromStore = $this->_request->getQuery('___from_store');
to
$fromStore = Mage::getModel('core/store')->load($this->_request->getQuery('___from_store'), 'code')->getId();
with the result that we can access the $stores array with the right key (with the store id instead of the store code). With this the if statement
if (!empty($stores[$fromStore])) {
works in the right way.
As a reminder: Do not modify core files. Just copy \app\code\core\Mage\Core\Model\Url\Rewrite\Request.php to \app\code\local\Mage\Core\Model\Url\Rewrite\Request.php before any change.
You will find this answer in German here: Rewrite von Seiten in verschiedenen Sprachen und verschiedenen URL Keys in Magento
Above solution works but takes a while. We just did the following to rewrite the language changer url when on a cms page to go to the base url:
Add the following code to app/design/frontend/default/template_name/template/page/switch/languages.html after the part where the $url variable is filled (on our it was like
$url = /*explode( '?',*/$_lang->getCurrentUrl()/*);*/;
so we added the following:
if(($this->getRequest()->getModuleName() == 'cms') && strpos($url,'.com/')){$url = strstr($url, '.com/', true) . '.com/';}
elseif(($this->getRequest()->getModuleName() == 'cms') && strpos($url,'.de/')){$url = strstr($url, '.de/', true) . '.de/';}
elseif(($this->getRequest()->getModuleName() == 'cms') && strpos($url,'.nl/')){$url = strstr($url, '.nl/', true) . '.nl/';}
What I did here is check if on a cms page and check if the url contains either .com/ ; .de/ or .nl and strip the part before then add the domain extension back.
in our example: http://www.mega-watch.com/about-us?blabla will become http://www.mega-watch.com/
Hope this helps someone out..

Prevent direct access to a page in Joomla

I have a payment gateway integrated on my website. When user is done with payment he/she is redirected to a particular page say www.example.com/redirect. I want to prevent users from directly entering this url (www.example.com/redirect) in address bar and access the page. I want it asap.
Actually the page is protected from guest users but if logged in user types that url then it will redirect him to that page and hence the payment option will be skipped. I want the user must pay the amount first and then redirected to this page.
Hard to answer precisely since you only give a non-joomla url as an example, but at the top of every Joomla script is the following line:
defined('_JEXEC') or die( 'Restricted access' );
You obviously can't prevent a user from typing in the url, so this will at least detect if a session is already in place. If the user isn't in an active Joomla session, this will fire and prevent access. You could easily adapt it to do whatever you want to happen for your requirement, depending on whatever you have to check with, i.e. if the referrer is your payment gateway, etc.
I had a similar desire. I wanted the page to only display if the users was logged in and if they had filled out the order entry page.
What I decided to do was check to see if there was data in the POST.
controller/place_order.php (snipet)
public function submitOrder()
{
$post = JRequest::get('post');
$model = $this->getModel();
if($post != null && $post != ''){
if($model->placeOrder()){
}
}
JRequest::setVar('layout', 'submitOrder');
parent::display();
}
This prevents the task from executing my placeOder function anything in the model. Then I just add something similar to the submit order page. In your case "redirect".
view/place_order/tmpl/submitOrder.php (snipet)
defined('_JEXEC') or die('Restricted access');
$user =& JFactory::getUser();
if ($user->guest) {
echo "<p>You must login to access this page.</p>";
}
else if($_POST == "" || $_POST == null){
echo "<p>You can not directly access this page.</p>";
}else {
//Your order was submitted successfully HTML (don't forget to close it at the bottom ;)
There are a lot of ways you could do it... you probably don't even need to check in the controller if you don't want to but I do to save on time. With out seeing your code it's hard to tailor the answer but if you grasp the concept here it should help (I hope...).
You might also want to check out this page from Joomla on authorization and privileges.
this should be done in your component's base controller (controller.php). if you look at this code snippet:
// Check for edit form.
if ($vName == 'form' && !$this->checkEditId('com_weblinks.edit.weblink', $id))
{
// Somehow the person just went to the form - we don't allow that.
return JError::raiseError(403,
JText::sprintf('JLIB_APPLICATION_ERROR_UNHELD_ID', $id));
}
this block of code is present in most of core components intended to do exactly what you want. how ever how this actually dos what it does is explained through the $this->checkEditId() function. I hope you are familiar with the JControllerForm class and if you are not check out the API. because creating an edit id for a page and "authorizing user for access to a specific page based on his last page" is done by JControllerForm.

Resources