I have set two languages in codeigniter, it works fine in my libraries, models and other files.
I have two folders in application/language/ english and french, and I put in witch one form_validation_lang.php take from system/language
I use form_validation library from system whitch load the language file like this
$this->CI->lang->load('form_validation')
It not have the second parameter and it load just the english language file from application/language/english/ witch is default language.
How can I pass current language to Form_validation.php from system/libraries or how can I make this work?
Load it with second param which is actual language:
$this->lang->load('form_validation', $language);
// or like this if you get instance of CI
$this->CI->lang->load('form_validation', $language);
where $language is a variable holding currently used language.
I fixed it. In my controller I put this line
$this->lang->load('form_validation', $language);
before
$this->form_validation->set_rules (......)
and now it works.
You have right deczo, but you dont tell me where to put this line.
Related
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.
This is rather a call for testimonies than a real questions. I would greatly appreciate feedbacks from CI experts and fans.
I have been searching the Internet for hours about this issue of mine: getting CI Form Validation class load the proper language file dynamically.
Let me explain. In my config.php file, I have:
$config['language'] = 'english';
Which is indeed the default language. But I have implemented a Settings controller which is letting my users set some values and of course change their default language. I could have stored that setting into a session variable but for the moment I don't, I just load that language user setting into each controller within the constructor :
$this->idiom = get_user_setting('language');
$this->lang->load('main', $this->idiom);
$this->lang->load('settings', $this->idiom);
$this->lang->load('cst', $this->idiom);
and as you can see I then load all the language files I need for each controller with the appropriate language. The 'get_user_setting' function is just a helper of mine querying the database to get a particular setting id.
I have copied the form_validation_lang.php from the /system/language/english/ directory and put it into my /application/language/french/ directory, and then I thought doing the following would do the magic:
$this->lang->load('form_validation', $this->idiom);
But nope... does not change anything. I took a look at the Form Validation class in the core folder and saw the following:
// Load the language file containing error messages
$this->CI->lang->load('form_validation');
THis clearly makes me think it will always load the file of the default language set in the config.php file. Am I right or wrong?
Hence, the only way I got to have this work with my user defined settings I fetch from the database (and which I could also store in a session variable), is to do the following:
$this->idiom = get_user_setting('language');
$this->config->set_item('language', $this->idiom);
...
I would greatly appreciate some feedbacks if some of you already had to cope with this kind of requirements and if you indeed managed that the same way I did or not. If I'm totally wrong, I'd appreciate solutions of course.
THanks a lot for your help.
Yes form_validation load default language file.If you want to load different language you can do a trick or change the source code of the Form validation class.
The trick I use, change the default language before using form validation(you can change back after form validation complete if you need.)
You can change default language with this code.
$this->config->set_item('language', 'YOUR_LANGUAGE');
After this code you can use form validation which will load the language you set.
Here is the way I solved it:
Create your own application\libraries\My_Form_validation.php.
Copy over the run() method from system\libraries\Form_validation.php into your file.
Change the following line in the run() method in your file from:
$this->CI->lang->load('form_validation');
To:
$language = !empty($this->CI->session->language) ? $this->CI->session->language : 'english';
$this->CI->lang->load('form_validation', $language);
This is assuming you are storing the language in the sessions. In your case you would do $language = get_user_setting('language'); I assume.
I'm working on my own module. I realize I constantly need to manually type my module name in different places. Most popular usage is with drupal_get_path($type, $name) function (I have more then 10 of these in my code). Where $name is theme or module name. During that time I need to already change my module name 3 times. As you can surmise I also need change all module names hard-coded in my project. So I thought it would be nice to have some convenient function to grab this name automatically.
How can I get machine module name programmatically?
For example if you have your module in following directory..
sites/all/modules/my_module/
..then you can grab it in this way
drupal_get_current_module_name(); // return my_module
Generally, you should know by convention - if you have: sites/all/modules/my_module/ then the machine name of the module should match the folder name - my_module.
Virtually all contributed modules follow this convention, and you should too.
It is possible to have your .info and .module file not match the name of the folder, but this isn't correct.
If you are already executing code inside your module, you should already know the machine name of the module by virtue of the name of the file you're editing - unless you're trying to do something that I'm not understanding.
Edit: Since we've determined you're just trying to call your module's theme function, you don't actually need to know the name.
If you have:
/** Implements theme_table **/
function my_really_long_module_name_table() {}
Your function might get called like this:
theme('table');
There is a little more to it than that, but the theme engine will make a determination about which theme functions get called based on what is implementing them.
It sounds like you may want to read up on some of the basics of the Drupal theme system.
Here's a good start for learning the Drupal 6 theme layer: http://drupal.org/node/165706
I figure out something like this:
function get_current_module_name() {
return array_shift(explode('.', end(explode(DIRECTORY_SEPARATOR, __FILE__))));
}
but don't know is't the best way to do it..
UPDATE:
I see now it's better to use basename
$name = basename(__FILE__, '.module');
UPDATE 2:
I think if this is needed across whole module then it could be accessible via constant defined in the very beginning of the module e.g.:
define('MODULE_NAME', basename(__FILE__, '.module'));
Then you could use all the time in all your function like this:
drupal_get_path('module', MODULE_NAME);
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've got a Zend-Framework application. I'm using the module-structure which Zend_Controller_Frontprovides. Here is a small excerpt from my directory-structure (only the important parts for this question):
root-directory
- modules
- blog
- views
- scripts
- index_index.phtml
- views
- pagination_control.phtml
As you can see I've got view-scripts that are specific to a module/controller/action. These views are located in the corresponding path (in this case like modules/blog/views. I've also got a more general view-directory located in the root-direcetory of my application.
What I am doing now is to call the PaginationControl-ViewHelper in modules/blog/views/scripts/index_index.phtml. This View-Helper however renders a partial-view, as you know. The ViewHelper tries to locate the partial-view within the same directory (meaning modules/blog/views/scripts. Since I want to use the same view-partial-script (pagination_control.phtml) in different modules I want to make this view-partial accessable from each module. So I want to put that file in the general views-folder in the root-directory.
However this doesn't work. The ViewHelper always looks for the view-script in the corresponding module-folder.
Anyone can help to make it accessable from my general views-directory?
As you can see here, since ZF 1.6.2 pagination control can take an array instead of a string for the partial argument, and in this array you set 1st the name of the partial and in 2nd the module name. This is still undocumented.
Using an array you can specify a module ('common'?) for the partial to use.
The real call will be (with $partial your 3rd argument to the paginationControl() view helper ):
$this->view->partial($partial[0], $partial[1], $pages);
This is usefull if you have a 'common' module.
Now here you are using a shared folder. You shoudl have installed it as a shared folder for your Zend_View this way (in a Boostrap or ressource code):
$view->addScriptPath("/root-directory/views");
or better:
$view->addScriptPath("/root-directory/views/partials");
And then you should'nt be required to specify any module directory. Zend_View should always check for a partial in this folder.