In my project I am trying to provide dynamic sizing in one of my modules. I am trying to figure out in yii2 internationalization how can I get the string length of the translated text.
for instance:
<?php
//I am getting the name from the database. Assume name to be "Hello"
$name = $gettingNameFrom->db;
//Now $name is equal to string "Hello"
//The below function will dump the output to be int(5) as the length of hello is 5
var_dump(strlen($name));
//Now I want to apply translation to the above name in the db.
// I have all my translation configured and working fine.
echo Yii::t('app','{0}',[$name]);
//I have configured fo french language.
// the above output for "Hello" in french would be "Bonjour".
?>
Now how can I get the length of the translated text? I am unable to find any help online on this topic. Any help appreciated.
Thanks!!
$translated = Yii::t('app', $name);
var_dump(strlen($translated));
Related
This is the first time that I try to generate a Word document with Laravel.
I have in database columns that I should to generate in a Word document.
For example I have the following database:
I should generate a document file which contains hello John for example
So all the elements that is inside '<<>>' are a dynamic variables .
How can I get from database the value of the column preamble and set the variable name in the same time?
I found that exists a package names phpwordn but I found that I should have a template that is a word file with dynamic variables n but in my case I have no template I have all the components of my Word file in the database.
Is it possible to do that?
If you have any idea or an example to do that help me.
You can use preg_replace_callback() to convert all special tags to their corresponding values. In your case a tag has a form of <<foo>>, so here it goes:
$values = [
'me' => 'user14053977',
'saviour' => 'RoboRobok',
];
$template = 'My name is <<me>> and <<saviour>> saved me!';
$result = preg_replace_callback(
'#<<(\w+)>>#',
function (array $matches) use ($values): string {
[$tag, $tagName] = $matches;
return $values[$tagName] ?? $tag;
},
$template
);
// $result is now "My name is user14053977 and RoboRobok saved me!"
It's a pure PHP code. Now it's just the matter of using the values in Laravel and injecting $result to your Word document.
I am working on retriving the manufacturer attribute from url
localhost/magento/index.php/test-pro.html?manufacturer/4
So i used $this->getRequest()->getParam('manufacturer')
I did not get any output.
But when i changed the url as localhost/magento/index.php/test-pro.html?manufacturer=4
(/ replaced by =), i get proper output.
But i need the url should be localhost/magento/index.php/test-pro.html?manufacturer/4
and want to fetch the product related to that manufacturer id 4.
Somebody help me.
In your query string ?manufacturer=4 will give you the value for manufacturer i.e. 4, while manufacturer/4 will give you no value as its not being treated as query string.
Also the param will be and the param will be manufacturer/4 and not manufacturer.
To achieve what you require, you can do sometinhg like below.
$currentUrl = 'localhost/magento/index.php/test-pro.html?manufacturer/4';
$parts = parse_url($currentUrl);
$val = explode('/',$parts['query']);
Mage::register('manufacturer',$val[1]);
$menuVal = Mage::registry('manufacturer');
echo $menuVal; //prints 4
This is a sample code by which you can get the query string value even if you use / instead of =.
I am getting MLNumbers from RETS Server but now I need to get all the fields of all the MLNumbers and store into my database. Could anybody help me to write query to get the whole property Object.
If you have all the Listing Numbers then you just need to write a search query to pull all the results back and process the results. I haven't ever seen a field name MLNumber but you will have to figure that out:
<?php
$search = $rets->SearchQuery("Property","RES","(MLNumber=11111,22222,33333)");
while ($listing = $rets->FetchRow($search)) {
echo "Address: {$listing['StreetNumber']} {$listing['StreetName']}, ";
echo "{$listing['City']}, ";
echo "{$listing['State']} {$listing['ZipCode']} listed for ";
echo "\$".number_format($listing['ListPrice'])."\n";
}
$rets->FreeResult($search);
I think this should return the results you need. You will need to figure out the Resource/Class/Field name you need to use for your SearchQuery().
More PHRETS examples here.
I use this code to get the country name,
Mage::getModel('directory/country')->loadByCode('DE')->getName();
in this sample I get "Germany". How can I get the translation of the country name to show for example "Deutschland" for the german language ?
Note: I don't use the code in a frontendend Module (it must be independant from the Frontend language of the customer).
Thanks a lot for help.
Of course Magento translation engine can be used in your external script:
Mage::getSingleton('core/translate')->init('de_DE', true);
$country_name = Mage::getModel('directory/country')->loadByCode('DE')->getName();
echo Mage::helper('core')->__($country_name);
Or you can use Zend_Locale class ar an alternative:
$locale = new Zend_Locale('de_DE');
$countries = $locale->getTranslationList('Territory', $locale->getLanguage(), 2);
echo $countries['DE'];
How can I read the section a certain URI belongs to?
I want to enhance the mod_breadcrumb to put section and category into the HTML. JApplication->getPathway() returns a JPathway which basically holds an assiciative array combining a name and an URL (as $list[]->name and $list[]->link). I think, it should be possible to get the section and category from a link, but don't know how.
A starting point could be the parsing into JURI-Object, but from there I don't know how get get further. Any ideas?
Pretty straight forward...
I assume you want to add category and section for the article and not your custom component.
Check if requested current URL is for article. If it is for article you know the article ID, use this article Id to go database and get catid from #__content, Use this cat_id to go to #__categories and get section (this is section id), go to #__sections to get the proper section name. All this can be done in 1 sql statement.
$breadcrumbs =& JFactory::getApplication()->getPathway();
$breadcrumbs->addItem("SECTION_NAME", JRoute::_("index.php?option=com_content&view=section&id=SECTION_ID"));
$breadcrumbs->addItem("CATEGOY_NAME", JRoute::_("index.php?option=com_content&view=category&id=CATEGORY_ID"));
$breadcrumbs->addItem("Article");
Alternatively, if you know the URL from the breadcrumb item. You can parse it and get IDS. The trick here is not to get the default URI object by JFactory::getURI() because things will get ugly, use JFactory::getURI('YOU_URI_NAME').
<?php
// You need to get Your own uri, you do not want to modify default URI
// because this will messup a lot of things
$uri = JFactory::getURI('MyCustomURI');
// Test # 1 [ID = SECTION_ID]
$url = "index.php?option=com_content&view=section&id=SECTION_ID";
$uri->parse($url);
echo "CURRENT SECTION = " . (int) $uri->getVar('id');
// Test # 2 [ID = 123]
$url = "index.php?option=com_content&view=section&id=123";
$uri->parse($url);
echo "CURRENT SECTION = " . (int) $uri->getVar('id');
?>