Translations in sourceLanguage does not work in Yii2 application - translation

I'm using a translations based on keywords in my Yii2 application (I know, that this isn't best option, but I don't have other). I've prepared #app/messages/pl/app.php and #app/messages/en/app.php files with translation strings using keywords, instead of full-featured sentences or words:
<?php
return [
'name_english'=>'Name in English',
'keywords_english'=>'Keywords in English'
];
?>
I have set my application to use Polish language as default:
'language' => 'pl',
'sourceLanguage' => 'en',
And I'm invoking translation:
Yii::t('app', 'keywords_english');
Everything works fine, when language is actually set to base, Polish (pl):
But, when I change it to English (en; either by setting Yii::$app->language during runtime or by changing application configuration), translation is not performed and I'm getting keywords_english:
I have put die() in the beginning of #app/messages/pl/app.php and #app/messages/en/app.php files and I can clearly see, that when language is set to English, second file is not being included by Yii2 (application run follows), while, when language is Polish, first file is included and application flow breaks on that die().
What am I missing? Why Yii2 is not using translations from #app/messages/en/app.php file, if language is set to English (en)?
EDIT: By default, I was not altering default i18n component configuration in my application's configuration as I found no need for that. Translation files are stored in default position (#app/messages/<language>/) and are using default class (yii\i18n\PhpMessageSource). And this is working for all languages except sourceLanguage. At some point, I tried to alter configuration:
'i18n' => [
'translations' => [
'*' => [
'class' => 'yii\i18n\PhpMessageSource',
'sourceLanguage' => 'en',
'basePath' => '#app/messages'
],
],
],
But, it brought no change (why should it -- it still uses default settings).

According to samdark at Yii Forum, this is by design. Translations are not performed, if language = sourceLangage.
To workaround this, and force translations in this case, one must set forceTranslation to true.
Therefore, one must add / modify i18n component in components section of application's config in the way similar to this:
'i18n' => [
'translations' => [
'app' => [
'class' => 'yii\i18n\PhpMessageSource',
'forceTranslation' => true
],
],
],

Solution:
'translations' => [
'app*' => [
'class' => 'yii\i18n\PhpMessageSource',
'basePath' => '#app/messages',
'sourceLanguage' => 'en_US',
'fileMap' => [
'app' => 'app.php',
],
],
Answers for your comment:
1) 'sourceLanguage' => 'en_US' - You must use full locale. Because English locale may be en_US, en_UK and e.t.c. The format for the language/locale is ll-CC where ll is a two- or three-letter lowercase code for a language according to ISO-639 and CC is the country code according to ISO-3166. from [doc][1]
2) In key use category. And category set in Yii::t('category'...)

Related

Dynamic setting language in Golang web framework Iris

I have these language files:
locales/
en-US/
a.yaml
b.yaml
zh-CN/
a.yaml
b.yaml
DA/
a.yaml
b.yaml
set default language:
app.I18n.SetDefault("en-US")
How to set language dynamically according to l parameter:
www.sete.com/xx/xxx?l=en => set en-US
www.sete.com/xx/xxx?l=cn => set zh-CN
www.sete.com/xx/xxx?l=da => set DA
www.sete.com/xx/xxx?l=NotFound => default en-US
In addition, whenI set a language that doesn't exist:
www.sete.com/xx/xxx?l=NotFound
I get an error in response like this:
{
"user": "yaml%!(EXTRA string=Tom....)"
}
So , what should I do to better solve these problems? I tried my best, my English is not good, please help me.....
You can find Iris i18n examples at: https://github.com/kataras/iris/tree/master/_examples/i18n
If the language was not found, then the default language's key will be shown instead unless app.I18N.Strict is true. If the Strict field is false and the default language has no available key for the translation then the app.I18n.DefaultMessageFunc will be fired instead, you can take a look on how to configure what happens if a key does not exist at: https://github.com/kataras/iris/blob/7b6a8f1e26469ab3ae53cfe468d6e5202c75c2a8/_examples/i18n/basic/main.go#L38-L47

Setting up fast_gettext search path

I am trying to localize an existing Ruby on Rails project, and I have decided to use fast_gettext. I have tried to set things up as described on the github page (https://github.com/grosser/fast_gettext) and in this small tutorial (http://blog.lingohub.com/developers/2013/08/ruby-gettext-internationalization-tutorial-fest-gettext-gem/). However, when I do
rake gettext:find
it appears that only my .rb files are being searched and not my other files (importantly, the .erb files are not being checked).
In addition to updating my bundle to include the necessary gems, this is what I've done so far:
Added config/initializers/fast_gettext.rb. Here it is:
# config/initializers/fast_gettext.rb
FastGettext.add_text_domain 'app', :path => 'config/locales', :type => :yaml
FastGettext.default_available_locales = ['en','es']
FastGettext.default_text_domain = 'app'
I have also created lib/tasks/gettext.rake
namespace :gettext do
def files_to_translate
Dir.glob("{app,lib,config,locale}/**/*.{rb,erb,haml,slim,rhtml}")
end
end
This is index.html.erb
<div class="home_title"><%= t(:xyz) %></div>
I am setting the locale in application_controller.rb, and if I manually modify the locale file (e.g. config/locales/es.yml), the text gets translated when I open the page. However, when I run gettext, it does not create an entry for this item. I end up with an empty app.pot:
# File headers....
# Copyright, my name, etc.
#
#, fuzzy
msgid ""
msgstr ""
"Project-Id-Version: app 1.0.0\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2014-03-19 16:52-0700\n"
"PO-Revision-Date: 2014-03-19 16:52-0700\n"
"Last-Translator: FULL NAME <EMAIL#ADDRESS>\n"
"Language-Team: LANGUAGE <LL#li.org>\n"
"Language: \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=INTEGER; plural=EXPRESSION;\n"
As I said above, I believe that gettext is only checking .rb files (based on some hacked logs that I placed inside the gettext code). It could also be that I need to do something extra to configure gettext to recognize this is a string to be localized:
<%= t(:xyz) %>
Thanks in advance for your suggestions.

How to configure 403 template in bjyauthorize

I just setup the bjyoungblood/bjy-authorize and wonder how I can tell Zend Framework 2 where my error/403 template lies.
I didn't configure the initial setting of 'template' => 'error/403',
The 403.phtml file lies within the vendor directory but I get the following error message:
Warning: include(C:\myproject\config\autoload/../view/error/403.phtml) [function.include]: failed to open stream: No such file or directory in C:\myproject\vendor\zendframework\zendframework\library\Zend\View\Renderer\PhpRenderer.php on line 507
What is wrong with my configuration?
For performance reasons (mainly avoiding stat calls), BjyAuthorize uses the template map to define which file to use when the error/403 view is requested. This is an option of the view_manager settings, as described in the Zend\View documentation.
To set your own, you can simply define something like following in your config/autoload/your-settings.local.php:
'view_manager' => array(
'template_map' => array(
'error/403' => '/absolute/path/to/your/error/403.phtml',
),
),
Or, in your module config:
'view_manager' => array(
'template_map' => array(
'error/403' => __DIR__ . '/view/error/403.phtml',
),
),
I suggest always providing absolute paths for configuration, so be sure that your file C:\myproject\config\autoload/../view/error/403.phtml is the correct path.
You can also use a different view for 403 errors if you prefer to do so. That can be achieved by changing $config['bjyauthorize']['template']:
'bjyauthorize' => array(
'template' => 'my-module/unauthorized-template',
),
You can manually set:
'error/403' => __DIR__ . '/../view/error/403.phtml',
in module/Application/config/module.config.php and comment out this line in module.bjyauthorize.global.php. I'm assuming you are working with the skeleton.
#aravind.udayashankara, BjyAuthorize does work with composer and the skeleton already comes configured to load modules in the vendor as well, so no need to move it in modules directory.
However, it does seem that phprender isn't able to access the view directory in the vendor. It might be the way "error/403' => DIR . '/../view/error/403.phtml" is actually mapped/interpreted at runtime.

Redcarpet & Middleman: :with_toc_data

I'm wondering about using Redcarpet's :with_toc_data option for Markdown working with Middleman (a Sinatra-based static site generator).
Our current config.rb:
set :markdown, :layout_engine => :haml
set :markdown_engine, :redcarpet
This doesn't work:
set :markdown, :layout_engine => :haml, :with_toc_data => true
set :markdown_engine, :redcarpet
Any help is very appreciated!
Fixed in Middleman 3.0 by Thomas Reynolds: https://github.com/middleman/middleman/issues/442
It appears that from Issue #200 of Middleman at Github, this should be done as such:
set :markdown, :layout_engine => :haml
set :markdown_engine, :redcarpet
set :redcarpet, :with_toc_data => true
The third line being the key. I also can't make this work, so there might be something still open as a bug with Middleman?
The latest release is 2.0.15.3, which is what I have installed; but I also can't make it work. Perhaps Issue #200 should be re-opened?
I have this exact code in my config.rb:
###
# GitHib flavoured Markdown, I can't go back!
###
set :markdown_engine, :redcarpet
set :redcarpet, fenced_code_blocks: true, autolink: true
I'd be eager to understand if I am doing something incorrectly. (I'm specifically trying to use this in a Middleman Blog)
Update to my answer: The commit referenced in Issue #200 does not exist in the 2.0.15.3 release, thus we'll have to use something newer.

rails 3, xml formatting and builder

I have an xml tag that needs to be formatted like so:
<AddDealRequest xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
I can't seem to get this to work properly, using builder. I am attempting the following code in builder:
xml.AddDealRequest(:xmlns:xsi => "http://www.w3.org/2001/XMLSchema-instance", :xmlns:xsd => "http://www.w3.org/2001/XMLSchema" ) do
but obviously that second colon is throwing off the symbol. Is there any way to escape that second symbol? Or is this declaration entirely necessary?
Thanks!
Try quoting your symbols:
xml.AddDealRequest(
:'xmlns:xsi' => "http://www.w3.org/2001/XMLSchema-instance",
:'xmlns:xsd' => "http://www.w3.org/2001/XMLSchema"
)
You could also try using strings instead of symbols
xml.AddDealRequest(
'xmlns:xsi' => "http://www.w3.org/2001/XMLSchema-instance",
'xmlns:xsd' => "http://www.w3.org/2001/XMLSchema"
)
but I don't know if builder will be happy with that but the documentation includes things like this:
xm.target("name"=>"compile", "option"=>"fast")
# => <target option="fast" name="compile"\>
so strings for the attribute names should work.
A bit of time in irb might be help clarify things:
>> 'where_is:pancakes_house'.to_sym
=> :"where_is:pancakes_house"
>> :'xmlns:xsi'.to_s
=> "xmlns:xsi"
Rather than expect anyone to read through all the comments in the earliest answer, I'll just post the outcome here:
Firefox doesn't display the xmlns attribute (at least not when it matches a default). If you view the source (Ctrl+U) or use Chrome as your browser, you'll see that the missing attributes are appearing in the xml output.

Resources