qTranslate and Wordpress functions - qtranslate

I'm using qTranslate on a Wordpress site but am having a little trouble working some bits of it into the Wordpress functions in some cases. For example, when I want to include a "Read More link" within a query, I can just do:
echo _e('[:en]Read More_[:ru]читать далее_');
(that said, it looks like it's echoing an echo, so I may not be doing that right, even though it seems to work).
But I can't quite work out the syntax for this:
echo comments_number( '<div class="noComment"></div>No Comments', '<div class="oneComment"></div>One Comment', '<div class="oneComment"></div>% Comments' );
How do I put in the translation for those?

try this:
echo comments_number(
qtrans_useCurrentLanguageIfNotFoundUseDefaultLanguage('<div class="noComment"></div>[:en]No Comments[:es]No comentarios'),
qtrans_useCurrentLanguageIfNotFoundUseDefaultLanguage('<div class="oneComment"></div>[:en]One Comment[:es]Uno comentario'),
qtrans_useCurrentLanguageIfNotFoundUseDefaultLanguage('<div class="oneComment"></div>[:en]% Comments[:es]% comentarios)'
);

Related

put https link in i18n translation file in react

I'm using i18n into translate my multi language react project.
I have a sentences that contain (:) character:
"e.g https://192.168.1.15 or https://example.com"
and I want to translate that to Persian like this:
"مثال: https://192.168.1.15 یا https://example.com"
I put that in my translation file like this:
export default {
"e.g https://192.168.1.15 or https://example.com": "مثال: https://192.168.1.15 یا https://example.com",
}
but it doesn't work. I realized the ":" is the issue . What I see is this:
"//192.168.1.15 or httpsfalse//example.com"
How can I Use (:) character in my translation in i18n?
":" has a special meaning when inside a translation key, it means what is the namespace which the key belongs to.
I had similar issue with one of mine translation keys, the solution was to ignore the namespace in that specific key.
t("Welcome to React :)", {nsSeparator: false})
// ---------------------------^ this will make the lib to ignore the ":"
For more info, you can read this bug
I used this solution:
t("e.g.", {
urlIP: "https://192.168.1.15",
urlDomain: "https://example.com",
})
and put this code in my translation file:
en.js:
"e.g.": "e.g. {{urlIP}} or {{urlDomain}}",
fa.js:
"e.g.": "مثال: {{urlIP}} یا {{urlDomain}}",
It's not the best solution and I think there is a better way to resolve this issue, but it works for now.
If you know better solution, I am glad to hear from you.

How to use template parameters in page content in hugo

Is it possible to use template parameters in the content of a post with Hugo? E.g. if I have the following parameters:
[params.company]
name = "My Company"
Can I then do something like this in the content of a post?
This site, {{ .Site.BaseURL }} is operated by {{ params.company.name }}
I've tried, but it's literally printing the above instead of interpolating the variables.
1. Front matter way
As far as I'm aware, it's not possible* to put variables within the markdown file's content because MD parser would strip them, but it's possible to do it using custom variables on the front matter of each .md content file. The Hugo engine can target any fields you set in front matter. Front matter fields can be unique as well.
In your case, the template which is called to show the rendered .MD file has access to front matter parameters and you can change template's behaviour (like add classes of extra <div>'s) or even pull the content right from the parameter.
For example, on my .md files I have:
---
title: "Post about the front matter"
<more key pairs>
nointro: false
---
The key nointro: true would make my first paragraph to be normal size. Otherwise, if key is absent or false, first paragraph would be shown at larger font size. Technically, it's adding a custom class on a <div>.
In the template, I tap into the custom parameter nointro this way:
parent template which shows your markdown file, which has front matter parameters:
<div class="article-body {{ if .Params.nointro }} no_intro {{ end }}">
{{ .Content }}
</div><!-- .article-body -->
Notice I can't put variables within {{ .Content }}, but I can outside of it.
For posterity, that's piece of the content from a file hugo/themes/highlighter/layouts/partials/blog-single-content.html, it's a partial for single post content. You can arrange your partials any way you like.
Obviously, that's Boolean parameter flag, but equally it could be content which you could use directly:
MD file's top:
---
title: "One of our clients"
<more key pairs>
companyname: "Code and Send Ltd"
---
Text content is put here.
Then, reference it like this (extra insurance against blank value using IF):
Anywhere in Hugo template files:
{{ if .Params.companyname }}{{ .Params.companyname }}{{ end }}
2. Using config.(toml/yaml/json)
Now, looking at your example, "This site is operated by " would almost warrant a custom field in more global location, for example, hugo/config.toml. If I wanted to add a companyname into my config, I'd do it this way:
hugo/config.toml:
BaseURL = "_%%WWWPATH%%_"
languageCode = "en-uk"
title = "Code and Send"
pygmentsUseClasses = true
author = "Roy Reveltas"
theme = "Highlighter"
[params]
companyname = ""
Then I'd use it anywhere via {{ .Site.Params.headercommentblock }}.
I guess if you want your client pages to be static pages then single location might not be the best and you might want to tap into front-matter. Otherwise, if it's a site's footer, this way is better. Alternatively, you could even put this data even on data files.
3. Using custom placeholders and replacing via Gulp/npm scripts
I said not possible*, but it's possible, although unconventional and more risky.
I had such setup when I needed two builds for my website: 1) Prod and 2) Dev. Prod URL's were coming from two places: CDN and my server. Dev had to come from a single place in a static folder because I wanted to see images and was often working offline on a train.
To solve that, I used two custom variables in all my templates (including markdown content): _%%WWWPATH%%_ and _%%CDNPATH%%_. I came up with this unique pattern myself by the way, feel free to adapt it. Then, I put it also on hugo/config.toml as:
hugo/config.toml:
BaseURL = "_%%WWWPATH%%_"
After Hugo happily generated the website with those placeholders, I finished off the HTML's using a Grunt task:
grunt file:
replace: {
dev: {
options: {
patterns: [{
match: /_%%CDNPATH%%_+/g,
replacement: function () {
return 'http://127.0.0.1:1313/'
}
}, {
match: /_%%WWWPATH%%_+/g,
replacement: function () {
return 'http://127.0.0.1:1313/'
}
}...
For posterity, I recommend Gulp and/or npm scripts, I'd avoid Grunt. This is my older code example above from the days when Grunt was the best.
If you go this route, it's riskier than Hugo params because Hugo won't error-out when your placeholder values are missing or anything else wrong happens and placeholders might spill into the production code.
Going this route you should add multiple layers of catch-nets, ranging from simple following Gulp/Grunt/npm scripts step which searches for your placeholder pattern to pre-commit hooks ran via Husky on npm scripts that prevent from committing any code that has certain patterns (for example, %%_). For example, at a very basic level, you would instruct Husky to search for anything before allowing committing this way:
package.json of your repo:
"scripts": {
"no-spilled-placeholders": "echo \"\n\\033[0;93m* Checking for spilled placeholders:\\033[0m\"; grep -q -r \"%%_\" dist/ && echo \"Placeholders found! Stopping.\n\" && exit 1 || echo \"All OK.\n\"",
"precommit": "npm run no-spilled-placeholders"
},
Basically, grep for pattern %%_ and exit with error code if found. Don't forget to escape the code because it's JSON. I use similar (more advanced) setup in production and nothing slips through. In proper setup you should creatively look for anything mis-typed, including: %_, _%, %__, __% and so on.
Normal Go template is not supported in the markdown file, but shortcodes are:
{{< param "company.name" >}}
To access arbitrary other Go template values, create a custom shortcode for it and call that from your markdown file.
For your example, you need the site's baseUrl, so save this as layouts/shortcodes/base_url.html:
{{ .Site.BaseURL }}
And write this in your markdown file:
+++
[company]
name = "My Company"
+++
This site, {{< base_url >}} is operated by {{< param "company.name" >}}
There is also the shortcode param : {{< param "companyName" >}} : https://gohugo.io/content-management/shortcodes/#param

CakePHP: Cannot read from a custom cache in 2.8.4?

This is painfully simple, but I cannot determine why it simply will not work as the Cookbook suggests it will. I am getting a blank result when I run the following:
Cache::write('req_quals', $value, 'permacache');
Cache::read('req_quals', 'permacache');
The config looks like:
Cache::config('permacache', array('engine' => 'File', 'path' => CACHE . 'permacache' . DS, 'duration' => '+9999 days'));
The write works. I know this because I'm looking directly into the tmp/cache/permacache folder and I see the file with its contents inside.
I can write/read this value without any problem if I remove the 'permacache' from both lines.
Am I missing something obvious?
When Cake calculates duration, +9999 days returns a negative duration. You should avoid being a cool guy and just use +999 days as the documentation subtly suggests.

Translate Month-Day combinations in 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

Drupal 7 - node custom display

I've got this problem. I've created file node--mycontenttype.tpl.php to display nodes in custom way. I've listed all the $content array by print_r($content). I can display all the variables except CCK fields. For example I can print out node type like:
<?php print $content['body']['#bundle']; ?>
But if I try to display any CCK field like:
<?php print $content['body']['#object']->field_url[und][0]['value']; ?>
It gives me an error "Notice: Use of undefined constant und - assumed 'und' w include()". Alright, so the "und" means "undefined" for langauge, but nor 'pl', nor 'en' solves the problem. How can I manage this?
Alright, after researching I've finally found an answer. It works, but in some cases it look a bit inefficient. Code goes like this:
<?php
$output = field_get_items('node', $node, 'field_url');
$output = $output[0]['safe_value'];
print $output;
?>
But if you have a lot of CCK fields it looks like you have to launch field_get_items() function a lot of times. If any of you knows a better approach it could be nice you could share.
When you write [und] Drupal (PHP) assumes that there's variable $und defined somewhere in the code.
You should use:
<?php print $content['body']['#object']->field_url['und'][0]['value']; ?>

Resources