Smarty assign var to config file vars - php

My question looks almost the same as this Is it possible to put variables inside config files?
I have a config file that controls the language. Also I have a feedback div that is showed for the feedback.
Now I want to assign feedback like this:
$smarty->assign('feedback', 'the_age_of_user_' . $user->name . '_is_changed_to_' . $user->age . '_years');
nl.conf
the_age_of_user_%s_is_changed_to_%s_years = De leeftijd van gebruiker %s gewijzigd naar %s jaar
en.conf
the_age_of_user_%s_is_changed_to_%s_years = The age of user %s is changed to %s years
Does anyone know how I can accomplish this? Or is there a better solution to assign variables to the config file?

Sorry, but I don't completely understand what are you trying to accomplish.
In our project, we have following solution for similar problem:
Language file:
the_age_of_user_is_changed = The age of user {user} is changed to {year} years
Notice that there is no %s or any variables on left side. It is always constant.
Option 1:
Smarty/code assigment:
$smarty->assign("VARIABLE_TO_USE_IN_TEMPLATE", getLang('the_age_of_user_is_changed', array('user' => $user->name,'year' => $user->age));
Then, in Smarty, you can use following code: {$VARIABLE_TO_USE_IN_TEMPLATE}
Function getLang reads string information from language files and replaces all {symbol} to corresponding data.
Option 2:
In template, we have smarty userspace function:
{LANG key='the_age_of_user_is_changed' name=$user->name year=$user->age}
Lang is actually calls function getLang from previous example.
In code you have to assign user: $smarty->assign("user", $user);. After that, smarty will do everything by itself.

Related

Typo3 - Typoscript set FE username as a constant

I am currently trying to add the username of the currently logged in FE user as a constant. I have tried the following but it does not seem to work.
Username = TSFE:fe_user|user|username
I am using this as a starting point
I would like to know how get the FE username into a constant so I can call it when ever I need for the rest of my typoscript.
This is the bit of typoscript that I am trying to get the username into, at the moment it just adds the company name which is defined as a constant else where, but I would like to add int he username also. I have tried adding the global variable for the username but no joy.
[usergroup = *]
config.tx_we_google_analytics {
_setCustomVar = 1, 'Client', '{$companyName} - {TSFE:fe_user|user|username}',2
}
[end]
Many Thanks in Advance
Bob
I wouldn't recommend using an extension to insert something as basic as a Google Analytics Snippet. Each extension slows down the system and makes it less maintainable.
I don't know what your customVar does, but you might try something like
temp.analytics = COA
temp.analytics {
10 = TEXT
10.value (
first part of GA snippet
)
20 = TEXT
20.data = TSFE:fe_user|user|username
30 = TEXT
30.value (
rest of GA Snippet
)
}
Then insert your snippet where you want, e.g. headerData.99 < temp.analyticsand you're done.
Or even try with a wrap and insertData=1.
As Artur says, if data doesn't render in .wrap, always try stdWrap.wrap - it tends to add functionality (although afaik there have been efforts to pass every wrap through stdWrap by default, but I don't think it's fully implemented).
You can use the appropriate global variable inside TypoScript setup directly, there is no need for a constant I think. The example you provided is the global variable I am talking about, which you can use in any getText context inside typoscript setup.
e.g.
temp.userName = TEXT
temp.userName.data = TSFE:fe_user|user|username
TypoScript constants allow only direct value assignment, it is not possible to assign any other "variable" to a constant IMHO.
Try this. If you are lucky it could work.
[usergroup = *]
config.tx_we_google_analytics {
_setCustomVar.cObject = TEXT
_setCustomVar.cObject.value = 1, 'Client', '{$companyName} - {TSFE:fe_user|user|username}',2
_setCustomVar.cObject.insertData = 1
}
[end]
If it doesn't, you will have to modify the plugin code to process the config value by stdWrap. Thsi code works from within the plugin controller
$configValue = $this->cObj->stdWrap('',$GLOBALS['TSFE']->tmpl->setup['config.']['tx_we_google_analytics.']['_setCustomVar.']);
or simpler (stil within the controller)
$configValue = $this->cObj->stdWrap('',$this->conf['_setCustomVar.']);

Wordpress custom query

I have a wordpress blog. I created a db table which stores dictionary information and I want to publish this data from a URL . (For ex: "myblogaddress.com/mytest.php")
I have been researching for 2 days but nothing works I tried.
In my page; I use the php code shown in blow.
<?php
global $wpdb;
$words = $wpdb->get_results("SELECT * FROM $wpdb->words")
echo $words[0]->ENG;
?>
I wonder that;
- Which directory does my php page to be into ?
- What I need to do (other config, permission etc.) to do what I want.
Regards.
If you're loading it from a standalone PHP file (ie not from within your WordPress theme), you'll have to call wp-load.php to initialise the WordPress variables (including $wpdb). Have a look at this answer, including the comment about only needing wp-load.php.
I'd consider using a relative path (what that would be would depend on where you put your page relative to WordPress) rather than using $_SERVER['DOCUMENT_ROOT'];, but that's just a personal preference.
EDIT
Rereading after seeing your comment, I've just realised $wpdb->words probably won't exist. Try
$words = $wpdb->get_results("SELECT * FROM " . $wpdb->prefix . "words")
instead. That'll generate the table name correctly as wp_words. Of course, you'll need to populate it the same way.

Multilanguage support PHP [duplicate]

This question already has answers here:
Create a webpage with Multilanguage in PHP
(10 answers)
Closed 9 years ago.
Alright, so I'm forced to create a multi-language support site in 4 languages (Greek, French, English and German). I have some ideas on how to set it up, although I would like a second opinion on this.
The first option is to include the file based on the user's settings.
/*
------------------
Language: English in ENGLISH.PHP
------------------
*/
$lang['TITLE'] = 'My website page title';
$lang['HOME'] = 'Home';
/*
------------------
Language: French in FRENCH.PHP
------------------
*/
$lang['TITLE'] = 'Titre site-web';
$lang['HOME'] = 'Accueil';
The file would be included accordingly: include_once 'ENGLISH.PHP'; etc.
The other option is to create one general file, but have the language output in an array:
$lang = array("en"=> array("TITLE"=>'My website page title',
"HOME" =>'Home'),
"fr"=> array("TITLE"=>'Titre site-web',
"HOME" =>'Accueil'));
$set = $USER_INFO->langPref(); // output: en, fr, gr, de
echo $lang[$set]['TITLE'];
The second option seems easier to manage, but I'm not sure if there are any drawbacks to this method...
Any opinions?
By the way, I was planning on translating everything myself.
Don't waste your time, use a framework, Symfony2 for example has a excellent internationalization component:
http://symfony.com/doc/current/components/intl.html
or CakePHP, even easier =)
http://book.cakephp.org/2.0/en/core-libraries/internationalization-and-localization.html
but...if you really...really want to do from scratch =) I recommend you to use something like .po files, here you are some article that could help; http://www.icanlocalize.com/site/tutorials/how-to-translate-with-gettext-po-and-pot-files/
I strongly recommend the use of a framework or even a CMS has this functionality built in...
I hope this can help.
You can used .ini to store language:
set = $USER_INFO->langPref();
if(file_exists("PATH_TO_FOLDER_LANGUAGE/".$set.'.ini')){
$arrLanguage= parse_ini_file("PATH_TO_FOLDER_LANGUAGE/".$set.'.ini');
}else{
$arrLanguage = array();
echo "Language file not found";
}
print_r($arrLanguage);
in ini file, it will be same:
TITLE=Titre site-web
HOME=Accueil'
read more about init file at here: http://en.wikipedia.org/wiki/INI_file
Hope it can help you!

Zend translate URL and language switcher

I have managed to make my URL i18n compliant using Zend_Controller_Router.
Ie:
en/user/login becomes fr/utilisateur/connexion and both URLs go to the same controller/action.
The problem I am facing is the following
I have a language switcher that is displayed as follow :
Français
English
Italiano
etc.
The currently active language doesn't have an anchor tag, but all others do.
For the languages that have an anchor on them I am building the URL and I want them to be translated in their specific languages.
Currently if I am in French all the urls get builded in french, even if I set the #local key as a param in the URL view helper (tried "#locale" => 'en', "#locale" => new Zend_Locale('en'))
en/utilisateur/connexion
it/utilisateur/connexion
instead of
en/user/login
it/utente/collegamento
because the locale used while building the URL is the one that is defined application wide.
EDIT
I digged a bit deeper in my issue an I found that only the current locale add its resources loaded, which mean I can't get route in the proper language for the route to be built in the right language
My new issue is : how do I load multiple language translation resources?
(I am planning to implement cache in the far future (near release), so I might get better performances)
Hope this helps re the new issue.
"My new issue is : how do I load multiple language translation resources?"
Just a quick caveat to say I didn't write the code below, it was taken from a example app put together by the guys at Zend however it might help to have it broken down like this.
Their approach for the example app was using a csv file containing translations and using your config file (the default one in a new project being application.ini) to specify the path to all your language translation resources.
like this:
;; Languages
language.file.en = APPLICATION_PATH "/../language/en/translate.csv"
language.file.fr = APPLICATION_PATH "/../language/fr/translate.csv"
language.file.de = APPLICATION_PATH "/../language/de/translate.csv"
language.file.es = APPLICATION_PATH "/../language/es/translate.csv"
language.name.zz = Language
language.name.en = English
language.name.fr = Français
language.name.de = Deutsche
language.name.es = Español
And in each csv file, if you are using something like Excel or Open Office to create it, column A would be the original language and column B the translation.
As an example where English is the original language and a Spanish translation is required:
A B
login entrar
logout salir
You could do that for all the words/phrases you want to translate.
If a translation isn't found then the default original word is used.
Your main application bootstrap could have something like this:
protected function _initLanguage()
{
$options = $this->getOptions();
Zend_Registry::set('language',$options['language']);
$langSess = new Zend_Session_Namespace('language');
if (!isset($langSess->translate)) {
$translate = new Zend_Translate('csv', $options['language']['file']['en']);
$langSess->translate = $translate;
}
if (!isset($langSess->locale)) {
$langSess->locale = 'en';
}
Zend_Locale::setDefault($langSess->locale);
}
As the translate object is held in the session you can use it in any of your views etc using something like:
$langSess = new Zend_Session_Namespace('language');
$translate = $langSess->translate;
and:
<?php echo $translate->_('login') ?>
where you want something to be translated on selecting an alternative language. In the example above the word login would appear when English is selected and entrar when Spanish is selected.
There's loads more to Zend_Translate than this and several ways to implement it so I'm not saying this is the best way by any means.
If it helps or if I can give you more info let me know.
Cheers,
Dave

Magento get language code in template file

I need a helper function to get the current language code. I want to use it in a templete file, like /products/view.phtml, only for testing purposes.
Does it already exist?
I have something in mind like the URL-helper
$url = $this->helper('core/url')->getCurrentUrl();
You can get the current locale code this way :
$locale = Mage::app()->getLocale()->getLocaleCode();
Result for answers given in this topic for "Belgium:French" (Be_Fr):
strtolower(Mage::getStoreConfig('general/country/default')); = be
substr(Mage::getStoreConfig('general/locale/code'),0,2); = fr
Mage::app()->getLocale()->getLocaleCode(); = fr_BE
Note that
Mage::app()->getLocale()->getLocaleCode() == Mage::getStoreConfig('general/locale/code')
but with the second one, you can specify an other store than default one (Mage::getStoreConfig('general/locale/code', $storeId)), so I would recommand it.
Afaik there is no such helper function, but you could of course build your own using:
Mage::getStoreConfig('general/locale/code', Mage::app()->getStore()->getId());
Try
$_language_code = substr(Mage::getStoreConfig('general/locale/code', $_store->getId()),0,2);
where $_store is current store object
You can also use :
$languageCode = Mage::app()->getStore()->getLanguageCode();
Don't forget to configure your store locales in your admin. Go to menu :
System -> Configuration -> General -> Locale Options
And set the correct Locale for each Websites or stores
For use in the html elements lang attribute etc.
echo strtolower(Mage::getStoreConfig('general/country/default')); // "en"

Categories