Translate Month-Day combinations in CakePHP - cakephp

I can translate an individual month or day just fine using my .po files:
echo __('December'); //becomes diciembre
echo __('Thursday'); //becomes jueves
//...etc
But, when I use a date formate like this:
echo __(date("j F, Y")); //becomes 20 December 2012
It doesn't translate - I assume because I have translations for each month and day in individual lines.
Normally I would just do something like this:
__(date('j')) . ' ' . __(date('F')) . ' ' . __(date('Y'));
But, in the CMS, the admin is allowed to change the date to any format they want. So, it could be "j F, Y", or "Y-m-d", or... anything else.
I thought maybe I could make a helper or something, that broke apart a date into pieces, and returns each part in a __(), but - this seems overkill. Is there an easy way to do this?
I am setting my locale in the AppController:
setlocale(LC_ALL, $currentLanguage['locale']);
Configure::write('Config.language', $currentLanguage['code2']);

Turns out CakePHP has a TimeHelper i18nFormat function:
$time = time();
$timestring = $this->Time->format('Y-m-d H:i:s', $time);
$this->Time->i18nFormat($timestring, "%A %e %B %Y");
Create a file "LC_TIME" (no extension) and put it in your /Locale/ara/ folder (or replace 'ara' with whatever 3-char language code you want)
Copy the contents of CakePHP's time_test LC_TIME file and put it into yours (then save of course).
Then change it's contents to whatever language you want (I believe that example is in Spanish).
That's it!
Notes:
More details about the LC_TIME file here: http://pic.dhe.ibm.com/infocenter/aix/v6r1/index.jsp?topic=%2Fcom.ibm.aix.files%2Fdoc%2Faixfiles%2FLC_TIME.htm

The CakeTime class (and thus the TimeHelper) uses the 'cake' domain for day and month names translation. So put those translations in cake.po file instead of default.po

Related

Calendar locale (German) for PlantUML gantt diagrams

Can someone tell me, how I can define another calendar's locale (e.g. German) for the rendered calendar parts of the nice gantt feature in plantUML
I have something like:
#startgantt
-- Vorbereitung --
Project starts 2020-12-01
[Themenfindung] starts 2020-12-01 and ends 2021-01-01
[milestone] happens at 2020-12-15
-- ... --
#endgantt
The output prints month and daynames in English:
Is it possible at all and if so how can this be changed to German.
There is nothing about this in the documentation (https://plantuml.com/de/gantt-diagram).
With last beta http://beta.plantuml.net/plantuml.jar you can now specify a language for the Calendar.
For example:
#startgantt
Language DE
Project starts the 20th of september 2017
...
#endgantt
This will be integrated in next official release.
According to their source code,
there are hardcoded values for days and months as Enums
and they use name() method to get their English names.
I would recommend you to clone their repo and change the hardcoded values in two classes:
In file src/net/sourceforge/plantuml/project/time/DayOfWeek.java
replace method shortName like this:
public String shortName() {
Locale locale = Locale.getDefault();
String s = StringUtils.capitalize(java.time.DayOfWeek.valueOf(this.toString()).getDisplayName(TextStyle.SHORT_STANDALONE, locale));
return s.substring(0,2);
}
In file src/net/sourceforge/plantuml/project/time/Month.java
replace two methods like this
public String shortName() {
return StringUtils.capitalize(java.time.Month.valueOf(this.toString()).getDisplayName(TextStyle.SHORT_STANDALONE,Locale.getDefault()));
}
and
public String longName() {
return StringUtils.capitalize(java.time.Month.valueOf(this.toString()).getDisplayName(TextStyle.FULL_STANDALONE,Locale.getDefault()));
}
I compile only these two classes and replace them in jar file. This works for me.
Mike.

Cached translations in CakePHP 3.7.4

I have taken over a project on cakephp.
The problem is that I can not change translation texts.
Under src/Locale I have:
en_EN
default.mo
default.po
no_NO
default.mo
default.po
cake.pot
default.pot
In controller
public function view($id)
{
$order = $this->Order->get($id);
$this->set(compact('order'))
}
In view I have a form where is translated text by default
<?= $this->Form->control('email_message', [
'type' => 'textarea',
'rows' => 15,
'help' => sprintf('Email will be sent to %s', h($order->contact_email)),
'default' => __('pickup_mailtext')
]) ?>
No I have in
en_EN
default.po
msgid "pickup_mailtext"
msgstr "This is the old pickup mailtext"
if I change it to
msgid "pickup_mailtext"
msgstr "This is the NEW pickup mailtext"
Nothing changes. I have deleted everything in persistent directory.
Also in Config/app.php default language is set to no_NO, but as I mentioned before this string is under en_EN
under
no_NO
default.po
There is:
msgid "pickup_mailtext"
msgstr ""
I have also noticed, tad this string is in
en_EN
default.mo
but if I try to modify it I get Internal server error.
So my question is:
1. How to get this translation working? Why this string is not changing?
2. If the default language is set to no_NO, then why the translations is in en_EN and why it is getting translated instead of being empty?
3. How to clear those .mo files?
Thanks
.mo files are the compiled binary versions of the respective .po files, you can't just modify them with a text editor, you need to recompile the .po files instead, using a program like msgfmt, or one with a GUI like Poedit.
CakePHP will by default prefer .mo files over .po files (the former are usually faster to parse), ie if a .mo file exists it will be used instead of a possible .po file with the same name, so if you change only the .po file, nothing will happen as that file is not being used.
If changing the default locale in config/app.php has no effect, then the locale might get changed somewhere else in your app. Check \Cake\I18n\I18n::getLocale() in your view template to figure what locale is actually being used at that point, and set a breakpoint in \Cake\I18n\I18n::setLocale() (vendor/cakephp/cakephp/src/I18n/I18n.php) or log a stacktrace to figure from where in the code the locale is being set.

How to make Text::slug() convert german umlauts properly?

I am using CakePHP 3.6, and when I am using words with german umlauts like:
Text::slug('Grundstücke')
I will get:
Grundstucke (where ü = u)
but that's not correct, I should get:
Grundstuecke (where ü = ue)
Is there an option to set so that umlauts are being converted the way I want them to?
Change your transliterator
The Text::slug() uses internally transliterator_transliterate (see php doc).
So you need to change the default transliterator that is being used.
After some research I found one that will work for you.
At the end of your bootstrap.php file add:
\Cake\Utility\Text::setTransliteratorId( 'de-ASCII; Any-Latin; Latin-ASCII; [\u0080-\u7fff] remove ');
Then your text will be converted as you expect.
Notes
Resources I've used to find this answer:
CakekPHP Text::transliterate()
Transliteration Identifiers Documentation
transliterator_list_ids - to get a list of valid identifiers - this is how I found the one that finally worked: de-ASCII
Text Utility API - to set the a new default transliterator id.

Change Joomla 3 page title and site name separator from '-' to '|'

how would i change the joomla page title and site name separator from - to |. I know which file generates the file. It's located at C:\xampp\htdocs\Yoursitename\libraries\joomla\document\html\renderer\head.php
$buffer .= $tab . '<title>' . htmlspecialchars($document->getTitle(), ENT_COMPAT, 'UTF-8') . '</title>' . $lnEnd;
I just can't figure out where the getTitle function is coming from.
Thanks
It uses a language constant JPAGETITLE which in english is set to %1$s - %2$s
You can use the language override manager in your Joomla backend to change that.
The title itself is usually set in the view you look at. There JDocument->setTitle() is used. Like for example here for com_content, article view: https://github.com/joomla/joomla-cms/blob/staging/components/com_content/views/article/view.html.php#L256

Different coding sets in database and website

I have a website with very simple news system (posting, editting, deleting etc). All my html pages are saved in UTF-8 formatting, everything displayes correctly.
I specify using UTF in every header:
For saving news to database, I use simple scripts like (all values come from a html form):
$newsTitel = isset($_POST['title']) ? $_POST['title'] : 'Untitled';
$submitDate = $date = date('Y/m/d');
$content = isset($_POST['newstext']) ? $_POST['newstext'] : 'No content';
include 'includes/dbconnect.php';
mysql_query("SET CHARACTER SET utf8");
mysql_query("SET NAMES 'utf8'");
$query = mysql_query("INSERT INTO news SET date='$submitDate',subject='$newsTitel',news='$content'");
The data get saved to database but in a weird format (coding). There are characters like à ¡ Ä etc which makes the content almost unreadable. Other problem is that when loading this content back to html forms (for editting news) it displays in this weird coding. When I looked into the specification of the database I use, it says that it saves data in UTF-8.
I use phpMyAdmin to access the MYSQL database.
So to sum it up:
Pages: saved in UTF8, all have correct header
Database: interaction with the server: utf8_czech_ci, tables in the same format
What I do not understand at all is this strange bevaior:
1) I save the data into the database using the script above
2) I take a look into phpMyAdmin and see broken encoding
3) I load the data back into my website and display them using this:
<?php
include 'includes/dbconnect.php';
$data = mysql_query("SELECT * FROM news ORDER BY id DESC limit 20") or die(mysql_error());
while($info = mysql_fetch_array( $data ))
{
echo '<article><h3> '.$info['subject'].'</h3><div id="date">'.$info['date'].'</div>';
echo '<p>'.$info['news']. '</p></article>';
}
?>
The encoding is correct and no weird characters are displayed.
4) I load the exact same data into a html form (for edition purposes) and see the same broken encoding as in the database.
What happened? I really dont get it. I tried fixing this by re-saving everything in utf8, alterign tables and changing their encodings into different utf8 versions etc...
This is example of a data I pass to the database (it is in czech with html tags):
<p>Vařila myšička kašičku</p>
<img src="someImage.jpg">
<p>Další text</p>
Thanks for any help...
The commands for specifying the character set should be:
set names 'utf8';
If you check the result returned from your queries at the moment, what does it say? If I try it in the monitor I get the following:
mysql> set names 'UTF-8';
ERROR 1115 (42000): Unknown character set: 'UTF-8'
Have you tried using set names 'utf8' before connecting for the SELECT as well? The characters you're saying are output make me think you're getting back the correct bytes for UTF-8, but they're being interpreted as ISO-8859-1.
You are not escaping single quotes or some other html chars.
Use mysql_real_escape_string.
$newsTitel = isset($_POST['title']) ? mysql_real_escape_string($_POST['title']) : 'Untitled';

Resources