On my page I have a date variable. I wish to translate it's shortened month name by locale (in my case Lithuanian).
I've set up the translations in langed/lang/lt_LT.yml:
Month:
Jan: 'Sau'
Feb: 'Vas'
Mar: 'Kov'
Apr: 'Bal'
...
And in my template when I put
$Date.Format(M d)
It gives the month and day always in English, no matter the locale (ex. "Apr 18", I need "Bal 18" in this case).
I have attempted to try to put the variable of Month into translation quotes of .ss template:
<%t Month.$Date.Format('M') %>
But it doesn't work. It throws an error:
"[User Error] Uncaught SSTemplateParseException: Parse error in template on line 16. Error was: Malformed opening block tag t. Perhaps you have tried to use operators?"
Can anyone please explain to me how am I doing this wrong?
You can use $Date.FormatI18N('%b') which utilizes strftime() which in turn needs your locale installed on the server to translate it. If not you just get the english (or default) locale.
See this gist for an extension to get the current locale in your controller.
The way I've implemented this is put a function on the controller:
public function MonthForVar($var) {
return _t("Month.$var");
}
and then you can call $MonthForVar($Date) in your template. Don't forget if you're doing it in a loop to go $Up.MonthForVar($Date)
** Should add that wmk's answer is better for date format however I was lead here from Google when I'm trying to translate something non-date related, in which case this should work.
Related
I’d like to add a text line saying that “The latest event has been ended 3 days ago”
And the part “3 days ago” will automatically change as the day goes by, e.g. 1 month ago and so on…. Is there any shortcode plugin I can use for this? Or any method?
I found this plugin https://wordpress.org/plugins/the-time-ago/ but it seems to work with post/page/comment time. Not for a text line.
Best.
If you want to use a custom timestamp for the event and know how to write basic php code, you can use the php's DateTime::diff method or better, use Carbon to get the difference between your custom timestamp and today's date.
Examples in the linked docs should suffice in understanding and to echo the difference between the dates. Now, use that inside WordPress, write that code in a function in your theme's functions.php file and return the value to create a shortcode for the difference.
function eventdiff_func( $atts ) {
// write code which calculates the difference here and store it in $diff
return $diff;
}
add_shortcode( 'event_diff', 'eventdiff_func' );
Now you can use the [event_diff] shortcode whereever you'd like to output the difference that you calculated.
You can use human_time_diff function for the difference is returned in a human readable format. you can read more about human_time_diff click here.
I want to format a variable as currency before displaying on the template. Now I know I can create a language specific dictionary file and use it to format it as currency, but is there a way to do it directly?
Yes, you can use the format() method:
echo $f3->format('Price: {0,number,currency}',29.90);//Price: $29.90
See here for more examples.
From within a template, the syntax would be:
{{'Price: {0,number,currency}',#price|format}}
I'm trying to solve a Internationalization problem with CakePHP but I cant get cake to start localizing... even though I have everything set up as the documentation requires... my problem is twofold:
first I'd like cake to localize all __(''); strings I defined in the views. The second thing is that I have a datetime input field which I'd like to localize as well...
currently my Locale folder looks as follows:
/Locale
-cake_dev.pot
-cake.pot
-default.pot
-/DE/LC_MESSAGES/
--default.pot
and in my /config/bootstrap.php the last line is:
Configure::write('Config.language', 'DE');
thx so much for your support!
Indeed it should be deu. And you need rename deu/LC_MESSAGES/default.pot to deu/LC_MESSAGES/default.po, open it in poedit, update it (catalog) from default.pot and translate it. On save deu/LC_MESSAGES/default.mo will be compiled, this file is used by cake.
I'm not sure if Cake internally converts DE to DEU, further it should be lower cased. Systems other than windows are case sensitive. So try "deu" instead of "DE".
I am currently working on a project built in codeigniter . I am trying to send date in d/m/Y in url , eg www.mydomain.com/guwahati-project-starting-12/12/2013. But if I want to extract the controller name
$news_title = $this->uri->segment(1);
the above $news_title will only extract upto guwahati-project-starting-12. Is there any way I can extract the full string except replacing / with - ?
If your controller name must include a date (I am assuming your full controller name is guwahati-project-starting-12/12/2013), then my recommendation would be to switch to using dd-mm-yyyy , as your date format will cause problems with codeigniters uri helper.
If you have to use dd/mm/yyyy then you could use $this->uri->uri_string() to get the full uri and then deal with the segmentation yourself.
Using PHP's urlencode or rawurlencode function might work. Also, in your config.php file that's located in the application/config folder, you might need to edit it as follows.
$config['permitted_uri_chars'] = 'a-z 0-9~%.:_,()&\/-';
Make sure to add the "/" and in there.
why not using urlencode ?
http://php.net/manual/en/function.urlencode.php
and then when you want to load the date just urldecode
http://php.net/manual/ru/function.urldecode.php
url_title() function is available in the URL Helper... just load it and run it through this function.
This function will filter the string for characters and allows characters in the defined characters list in your config file (which you should most likely not edit)
--- sorry my answer isn't complete ---
few option, 1 is, why not use multiple segments... the "/" between your date make it different segments.
www.mydomain.com/guwahati-project-starting-12/12/2013
being guwahati-project-starting-12 = segment 1,
12 segment 2, and 2013 segment 3.
this will fail though, because segment 1 is class and 2 is controller...
try making
www.mydomain.com/class/function/guwahati-project/12/12/2013
now you have segment 3 your title, 4 your day, 5 your month and 6 your year
another option, which i think is an overkill but is secured and url friendly
is encrypting and decrypting it using. HASHIDS. like youtube and bitly does.
a link like this
www.mydomain.com/guwahati-project-starting-12/12/2013
can be made to
www.mydomain.com/guwahati-project-starting/hjDm
you can handle the parameter easily. Just dont forget to cast the date as int
In php (or maybe gettext in general), what does gettext do when it sees a variable to dynamic content?
I have 2 cases in mind.
1) Let's say I have <?=$user1?> poked John <?=$user2?>. Maybe in some language the order of the words is different. How does gettext handle that? (no, I'm not building facebook, that was just an example)
2) Let's say I store some categories in a database. They rarely, but they are store in a database. What would happen if I do <?php echo gettext($data['name']); ?> ? I would like the translators to translate those category names too, but does it have to be done in the database itself?
Thanks
Your best option is to use sprintf() function. Then you would use printf notation to handle dynamic content in your strings. Here is a function I found on here a while ago to handle this easily for you:
function translate()
{
$args = func_get_args();
$num = func_num_args();
$args[0] = gettext($args[0]);
if($num <= 1)
return $args[0];
return call_user_func_array('sprintf', $args);
}
Now for example 1, you would want to change the string to:
%s poked %s
Which you would input into the translate() function like this:
<?php echo translate('%s poked %s', $user1, $user2); ?>
You would parse out all translate() functions with poEdit. and then translate the string "%s poked %s" into whatever language you wanted, without modifying the %s string placeholders. Those would get replace upon output by the translate() function with user1 and user2 respectively. You can read more on sprintf() in the PHP Manual for more advanced usages.
For issue #2. You would need to create a static file which poEdit could parse containing the category names. For example misctranslations.php:
<?php
_('Cars');
_('Trains');
_('Airplanes');
Then have poEdit parse misctranslations.php. You would then be able to output the category name translation using <?php echo gettext($data['name']); ?>
To build a little on what Mark said... the only problem with the above solution is that the static list must be always maintained by hand and if you add a new string before all the others or you completely change an existing one, the soft you use for translating might confuse the new strings and you could lose some translations.
I'm actually writing an article about this (too little time to finish it anytime soon!) but my proposed answer goes something like this:
Gettext allows you to store the line number that the string appears in the code inside the .po file. If you change the string entirely, the .po editor will know that the string is not new but it is an old one (thanks to the line number).
My solution to this is to write a script that reads the database and creates a static file with all the gettext strings. The big difference to Mark's solution is to have the primary key (let's call it ID) on the database match the line number in the new file. In that case, if you completely change one original translation, the lines are still the same and your translator soft will recognize the strings.
Of course there might be newer and more intelligent .po editors out there but at least if yours is giving you trouble with newer strings then this will solve them.
My 2 cents.
If you have somewhere in your code:
<?=sprintf(_('%s poked %s'), $user1, $user2)?>
and one of your languages needs to swap the arguments it is very simple. Simply translate your code like this:
msgid "%s poked %s"
msgstr "%2$s translation_of_poked %1$s"