Laravel - Multiple language files at custom locations - php

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') }}

Related

Best way to set a common variable between projects in PHP?

I have several projects on my host. They have many similarities and I want them to use common node_modules. My folder structure looks something like this:
_CONFIG.php
node_modules/
project1/
index.php
project2/
index.php
project3/
index.php
In all my index.php files, I require_once '../_CONFIG.php' where I've set:
$node_modules = '../node_modules/';
Then in each project, when I need to use a Node module, I have:
<script src="<?= $node_modules; ?>jquery/dist/jquery.min.js"></script>
I do this because I might need to change the path of node_modules. If everything is hardcoded, I'd have a rough time. Let's say I install upgraded node_modules and I want to have a backup. I want to be able to quickly switch between the old and new modules, in case some of the new ones are incompatible with one of my projects.
I have other common things between the projects that I want to be easily configurable via the _CONFIG.php file.
Question
I know that polluting the global scope is not a great thing and that's exactly what I'm doing. Is there a better way to do this? In the same time, I don't want to do a ton of stuff just to output a string in a script tag's src attribute.
What about defining constants? It's quite useful for defining global options, and you do not pollute the global scope at all.
define('NODE_MODULES_DIR', '../node_modules/');
and then later use it there:
<script src="<?= NODE_MODULES_DIR ?>jquery/dist/jquery.min.js"></script>
I'm not aware of a "better" solution.
Keeping configuration outside of your code is the way to go. You put all your connection strings, pwds etc in a config file as suggested by The Twelve-Factor App.
Instead of using global variables, you could add one level of indirection by using a wrapper function that returns values based on a given key-string.
phpdotenv provides these capabilities nicely.

Typo3 with extbase - get translation for specific language

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.

Codeigniter HMVC asset managment

I am trying to give a shot to HMVC in Codeigniter. Here is my folder structure.
-ROOT
--APPLICATION
---MODULES
----Module_Email
-----Controllers
-----Models
-----Views
-----Assets
------JS
------CSS
------IMG
To render the Module i have to use
Module::run('Module_Email');
This method will output the rendered output, an example is given below
<script type="text/javascript" src="PATH/TO/EMAIL_MODULE/JS/JS_FILE.JS"></script>
<div data-module-name="Module_Email" class="Email_wrapper">
//RENDERED HTML CONTENT
</div>
Now here my problem start. Normally i would like to put all my resources to header. So when i call any module, its dependence need to be added in header instead of from where its get called.
I searched a lot but i couldn't find any good methods.
Please help.
Update
Currently i have a function on my header called get_assets() which will output predefined resources to header. But i cant say which modules is going to use in pages, so the system need to check which modules are used in this page, and if its used then its dependencies need to be added on header.
Seems like your main problem then is trying to figure out what modules were used.
Unfortunately as far as I can tell with the default Wiredesignz modular extension there is no way to access the module name unless you write some sort of hack to get at that data. The module being used is stored in the protected variable $module in the MX_Router class, however, there is no public method to allow you to get access to it. So your only choice would be to extend the class and create a public function.
Alternatively you could use a forked version of Wiredesignz implementation which I did which provides numerous other features including a public function to get at the $module variable. Using the forked version I wrote you could then use code such as this:
<?php $module_name = $this->router->fetch_module(); ?>
However, that will only record the last module you loaded, so you would still need to do work to store all the modules, and then have your function use this information to determine what assets to load. If I were doing something like you I would probably fork my version and then create an additional data structure to store every module that was loaded that you could then later get access to.
I don't think this is exactly what you were hoping for, but might be something to get you on the right track to finding a solution.
I added an array to the Module class to store the assets and two functions to store/retrieve the items. Here is the source (updated Modules.php)
# Register your assets
public static function register_asset( $asset )
{
if( in_array($asset,self::$assets) === FALSE )
{
self::$assets[] = $asset;
}
}
public static function assets()
{
return self::$assets;
}
and now you can register your assets like this inside your module
Modules::register_asset('myslider.js');
You can retrieve all your assets using
Modules:assets();
Which will return an array of assets that can be processed depending up on the situation.

How can I get Drupal module name programmatically?

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);

Modifying Zend_ViewHelper_Pagination_Controller Partial-Path

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.

Categories