Problems with CodeIgniter Languages - codeigniter-2

I'm trying to make my site multi-lingual. I've created 2 folders and 2 files so far:
/application/language/en-us/
/application/language/en-us/application_lang.php
/application/language/fr-ca/
/application/language/fr-ca/application_lang.php
In the language files, I have the following declarations:
$lang['lang_company_name'] = 'Hello'; // en-us
$lang['lang_company_name'] = 'World'; // fr-ca
Then my test code:
$this->lang->load('application', 'en-us');
echo $this->lang->line('lang_company_name');
$this->lang->load('application', 'fr-ca');
echo $this->lang->line('lang_company_name');
The output is: HelloHello. Why am I not getting HelloWorld?
UPDATE:
autoload.php has nothing for $autoload['language'].
config.php still has $config['language'] = 'english'

The first language you load becomes the default language. If you did that in the opposite order you'd get "worldworld".

Once you load the language, it keeps track that the "application" language file has been loaded. So once it has been loaded, the 2nd call doesn't actually do anything since it sees it as loaded (it ignores the 2nd parameter when keeping track of loaded files).
Looking at the code here: https://github.com/EllisLab/CodeIgniter/blob/master/system/core/Lang.php, it looks like you might be able to hack it so it loads it again (unset from the $is_loaded attribute), but I guess the better question might be why do you need to do this? Somehow it seems a bit odd loading 2 different language files!

Related

Language system design

I am about to implement a language system into my codeigniter project. (following this tutorial: http://www.sitepoint.com/multi-language-support-in-codeigniter/), but I am a bit stuck with the thinking process.
The website will contain alot of text so there would have to be alot of indivudual language files like error_english.php, user_english.php etc...
But I'm wondering, is that the right way to go? Like for example what if I have a page with different language files loaded in it because it has alot of text and I have to load 1 word, something like Firsname or something.
That would mean I'd have to load user_english.php while that will contain for example more than 100's rows of texts. Wouldn't there be alot of loading because of this, just for 1 word? There would be so many unneeded arrays.
Does anyone know a good design/routing pattern to be able to keep the server loadtime/performance as it's best?
language packs are simply key-value pairs. yes, you load thousands of lines if need be and it works fine. One way to mitigate that line count somewhat is to have different language pack files for different sections of site.
so you could have an error file and do:
$this->load->language('errors');
But really it doesn't make a practical difference for speed - it is an organisational thing.
I have extended mine to use field substitution. Language packs also provide a way to separate code from presentation - i always use them in models even if I am only writing in english because it forms a separation between code logic and outputs that makes the code easier to read.
so if you put this in your helpers:
function lang_sub( $str, $params = '')
{
$CI = & get_instance();
$return_string = $str;
if (array_key_exists($str, $CI->lang->language))
{
// the str is a key. so it only is a one line substitution.
$return_string = $CI->lang->line($str);
if (is_array($params))// if a parameter array is sent, substitute the strings %s in order.
{
$return_string = vsprintf($r, $params);
}
}
return $return_string;
}
you can do something like this in your language entry:
$lang['login_message'] = "Welcome %s good to see you again! we as last saw your on %s.";
and in your model can do something like:
$welcome_message = lang_sub('login_message',array($username,$last_seen));
note that it is a good idea to consider naming and collisions of your entries before you start.
good luck!

loading macro files in a directory, transparently

I am looking for a way to separate the repetitive html codes from web pages, and for this I am planning to use the macro functionality. The problem here is for every macro I need to put this macro in a file, or put some of them in a file and include this in the template file.
What I need is to include once just the directory name something like
<#import "/tags/widgetDirectory" as widgets />
here the /tags/widgetDirectory is a directory , and every files here can be seen as a macro defined.
when I need to insert a code part from a file from this directory lets say slide.ftl I will just use
<#widgets.slider />
the system will check for slider.ftl in the /tags/widgetDirectory directory . here the slider.ftl can have <#macro> as first and as last line , or these can transparently added and system can load it as a macro
this will easy my designer work.
Maybe there is better way for doing this kind of widgets/components based web design ?
best regards,
This feature (importing directories) is something that's planned for FM actually... but it won't happen anytime soon. But I guess it can be solved fairly well with a hack right now. Instead of #import, use your own TemplateMethodModelEx, that you could use like <#assign widgets = importDirectory('/tags/widgetDirectory') >. This will return a TemplateHashModel that's also implemented by you and is bound to the directory path. When an item of that hash is get, it uses Environment.getCurrentEnvironment().include. The included file is expected to create a macro with name __main or something. So then you get that variable with Environment.getCurrentNamespace().get("__main") and return it as the result of the hash lookup. Of course this hash should also maintain a cache, so that if the same item is get twice, it wont include the template for the second time, just return the macro extracted earlier. This can be developer further, so that if the include file didn't define __main, then it's supposed that it prints directly to the output, and so it will be included again, when the "tag" is called again.

Custom syntax highlighting in Geany

I am trying to create custom syntax highlighting for Kivy '.kv' files in the Geany editor. Although the specific filetype seems irrelavant to the issue I'm having, as any efforts I make at getting syntax highlighting to work for a custom filetype results in a completely non-highlighted file. I believe I have done my homework on this, and nothing seems to work.
I have added the following to ~/.config/geany/filetype_extensions.conf
Kivy=*.kv;
I also have a custom type definition file named 'filetypes.Kivy.conf' in ~/.config/geany/filedefs/. I have tried basing this file off several of the stock type definition files in /usr/share/geany/ and the file never gets any syntax highlighting applied in Geany. Right now, just for experimentation's sake, my 'filetypes.Kivy.conf' file looks like this:
# For complete documentation of this file, please see Geany's main documentation
[settings]
# default extension used when saving files
extension=kv
# single comments, like # in this file
comment_single=#
[keywords]
# all items must be in one line
primary=size canvas
secondary=pos size
[indentation]
width=4
# 0 is spaces, 1 is tabs, 2 is tab & spaces
type=0
This is very loosly based on the stock XML definition file, but like I said I've tried many other stock files. In many cases I only changed the 'extension=' value to kv and still no highlighting was applied, even though going to Document>Set Filetype in Geany and choosing virtually any random filetype (besides my custom entry) would yeild some sort of highlighting within my .kv file. This is even the case when using the unmodified contents of a stock definition which otherwise works fine on my .kv file when specifically selected in Geany!
Also, the Kivy filetype is listed and selected by default in Document>Set Filetype within Geany, so I must be doing something right here!
I realize this similar question has been asked, but the solutions seem irrelavent to my case, as I've tried every related topic on this and many other sites. My Geany version is 1.22 and I'm running Arch Linux. This is driving me nuts - any suggestions?
Thank you!
Set lexer_filetype= property in the [settings] section of your filetype file. Working highlighting requires that there is a lexer that could be used for highlighting the .kv-files.
For more info see http://www.geany.org/manual/#lexer-filetype
There are three important things to obey:
the configuration file should be placed in "~/.config/geany/filedefs"
the configuration file must have the extension ".conf" - otherwise it won't show up at all (the files in "/usr/share/geany/filesdefs", where I copied my base file from, do not have a ".conf" extension!)
you must set the "lexer_filetype" to an existing (presumably builtin) configuration; e.g. "lexer_filetype=Python"

Set allow_url_include on SINGLE file

I've created a php file called pagebase.php that I'm quite proud of. It contains a class that created the whole html file for me from input such as css links and js links.
In any case, this file is several hundred lines long, as it includes several helper functions such as cleanHTML() that removes all whitespace from the html code then, in layman's terms, makes the source look pritty.
I have decided to use this pagebase in all my projects, particularly in all my internal projects. I also plan to add and expand to the pagebase file quite a lot. So what I'm wondering is if it's possible to set the allow_url_include option to on, but just on this one single file.
If I got my theory right, that would allow me to include() that file from any server and get the pagebase class.
So what I'm wondering is if it's possible to set the allow_url_include option to on, but just on this one single file.
No, as far as I'm aware this is not possible.
What you are planning to do sounds like a bad idea anyway, though. An include that gets loaded over the web on every request is awful for performance.
You should keep local copies of your library, and use a update script (or version control system) to keep versions up to date.
That is a bad practice.
You should put this file along with the project that needs it and locally include() it.
10 years and still no good solutions? Use this first one with allow_url_fopen for any convenient solution for your PHP file to allow the use of the php.ini brackets.
Replace allow_url_fopen in my example with allow_url_include to address the question.
This will surely be stated as a minor problem in future PHP. Especially if things aren't set up right with at least options are enabled globally. And this only works if there are no global php.ini rules.
Number 1 and 0 in code is either on and off.
<?php
echo ini_get('allow_url_fopen');
if (!ini_get('allow_url_fopen')) {
ini_set('allow_url_fopen', '1');
}
echo ini_get('allow_url_fopen');
Your code of use of either fopen() or copy() in between the code. Even some curl_init() might work.
echo ini_get('allow_url_fopen');
if (!ini_get('allow_url_fopen')) {
ini_set('allow_url_fopen', '0');
}
echo ini_get('allow_url_fopen');
}
?>
This also works for several other php.ini rules. I believe that this proof would be a security issue in many PHP codes further into the future. Surely a good thing to monitor in plugins in the further future or now for anti-malware signature. At least use the right global settings from now.

Getting started with Mathematica packages

Can someone offer some advice on how to get started with mathematica packages?
I can save the following in a file named "Foo.m". When I run the input cell in that file, I can see the effects reflected in $ContextPath variable.
BeginPackage["Foo`"]
bar::usage = "barfunction";
Begin["`private`"]
bar[w_] := w;
End[];
EndPackage[];
However, in my notebook I have the following:
#include<foo>
SetDirectory[ToFileName[{$HomeDirectory, "My Documents", "mathematica"}]];
Needs["Foo`"]
$ContextPath
But the needs call is failing for some reason, and the $ContextPath remains unchanged.
Edit
I believe that I've got a partial solution working now, the cell in my file wasn't marked as an initialization cell - and whilst I can now <<Foo', Needs["Foo"]` still isn't working correctly.
Check to make sure the saved file, "Foo.m", is on your $Path, which tells Mathematica which directories to look in when trying to load packages, much like the PATH environment variable in Unix or Windows.
EDIT: $ContextPath won't be changed unless there's an actual BeginPackage statement (or you manipulate it directly using Set or Block or something).
EDIT the second: One thing to check is what
FileNames["Foo.m", $Path]
returns. What you're describing does sound a little strange, though.
Either form should work. When a file is loaded using Get (or <<) or Needs, the directory on the top of the DirectoryStack[] is searched first, and then the $Path is searched. (SetDirectory does not change $Path, so FileNames["Foo.m", $Path]won't find Foo.m.) However, FindFile by default searches Directory[] and $Path. You can test it by doing the following:
FindFile["Foo`"]
SetDirectory[<Foo dir>]
FindFile["Foo`"]
it should return
$Failed
<Foo dir>
<Foo dir>/foo.m
If FindFile can find Foo.m then Needs should be able to find it. In general, I put my packages in $UserBaseDirectory/Applications, and Needs picks them up just fine.

Resources