xfra35 / f3-multilang

Create multilingual apps with this localization plugin for the PHP Fat-Free Framework
GNU General Public License v3.0
48 stars 13 forks source link

Where i have to define @dict #14

Closed eliogsolis closed 8 years ago

eliogsolis commented 8 years ago

i saw an example here https://groups.google.com/forum/#!topic/f3-framework/NAhOB6rL6sw

im trying this

{{ @dict.hello }}

but it doesnt work, can you help me please?

xfra35 commented 8 years ago

Hi,

In order to define a dictionary, you should follow the steps below:

  1. create a folder (for example dict/) with a dictionary file for each supported language. For example, en.ini, nl.ini and zh.ini for English, Dutch and Chinese. Each dictionary file contains a list of key-value pairs, such as hello = 你好.
  2. instruct the framework about the dictionary folder location: $f3->LOCALES='dict/';
  3. instruct the framework about the user language: $f3->LANGUAGE='zh';
  4. now echo $f3->hello outputs 你好

NB1: when using the Multilang plugin, step 3 is automatically handled by the plugin (language is detected from the URI).

NB2: you can automatically prefix all dictionary entries. For example:

See the framework documentation for more details.

eliogsolis commented 8 years ago

Not working for me My index.php

require('vendor/autoload.php');
    $f3=Base::instance();
    $f3->config('app/config.ini');
    $f3->config('app/routes.ini');

    Multilang::instance();
    $f3->set('LOCALES','/dict');
    $f3->set('PREFIX','dict_');
    $f3->run();

My dict/en hello = Hello

and dict/es hello = Hola Controllor just doing an echo $f3->dict_hello;

xfra35 commented 8 years ago

It should be $f3->set('LOCALES','dict/').

eliogsolis commented 8 years ago

Well... actually the plugin is working with URL's /multilang/es but still not working in my controller (i made the change that you suggested)

$f3->get('dict_hello');
$f3->get('dict.hello');
$f3->get('DICT.hello');
$f3->dict_hello;

Thanks for the help!! And sorry for my bad english...

xfra35 commented 8 years ago

What do you mean by "not working"? Does it output an empty string? or a bad translation?

eliogsolis commented 8 years ago

Gettin null from
var_dump($f3->get('DICT.hello')); or empty if i use an echo echo $f3->dict_hello;

xfra35 commented 8 years ago

Try to output $f3->LANGUAGE just after the plugin instantiation to see if Spanish is detected.

eliogsolis commented 8 years ago

Gettin es-ES,es,en-GB,en,en-US

xfra35 commented 8 years ago

The detection is correct. So there's an issue with your dictionary files.

You should have:

Your folder structure should look like:

app/
dict/
  es.ini
vendor/
index.php

es.ini should look like:

hello = Hola
thanks = Gracias

After which, print_r($f3->dict) should output an array containing Hola and Gracias.

eliogsolis commented 8 years ago

Mmm... (T-T) ... I dont know why its not working for me...

image

es.ini image

my index just in case image

my controller image

And my config.ini and routes.ini image

image

ikkez commented 8 years ago

You don't need to have dict/ in your autoload path. And in case, it should be AUTOLOAD = "app/;dict/" otherwise it becomes an array without the " " around it, which could cause misbehaviour for the autoloader, as this is a special edge case for custom autoload functions

xfra35 commented 8 years ago

@eliogsolis you're defining PREFIX after loading the dictionary (via LOCALES), so the term is stored in hello instead of dict.hello.

Just reverse the two lines to fix your problem:

$f3->PREFIX='dict.';
$f3->LOCALES='dict/';

I've just added a mention about it in the docs.

xfra35 commented 8 years ago

@ikkez the issue you're describing occurs with commas, not semicolons. This is because $f3->config uses str_getcsv to split values, not $f3->split.

Anyway thanks for mentioning it. It's good to know that semicolons are safer to use in config files. I've just added a warning for it in the docs.