I want to do yii file translation and a set in my protected/views/layouts/main.php:
<?php
if(preg_match('/de/', $_SERVER['HTTP_ACCEPT_LANGUAGE']))
Yii::app()->language='de';
?>
In protected/config/main.php I set 'sourceLanguage'=>'en_us', 'language' => 'en'
I have two language folders views/site/de and views/site/en.
The files from the de directory are never loaded when the Yii::app()->language='de'; is called in the layout/main view. But, when I set 'language' => 'de', in the config file it is loaded.
Is there a way to fix it, such that the language folders are used depending on the users browser language?
But why you set Yii::app()->language into the view? I think, that problem may be in it - because at this moment all translated messages are loaded. So, try to do it in controller (or in filter).
Related
To make my project support localisation for both frontend views and with Controllers for notifications/warnings etc I was considering nesting the lang files in a mirrored structure as the views themselves?
For example my view of:
app/resources/views/user/profile.blade.php
Would have a lang file of:
app/resources/lang/en/user/profile.php
Now lets say my lang file simply contains a title:
<?php
return [
'title'=>"Welcome to Your Profile",
]
?>
And my profile.blade.php simply contains this. I have attempted to access the profile.title as follows but it just displays:
<h2 class="page-title mb-0">{{__('user.profile.title')}}</h2>
<h2 class="page-title mb-0">'user.profile.title</h2>
Is this possible, I've attempted to Google this approach but all the examples I could find don't mention this but I would assume larger apps systems would need this to make localisation manageable?
From my continued research into this it appears you cannot have a set of nested folders of configs and be able to access it as I queried above.
The format works the exact same way as the config so you could have a file user.php and have an array of profile containing a key of title and access it like this:
{{__('user.profile.title)}}
It just means if you have a complex system you will end up with a lot of files or few large ones.
According to Laravel Docs: https://laravel.com/docs/5.1/localization#overriding-package-language-files
for example, if you need to override the English language lines in messages.php for a package named skyrim/hearthfire, you would place a language file at: resources/lang/vendor/hearthfire/en/messages.php.
I currently have the package activewebsite/enterprise-entity package that I am including in my project. It contains a language file:
vendor/activewebsite/enterprise-entity/src/resources/lang/en/phone.php
This file contains translations for types of phone numbers:
'phone_1' => 'Home',
'phone_2' => 'Work',
'phone_3' => 'Mobile',
'phone_4' => 'Office',
'phone_5' => 'Fax',
'phone_6' => 'Fax 2',
'phone_7' => 'Home 2',
'phone_8' => 'Direct',
Following the example above, I attempted to override this file by creating the following directory:
resources/lang/vendor/enterprise-entity/en/phone.php
containing an additional phone number specific to this project:
'phone_9' => 'Rapid Rewards Text Alert Number',
But the the translation does not come through to the front-end. The only way I've been able to get the translation to appear is to edit the language file within the enterprise-entity package.
I found this thread: https://octobercms.com/forum/post/localication-problems-where-to-overwrite-octobercms-lang-settings-in-general
Where another user seems to be having a similar issue, but it is suggested that they use the directory structure:
/lang/{locale}/{vendor}/{plugin}/lang.php
so I attempted
/lang/en/activewebsite/enterprise-entity/phone.php
with no luck.
Can anyone tell me what I may be getting wrong here? I've attempted running a php artisan optimize after each change to see if that could clear things up, but no luck.
Thanks!
Turns out the folder structure needs to use the NAMESPACE for the package, camelcase, not follow the same naming convention/directory structure as in the package or in the docs. So, for my example, the namespace used for the enterprise-entity package is EnterpriseEntity, camelcased would be enterpriseEntity.
The correct directory structure would be:
resources/lang/vendor/enterpriseEntity/en/phone.php
From Laravel Official Documentation
Overriding Package Language Files
Some packages may ship with their own language files. Instead of changing the package's core files to tweak these lines, you may override them by placing files in the resources/lang/vendor/{package}/{locale} directory.
So, for example, if you need to override the English translation strings in messages.php for a package named skyrim/hearthfire, you should place a language file at: resources/lang/vendor/hearthfire/en/messages.php. Within this file, you should only define the translation strings you wish to override. Any translation strings you don't override will still be loaded from the package's original language files.
I have added some contents to the mediawiki sidebar(Mediawiki:Sidebar). When I changed the language from English to Malayalam using the UniversalLanguageSelector, everything in the sidebar except the newly added contents got translated. My question:
What do I do to get all the sidebar contents to be translated to Malayalam?
Can I add those contents along with the translations to some file in the extension's folder so that it also will get translated?
The easiest way to localize the sidebar, is to use system message as variables. E.g., rather than adding the link *mw-mainpage-url|My mainpage in English, *mw-mainpage-url|Min huvudsida in Swedish, etc, just do *mw-mainpage-url|mainpage-description in MediaWiki:Sidebar, and then use mainpage-description/en, mainpage-description/sv, etc to change the translation of that part of the sidebar. This way you only need to change the sidebar layout in one place, and you can make use of the many translations already available, as well as create your own, custom system messages, by adding pages to the MediaWiki namespace.
Note that sidebar contents is aggressively cached. You might need to purge your pages.
Also note that $wgUseDatabaseMessages must be true (default).
i am developing web app with ZF2. It is a multilingual website. I would like to use ZF2 Translator for translation. Because, admin can modify the language variables. So, i planned to use the language folder files. I will update the language files while the admin modifying the language variables. Can anyone suggest ideas for this implementation? Thanks.
Translating based on user input can get a little messy. There is no easy way of doing that right now, as the currently supported formats are all file formats: php files with arrays, a gettext .po file or Tmx/Xliff files. For the easiest access, you would likely store the translations in a database.
You therefore need to write your own loader, loading the translations from the database. An alternative is you will write the translations in a php array and export that to a file, but I would not recommend that. For the database loader, you must implement the RemoteLoaderInterface. In very simple terms:
use My\DbTable;
use Zend\I18n\Translator\Loader\RemoteLoaderInterface;
Zend\I18n\Translator\TextDoamin;
class DbLoader implements RemoteLoaderInterface
{
protected $table;
public function __construct(DbTable $table)
{
$this->table = $table;
}
public function load($locale, $textDomain)
{
$data = $this->table->loadFromLocale($locale);
// process $data into $messages
// $messages is array(key=>value) with key translation key
$domain = new TextDomain($messages);
return $domain;
}
}
The db table loads the messages in the locale you specify. If you use text domains, pass that on too. Then you need to configure your Translator such it uses this loader too to load the message. Everything else should be fine then. In your views, you can create translations like this:
<?php echo $this->translate('my original text'); ?>
The SkeletonApp comes with Translation support built-in. However further details on how to use it can also be found in the manual -> http://zf2.readthedocs.org/en/latest/modules/zend.i18n.translating.html
I personally keep all my language files in a folder called /language
In your views you could also simply call
$this->translate('text_you_want_to_translate');
I have all my text inside view coding. I preffer to save it in app_resources, because i would to add other languages in future, and its look quite cleaner.
I just know that I need to use metatags in my view for text to connect it with file app_resource. How to it correctly, could you show me some examples how it looks like in view??
Thanks a lot and take care,
Ragims
You only need to create App_LocalResources folder in the same folder where you hold your views. Then give a name to zzz.resx. And then you can call resources in your view via zzz.
Example: *You have folder Contact in your View Folder.
Your add App_LocalResources to Contact folder. Then you give a name Contact.resx to your .resx file.
Finally in the view you can use <%= ContactResources.Contact.Index_ContactWithUs %> where Contact.Index_ContactWithUs - name of concrete resource.
Don't forget that in resources settings you can modify access type (Internal or Public).