I wrote a backend hook so that I can write notification E-mails as soon as an item is set to hidden = 0 in typo3. I managed to access LocalizationUtility to access my translation files, like this:
$localization = $objectManager->get('\TYPO3\CMS\Extbase\Utility\LocalizationUtility');
$localization::translate('tx_extplugin_domain_model_item.email.text1', 'ExtPlugin')
But how do I define which language to use? It doesn't seem like the translation function takes a language parameter, so how do I get the text in a different language?
Thanks in advance!
You can use the readLLfile Method to get specific translation by languagekey. This will return a array of all translated strings in $file.
$fd = GeneralUtility::readLLfile($file, $langKey);
You can't use the Typo3 translation in this way - Typo3 will always translate to the current language scope.
As per this answer I think the only way you could do it would to hold your translations outside of Typo3 (in an array or similar), and then do your own translation, rather than using the Typo3 built in one.
Related
I'm developing a localized Typo3 extension and I've build the repository and models, currently being able to view each language version of the content into its own language id page on the frontend.
I need however to display some of the translated properties(content fields) into a single webpage, but I do not know nor am I able to find on other sources how to handle all the translated content into my templates view or how to assign the variables.
Example:
In my Controller there is:
$test = $this->testRepository->findAll();
$this->view->assign('test', $test);
For which I display the content in my template as:
{test.title}
Now, Typo3 does the work and for EN it displays the english version of 'title', for DE the german version and so on, but all of this on separate pages.
How can I take all of the language versions of 'test.title' and display them into the same page?
Thank you
Just an idea, I didn't test it yet.
In your Repository, insert this function:
public function initializeObject() {
$querySettings = $this->createQuery()->getQuerySettings();
$querySettings->setRespectSysLanguage(false);
$querySettings->setLanguageMode('ignore');
$this->setDefaultQuerySettings($querySettings);
}
If you don't want to ignore the language filtering in your whole extension, just change the query settings only for a single query.
Now you should get all translations at once for your query. Do your own filtering of languages with
$query->matching($query->equals('sysLanguageUid', 1));
Don't forget to define sysLanguageUid with getter and setter functions in your model.
Is there a way to have multiple language files for the same language? I know that it would work if you have different packages, however i have a own plugin system. Is there a way to load additional language files on runtime?
If you do this (inside app/start/global.php or where you prefer)
Lang::addNamespace('namespace', '/your/custom/location');
then you should be able to
Lang::get('namespace::filename.localekey');
Create a file for your plugin in the proper language directory
/app
/lang
/en
myplugin.php
Then you can do
echo Lang::get('myplugin.foobar');
where foobar is the key in the line that contains the locale
Q: Is there a way to have multiple language files for the same language?
A: Yes, many ways.
Q: I know that it would work if you have different packages, however i have a own plugin system.
A: Unhandled Exception: What do you mean by this? You have a plugin system, ok, why would that prevent you from having multiple language files? What mechanism related to having "different packages" would somehow enable multiple language files for the same language?
Q: Is there a way to load additional language files on runtime?
A: Javascript?
Actual Answer: Have you seen this package? https://github.com/mcamara/laravel-localization - it might help you out quite a bit. Without more information that's about as specific as I can get.
I found this way:
In AppServiceProvider.php:
public function boot()
{
// ...
app('translator')->addNamespace('shared', base_path() . '/src/Domain/Shared/resources/lang/');
// ...
}
Then in any blade file:
{{ __('shared::langfile.key-in-the-file') }}
Currently i have a language loaded inside MY_Controller which extends CI_Controller. But inside a special page which controller (let's call it ABC controller) extends MY_Controller, I need to override the loaded language with another language. I tried loading another language inside this ABC controller, but unsuccessful. Is there a way to unload the loaded language and load another language?
an easier way is to reset the language data and is_loaded
$this->lang->is_loaded = array();
$this->lang->language = array();
I know it's a bit late to answer this but I think you can change the config item 'language' dynamically based on page requirement.
$this->config->set_item('language', 'chinese');
$this->config->set_item('language', 'english'); // based on the language folder of course holding language files
I had a requirement to send newsletters in users base lang, and this helped me change the language on the fly, hope this might help..
Have you tried just loading the language file you need?
$this->lang->load('filename', 'language');
It should be then accessible just like your default language. I haven't tested this tho, but from my understanding this should be the way to go about it.
Reference: http://codeigniter.com/user_guide/libraries/language.html
REVISED
I ended up digging a bit more for you, and found that you CANNOT load a default language (define it as default in your controller) and then later try to change it to something else.
Follow these steps:
If you need a language OTHER than english (default), set that in your config.
If you want to load ANOTHER language on a controller basis, you need to define that (most commonly in your constructor using something like session array / user selection.
You cannot load 2 languages (1 in the constructor, then another in a different class.. won't work!)
Reference here per forum posts: http://codeigniter.com/forums/viewthread/176223/
I encounter this problem and find a tricky solution.
$this->lang->load('text', 'english');
echo $this->lang->line('__YOUR_LANG_VARIABLE__');
//CI will record your lang file is loaded, unset it and then you will able to load another
//unset the lang file to allow the loading of another file
if(isset($this->lang->is_loaded)){
for($i=0; $i<=sizeof($this->lang->is_loaded); $i++){
unset($this->lang->is_loaded[$i]);
}
}
$this->lang->load('text', 'chinese');
echo $this->lang->line('__YOUR_LANG_VARIABLE__');
Hope it helps.
If you have any application installed built in codeigniter and you want add a language pack, just follow these steps:
Add language files in folder application/language/arabic
(I added arabic lang in sma2 built in ci)
Go to the file named setting.php
In application/modules/settings/views/setting.php you will find the array:
<div class="controls">
<?php /*
$lang = array (
'english' => 'English',
'arabic' => 'Arabic', // +++ Add this line
'spanish' => 'EspaƱol'
Now save and run the application.
I'm creating my own script using the CodeIgniter MVC framework. Now, i want users to easily modify the site functionality and adding their own without modifying the code which i've already written.
How do i make my site pluginable ?
EDIT: The users would be the site admins. Not the end user. Basically just like drupal or joomla. Want the admin to be able to create/add plugins to extend site functionality.
There may be a better way that's specific to CodeIgniter, but this is what I would do:
First, create functions for various "hook points" in your code. Say, a function named PreArticle that you call in your code, before an article is displayed.
Allow the user to write code like this:
addHook_PreArticle('funcToCall');
function funcToCall( &$articleText ) {
$articleText = str_replace('Hello', 'World', $articleText);
}
addHook_PreArticle is a function you've defined, which would add the passed string to some internal list. Then when the PreArticle function is called, each of those functions are executed, passing in any appropriate parameters that you define.
Many CMS's Like Joomla and Blogs like Wordpress use variable function names:
$function="phpinfo";
$function();
You could load this into an array to create a list of functions that can be overridden.
That's a perfect case to use the Observer Pattern.
http://devzone.zend.com/article/5
I'm using CodeIgniter, and will likely use their template library as I want to keep things extremely simple to use. The content for the template variables will come from the database, but I want the business admins to know what content areas are available. Basically the names of the parameters when they choose a specific template. For instance, Joomla uses an extra XML file that defines each area, whereas Wordpress uses comments within a page template to inform the system that the PHP file is a template. I like the Joomla approach because you don't have to parse the PHP file to find the areas, but I like the Wordpress approach because you don't have an extra XML file associated with every template. Are there other approaches that I'm missing?
I think the nicest way would be to add a small hack to the template parser class. The code looks quite readable and clean in system/libraries/Parser.php. You could insert a hook in that class that can be used to keep track of the variables. I don't know, if it works, but here's a snippet:
class CI_Parser {
var $varCallback;
function setVarCallback($callbackFunction) {
$this->varCallback = $callbackFunction;
}
...
function _parse_single(...) {
$callback = $this->varCallback;
$callback($key);
}
...
//Somewhere in your code
function storeVarName($variableName) {
// Persist the variable name wherever you want here
}
$this->parser->setVarCallback('storeVarName');
You could do this directly in the controller:
// in the controller
print_r($data);
$this->load->view("main", $data);
Or a little more rudimentary, but you could pass to the template a PHP array of variables (or an object):
// in the controller
$data = array();
$data["namespace"] = array(
"title" => "My website",
"posts" => array("hi", "something else")
);
$this->load->view("main", $data);
And then in the view, have a flag to print_r the namespace to show all the vars available, so that business admins know exactly what to use.
// in the view
if(isset($namespace["showAllVars"])) print_r($namespace);
One option would be to call token_get_all on the PHP file (only when your business admins are loading it up), and parse the output of that.
The best approach, in my opinion, is to keep the variable definitions in another place (such as a database table, or a separate file). This will help with testing (i.e., a programmer can't just remove a tag and it's gone) and making sure things are still working as you move on with the application development in time.
Another advantage is that your application logic will be independent from the templating engine.
On a side note, if you expect a lot of traffic, you may consider using smarty instead. We have done extensive testing with most of the templating engines around and smarty is the fastest.