I'm having trouble with the I18n functionality in Kohana 3.3
My I18n folder structure is as follows
i18n/
en/
us.php
zh/
hk.php
cn.php
The problem I'm getting is that everywhere I read it should be possible to load the language like so i18n::lang('en-us'); because the api states that it explodes the string on the “-” character, so the default target language “en-us” results in a search for the following files:
/application/i18n/en.php
/application/i18n/en/us.php
It only seems to load the correct language files when I use i18n::lang('en/us'); instead of i18n::lang('en-us');
Works for me. These calls are equals for Kohana:
I18n::lang('en-us');
I18n::lang('en us');
I18n::lang('en_us');
When you use 'en/us' value, I18n will not load i18n/en.php file, only i18n/en/us.php.
I can suggest only one reason for your problems:
You are using __() function, which ignores translations for default language ('en-us' is hardcoded). So, when you call I18n::lang('en/us'), default language is still english, but it differs from 'en-us'. Little hack :)
You can extend I18n class with APPPATH/classes/I18n.php file (standard Kohana way), and add your own version for that function:
// translate always!
function __($string, array $values = NULL, $lang = 'en-us')
{
$string = I18n::get($string);
return empty($values) ? $string : strtr($string, $values);
}
Related
Am trying to create a language switcher for a Symfony 4 project (with Twig). Basically when clicking a link, the website's language will change.
Configuration I have is this:
config\packages\translation.yaml
framework:
default_locale: en
translator:
default_path: '%kernel.project_dir%/translations'
fallbacks:
- en
This is the important lines I got in
config\packages\services.yaml:
parameters:
locale: 'en'
# This parameter defines the codes of the locales (languages) enabled in the application
app_locales: en|fr
My Controller:
<?php
namespace App\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Translation\TranslatorInterface;
use Symfony\Component\Validator\Constraints\Json;
// Class name really should match Controller name
class ControllerBase extends AbstractController
{
/**
* #Route("/{_locale}", name="app_landing", defaults={"_locale" = "en"}, requirements={"_locale" = "en|fr"})
*/
public function index(Request $request)
{
$locale = $request->getLocale();
return $this->render('landing/index.html.twig');
}
}
The actual language switcher is just two list elements contained in the template file. They look like this:
<li>English</li>
<li>French</li>
This works fine.
Problem arises when clicking another link on the website. It doesn't contain the _locale and the site defaults back to en.
I really would like the _locale to be added to each link automatically (including the index path "/").
After a while of experimenting, I found the answer.
I tried to do the same thing in two different ways. In my case it was best to simply use the code in the template file and omit the {_locale} parameter in the controller.
Like that all works fine.
I am using the botbuilder framework. I have defined several namespaces for the dialogs I have created, such as help or default. For all of these I have also created json files in my locale/en/ directory, and all is well.
However, I have a few sentences that are very common, and I don't feel like copying those over to each of the individual namespaces. I have tried using index.json as a 'fallback' in case the namespace file doesn't define the string. But it doesn't work for me. Contrary to what the documentation seems to suggest.
/locale
/en
/help.json
/default.json
/index.json <-- Doesn't work
/dialogs
/help.js
/default.js
bot.js
Say I have the following library in help.js, that :
lib = new builder.Library('help')
lib.dialog('/', (session) => {
session.send('custom_cancel')
}
module.exports = lib
The library is used in bot.js:
bot.library(require('./dialogs/help'))
And index.json has this content:
{
"custom_cancel": "My custom cancel"
}
Whereas help.json is empty:
{}
Because help.json does not have custom_cancel, the bot will actually send custom_cancel as the string.
Again. I can copy paste the strings to both locations and there is no more problem. But that seems like an ugly solution to me.
I have tried the more explicit version, which seems to help in more cases, but I am not fully convinced yet.
session.localizer.gettext(session.preferredLocale(), 'custom_cancel')
You can use the third argument for the namespace. It seems that '' will point to the index.json file.
I have packaged some of my dialogs as libraries by following the instructions from the basics-libraries example. Before, I had all dialogs in app.js and I used localization following the instructions from the documentation. For example, I used
var chatBot = new builder.UniversalBot(chatConnector, {
localizerSettings: {
botLocalePath: "./locale",
defaultLocale: "de"
}
});
and
session.preferredLocale("de");
to use German for all the prompt labels, and stored all the German translations in ./locale/de/index.json and ./locale/de/BotBuilder.json.
However, the localization doesn't work with the dialogs I have packaged into libraries. Instead of the localized strings only the message IDs are displayed.
It works if I use session.localizer.gettext(session.preferredLocale(), "message ID") on each string. However, that is very tedious, and I am wondering if there is a way to localize all strings inside a library at once.
In order to this to work, your locale file should have the same name as your library.
./my_bot_library.js
./locale/en/my_bot_library.json
Then, to get your localized text you can either use:
session.localizer.gettext(session.preferredLocale(), 'message ID', 'my_bot_library')
Please note that the the third parameter is the namespace that
represents the library name
or just the following which internally resolves the preferred Locale and current namespace/library name
session.gettext('message_id') or session.send('message_id')
I would like to see default button captions in my native language ,
Even I changed iPad's language to another lang. my app still shows English ones...Edit,Done,Save etc.
I also set CFBundleDevelopmentRegion as a User-Defined variable in XCode but not helped !
Can you help ?
You should make these things:
Add your native language to app's supported Localization array of languages;
Make your Application.Main procedure looks like:
static void Main (string[] args)
{
// if you want to use a different Application Delegate class from "AppDelegate"
// you can specify it here.
NSUserDefaults.StandardUserDefaults.SetValueForKey(NSArray.FromStrings("tr"), new NSString("AppleLanguages"));
NSUserDefaults.StandardUserDefaults.Synchronize();
UIApplication.Main (args, null, "AppDelegate");
}
Then, this
turns to this
(I suggested that your language is turkish.)
For monotouch I think it is different,
They hide some langs for reducing the size of IPA.
http://docs.xamarin.com/guides/ios/advanced_topics/localization_and_internationalization
I'm porting a Symfony 1.2 project to Symfony 2.x. I'm currently running the latest 2.1.0-dev release.
From my old project I have a class called Tools which has some simple functions for things like munging arrays into strings and generating slugs from strings. I'd like to use this class in my new project but I'm unclear how to use this class outside of a bundle.
I've looked at various answers here which recommend changing app/autoload.php but my autoload.php looks different to the ones in the answers, maybe something has changed here between 2.0 and 2.1.
I'd like to keep my class in my src or app directories as they're under source control. My vendors directory isn't as I'm using composer to take care of that.
Any advice would be appreciated here.
Another way is to use the /app/config/autoload.php:
<?php
use Doctrine\Common\Annotations\AnnotationRegistry;
$loader = require __DIR__.'/../vendor/autoload.php';
$loader->add( 'YOURNAMESPACE', __DIR__.'/../vendor/YOURVENDOR/src' );
// intl
if (!function_exists('intl_get_error_code')) {
require_once _DIR__.'/../vendor/symfony/symfony/src/Symfony/Component/Locale/Resources/stubs/functions.php';
$loader->add('', __DIR__.'/../vendor/symfony/symfony/src/Symfony/Component/Locale/Resources/stubs');
}
AnnotationRegistry::registerLoader(array($loader, 'loadClass'));
return $loader;
Just replace YOURNAMESPACE and YOURVENDOR with your values. Works quite well for me, so far.
You're correct, I stumbled upon the changes in autoload from 2.0 to 2.1. The above code works fine with the latest version, to which I upgraded my project ;-)
For a simple case like this the quickest solution is creating a folder (for example Common) directly under src and put your class in it.
src
-- Common
-- Tools.php
Tools.php contains your class with proper namespace, for example
<?php
namespace Common;
class Tools
{
public static function slugify($string)
{
// ...
}
}
Before calling your function do not forget the use statement
use Common\Tools;
// ...
Tools::slugify('my test string');
If you put your code under src following the proper folder structure and namespace as above, it will work without touching app/autoload.php.