Can anybody help me with date localization?
My code:
<?php echo date("j F", ($data->create_time)); ?>
And it returns:
1 January
I want to translate this output to Russian language:
1 Января
Yii provides robust i18n functionality through classes such as CDateFormatter and CLocale. You can get an instance of these classes for your application's current language with Yii::app()->getDateFormatter() and Yii::app()->getLocale(). Use these to format your date with a format string either taken straight from the current locale (good if it works for you) or by specifying a custom string:
$formatter = Yii::app()->getDateFormatter();
$format = Yii::app()->getLocale()->getDateFormat('medium'); // use built-in
$format = 'd MMMM'; // or use custom
echo $formatter->format($format, $data->create_time);
i didn't run this code but looks what you are looking for.
http://kr2.php.net/strftime
Udachi^^
Related
i want to show date and days in Arabic format using Carbon. but it's showing in English.
Carbon::setlocale("ar");
echo Carbon::parse()->format('D d F Y');
Result: Sun 12 May 2019
Expected result:
it should show day and month in arabic.
You can use this package instead Date
This package contains language files for the following languages:
Albanian
Arabic
Azerbaijani
Bangla
Basque
Belarusian
Bosnian
And more!
Insatall
composer require jenssegers/date
Usage
use Jenssegers\Date\Date;
Date::setLocale('nl');
echo Date::now()->format('l j F Y H:i:s'); // zondag 28 april 2013 21:58:16
echo Date::parse('-1 day')->diffForHumans(); // 1 dag geleden
Change the config/app.php locale to your lang then apply this code:
or you can change app locale dynamically by:
app()->setLocale('ar');
Then
\Carbon\Carbon::now()->translatedFormat('l')
Or
\Carbon\Carbon::createFromDate($now->year, $now->month, $day)->translatedFormat('l')
and so on...
Carbon::setLocale() is only for the diffForHumans method, otherwise it uses the gloabl PHP Datetime locals. So if you want to use Arabic you have to call
setLocale(LC_TIME, $locale);
then use Carbon method formatlocalized()
Carbon\Carbon::now()->formatLocalized($format);
Note that PHP recognizes more than one Arabic locale so you'll have to choose one from this list
Just make sure you pick one that's installed on the server you're working on or the setLocale() method will fail and return false.
Import Carbon ( use Carbon\Carbon; ) In The Top Of Your Controller.
Use Carbon In Your Function Like This :
public function index()
{
Carbon::setLocale('ar');
// Your Code Here...
}
In Your View Use Date Like This:
<p>
{{ \Carbon\Carbon::parse($user->created_at)->translatedFormat('l j F Y H:i:s') }}
</p>
I have a model where I have this function:
public function showPrice(){
return money_format('%i€', $this->price);
}
Then in the view is used like:
<span>X {{$prod->pshowPrice()}}</span>
But instead of appear 10.00€ appear GBP10.00€.
Do you know why?
i formats the number based on the format used to describe the currency of the current locale
What you're seeing is expected if you're using a locale of en-gb
Something like money_format('%i!', $this->price) will show it with no currency info (where ! is a flag which suppresses the currency symbol)
I have a problem in Perl with a Turkish character. I have set the Turkish character set but my Turkish character isn't displaying properly.
This is my Perl script.
#!"c:\xampp\perl\bin\perl.exe"
use strict;
use warnings;
use CGI qw(:standard);
my $name = param('name');
my $surname = param('surname');
my $age = param('age');
my $gender = param('gender');
my $q = new CGI;
# We're outputting plain text, not HTML
print $q->header(-content_type => 'text/plain' , -charset => 'ISO-8859-9');
my $text = $name." ".$surname." ".$age." ".$gender." kaydı, sistemde yapılacak olan güncellemelerden sonra sisteme başarıyla eklenecektir.";
# printf("%s %s %d %d kaydı, sistemde yapılacak olan güncellemelerden sonra sisteme başarıyla eklenecektir.", $name , $surname , $age , $gender);
print $text;
How can I fix the problem?
First and foremost: Do not use the HTML generation functionality in CGI.pm. Using those leads to an unmaintainable mess of a presentation layer. Instead, use Template Toolkit based templates to separate presentation and functionality.
Second, do not use indirect object notation. That is, do not write:
my $cgi = new CGI;
Instead, write
my $cgi = CGI->new
The use of $q or $query to refer to the CGI object is weird and has it roots in the early days of the WWW. There is no reason to perpetuate it when you are learning things from scratch.
In addition, given that you have just instantiated an object, don't use plain subs such as param and don't pollute the namespace of your script. Access parameter values using:
my $value = $cgi->param('surname');
Finally, if you are going to use "interesting" characters in your source code such as Ş, save your source code as UTF-8 and specify
use utf8;
at the top of your script.
Finally, do also save all your HTML templates in UTF-8 and generate all output from your script encoded in UTF-8 and specify the document encoding as UTF-8. All other paths lead to insanity.
Also, don't use sexual as a parameter name. Use nouns as parameter and variable names. And, most infuriating to me as a Turk, Mr. (Bay) and Ms. (Bayan) are just titles and they are not appropriate choices for an input field asking about the sex of the respondent.
See also How can I deal with diverse gender identities in user profiles? That may not be on your radar in Turkey at the moment, but you will eventually encounter the issue.
Here is an untested script that might work for you:
#!"c:\xampp\perl\bin\perl.exe"
use utf8;
use strict;
use warnings;
use warnings qw(FATAL utf8);
# Provides only object oriented interface
# without HTML generation cruft
use CGI::Simple;
run( CGI::Simple->new );
sub run {
my $cgi = shift;
binmode STDOUT, ':encoding(UTF-8)';
my ($name, $surname, $age, $gender) = map $cgi->param($_), qw(name surname age gender);
print $cgi->header(-type => 'text/plain', -charset => 'utf-8');
printf("%s %s %d %d kaydı, sistemde yapılacak olan güncellemelerden sonra sisteme başarıyla eklenecektir.\n",
$name , $surname , $age , $gender);
return;
}
I think you are misunderstanding the purpose of the charset attribute on the Content-type header. Your program is emitting this header:
Content-Type: text/plain; charset=ISO-8859-9
This says to an HTTP client (a browser, for example) "I am going to send you some plain text which is encoded as ISO-8859-9". But it's important to note that the header is purely informational. It tells the world that your text is encoded as ISO-8859-9. It does not do the encoding for you. That is up to you.
This is why Borodin and others have been asking you questions which you have not been answering. Most editors will create text that is encoded either as ISO-8859-1 or UTF-8. Unless you have a special Turkish editor or you have changed the configuration of your editor, it seems very unlikely to me that you are producing text in ISO-8859-9.
If you are determined to emit ISO-8859-9 text, then you need to do that encoding yourself. You can use the encode() function from the Encode module to do that.
use Encode;
my $encoded_text = encode('ISO-8859-9', $text);
print $encoded_text;
But I wonder why you want to use a relatively obscure encoding like ISO-8859-9. UTF-8 covers all of the characters used in Turkish. Why not use that instead? Your life will become far easier if you embrace the same standards as the web.
As an aside, you have introduced a small strangeness in your code. You use CGI.pm in "functions" mode and load it in a way which imports a number of its functions into your namespace.
use CGI qw(:standard);
And then you use the param() function a few times in this way. But after that you create a CGI query object in order to call the header() method on it.
my $q = new CGI;
print $q->header(...);
You probably don't realise it, but the header() function is included in the :standard set of imports, so you can call it without creating a CGI object.
print header(...);
I used it that way in my answer to your previous question. I'm not sure why you changed the code to make it more complicated.
I should also point out that if you do want to create a CGI query object, then you shouldn't use the indirect object notation:
my $q = new CGI;
This will cause you problems at some point. Far better to write:
my $q = CGI->new;
(As demonstrated in the CGI.pm documentation)
I am migrating from Yii 1.x to Yii2.
In Yii 1.x you could define/change the localized formats in a file (sth like /app/i18n/en.php), where you could set all your desired formats, which you could later use.
Now in Yii2 this is gone?
I have 4 languages, each one has different settings. What am i supposed to do if I need a new formatting function?
E.g. I want to make a format for prices in a shop, in each lang differently
DE - 1.234,56
EN - 1,234.567
SK - 1234,5
CZ - 1 234,5678
So I create a new formatter function Yii::$app->formatter->asPrice(1234.567890).
Do I have to program a switch inside the function, and check for the language? That would be very inconvenient, and a lot of duplicity if I need more such functions. And if I had a new language later, I would have to adjust all such functions with a new case.
There must be a better solution. Any ideas?
UPDATE
I think you guys did not get my problem.. I know I can set the locale, and use the asDecimal or similar function. But the problem is that I cannot specifically customize the formatting options - it automatically takes the format defined in the intl PHP extension. I need the possiblity to specifically customize these formats. Maybe e.g. the default for EN is 2 decimals, but I need 3. Where can I set this?
In Yii2 official documentation:
You can use as below:
Yii::$app->formatter->locale = 'en-US';
echo Yii::$app->formatter->asDate('2014-01-01'); // output: January 1, 2014
Yii::$app->formatter->locale = 'de-DE';
echo Yii::$app->formatter->asDate('2014-01-01'); // output: 1. Januar 2014
Yii::$app->formatter->locale = 'ru-RU';
echo Yii::$app->formatter->asDate('2014-01-01'); // output: 1 января 2014 г.
So if you have table with language you can set locale there and in main layout define "Yii::$app->formatter->locale = $lang->locale" where $lang is object of Language model(class) matching current language.
Why dont you want to use asDecimal?
echo Yii::$app->formatter->asDecimal(1234.5678);
DE - 1.234,568
EN - 1,234.568
SK - 1 234,568
CZ - 1 234,568
Or you may try something like:
private $_priceDecimals = [ 'de_DE' => 2, 'en_EN' => 3, 'sk_SK' => 1, 'cz_CZ' => 4];
public function asPrice($value)
{
return $this->asDecimal(
$value,
isset($this->_priceDecimals[$this->locale]) ? $this->_priceDecimals[$this->locale] : null
);
}
Magento can add a suffix that is defined by the user to append onto urls. I want to get that suffix from my code. Does anyone know an easy way to do this?
If it's stored in the configuration area, then you access it just as you would any other configuration value, by using Mage::getStoreConfig($config_path) where $config_path is defined in the system.xml of the module that defines it.
If you're not sure of the $config_path, then I usually cheat and inspect the textbox/dropdown in the configuration section, take a look at the id, e.g. dev_log_file, and translate it to dev/log/file. You'll need to use some intelligence when there are multiple _ though :)
Nick's answer is good but the actual answer to this question is:
$suffix = Mage::helper('catalog/category')->getCategoryUrlSuffix();
If I am not mistaken, here is the code ( because I don't understand what you want with URL )
<?php
$currentUrl = $this->helper('core/url')->getCurrentUrl();
$url_parts = split('[/.-]', $currentUrl); // escape characters should change based your url
echo $url_parts[0]; //check here
?>
complete product url:
$productId = ***;
$productUrl = Mage::getBaseUrl().Mage::getResourceSingleton('catalog/product')->getAttributeRawValue($productId, 'url_key', Mage::app()->getStore()).Mage::helper('catalog/product')->getProductUrlSuffix();