next-i18next switching to default locale after refresh [duplicate] - next.js

I'm trying to make my default language in Next.js i18n but always is getting "En" as default language called like fallback.
And I also get this error:
Error: [#formatjs/intl Error MISSING_DATA] Missing locale data for locale: "sq" in Intl.NumberFormat. Using default locale: "en" as fallback
module.exports = {
i18n: {
locales: ['sq', 'en'],
defaultLocale: "sq",
}
}

Next.js will automatically detect which locale the user prefers based on the Accept-Language header sent in the page request.
In your case, although your default locale is sq, the en locale is detected in the Accept-Language header so you get redirected to the locale-prefixed path.
This behaviour can be disabled by setting localeDetection to false in your i18n options.
// next.config.js
module.exports = {
i18n: {
locales: ['sq', 'en'],
defaultLocale: 'sq',
localeDetection: false
}
}
From the Disabling Automatic Locale Detection documentation:
When localeDetection is set to false Next.js will no longer
automatically redirect based on the user's preferred locale and will
only provide locale information detected from either the locale based
domain or locale path as described above.
As a side note, regarding the #formatjs/intl error, it indicates that you're using an environment/browser that doesn't have support for the sq locale. You may want to look into #formatjs/intl-numberformat to polyfill that locale data.

Related

i18n use json locales file and fetching some from backend

I have project on next.js + spring boot. I want to use i18n and separate getting localization files methods. One part of localization (such as UI text) I would like get from json files in next.js project, and and the other part of locales (some content I want change from admin panel) - from my backend server by 18next-http-backend.
I can only implement one of the methods, but not both at the same time.
const I18NextHttpBackend = require('i18next-http-backend')
module.exports = {
i18n: {
defaultLocale: 'ua',
locales: ['ua', 'ru', 'en'],
backend: {
loadPath: `${process.env.NEXT_PUBLIC_BACKEND_URL}/localization/{{lng}}/{{ns}}`,
},
},
debug: true,
ns: ['common', 'footer'],
serializeConfig: false,
use: [I18NextHttpBackend], // When this setting is enabled, next.js does not see locales.json files in ./public/locales
}
Is there a way to get those locales from both methods?
I tried to find a solution on the Internet, but I did not find similar examples where localization files are obtained in two ways at the same time

Symfony saving locale in session

I have two translation languages: en and ru
I have locale: en|ru in parameters.yml and translator: { fallbacks: "%locale%"} in config.yml.
I need to do switching of languages. I need to save locales into session and set the right locale in parameters.yml. How i change locale parameter value to the right one from session?
The parameters.yml is not designed for dynamic content, but I think what you want to you need is this:
To get the currently set locale:
$locale = $request->getLocale();
To set the preferred locale order:
$session = $this->get('session');
$session->setLocale($request->getPreferredLanguage(array('de', 'en')));

Symfony2 jms/i18n-routing-bundle and multiple hosts to one locale

I am using mentioned bundle in my application, and I would like to be able to configure it this way:
jms_i18n_routing:
default_locale: en
locales: [en, de]
strategy: custom
hosts:
en: [mydomain.com, subdomain.domain.com]
de: mydomain.de
redirect_to_host: false
so multiple domains to one locale. I would like to run two similiar websites at one application to have access to the 90% of the code which is similiar and same database. Any tips how could i achieve this? Or maybe there is other bundle/solution more accurate for my problem?
From the configuration you cannot bind multiple domains to one locale.
You can try to extend this class of the bundle:
JMS\I18nRoutingBundle\Router\DefaultLocaleResolver
You need to change this part:
public function resolveLocale(Request $request, array $availableLocales)
{
if ($this->hostMap && isset($this->hostMap[$host = $request->getHost()])) {
return $this->hostMap[$host];
}
...
}
adding a more complex hostMap that supports multiple domains for the same locale.

a2lix_translation_form default locale overriden by stof_doctrine_extensions translatable?

I'm using a2lix_translation_form tabs in my form. It has the feature, that it allows you to edit several translations to one property in one form. I have it configured like this:
a2lix_translation_form:
locales: [sk, en, de] # [1]
default_required: false ... # further as default
In the form I have following 3 Tabs where I can edit one property (Description)
|SK [Default] | En | DE |
It worked fine (stored things in database and so on), until I turned on the translatable in stof_doctrine_extensions. Here is the config:
stof_doctrine_extensions:
default_locale: sk
orm:
default:
translatable: true # not needed: listeners are not enabled by default
I also use jms_i18n_routing:
jms_i18n_routing:
default_locale: sk
locales: [sk, de, en]
strategy: prefix_except_default
When I acess
localhost/app_dev.php/product/1/edit
then everything looks fine, but when I access
localhost/en/app_dev.php/en/company/11/edit
the Sk [Default] contains En description.
When I set the translatable in stof_doctrine_extensions to false the form is displayed correctly. But I need to have it ON, because I need it for other components. What can I do?
You are in a specific case, that I don't advice henceforth. You will have some difficulties with you database if you change your default locale in the future.
I've updated the doc (I have still some work..), see the end of http://a2lix.fr/bundles/translation-form/#bundle-advanced.
You can use annotation as explaned in the doc or add at the beginning of yours edit/create methods:
$translatableListener = $this->get('stof_doctrine_extensions.listener.translatable');
$translatableListener->setTranslatableLocale($translatableListener->getDefaultLocale());

Symfony2.2 : default_locale always applying in twig translations

I'm having a strange issue with Symfony2.2. I have a project using two languages : en/fr. So I create as usual (like Symfony2.0) two translation files "messages.en.yml" and "messages.fr.yml" in Ressources/Views/translations/. But Translations in twig could not change even if we set the request object and the locale session. Translation is always set by the default_locale (config.php).
Example : if default_locale = en, all my website (in twig) is translated in en, even if i set the _locale object in fr (request and session). Of course if I manually change the default_locale to fr the website is naturally in fr...
However, _locale session works but I don't know if locale request works, and of course translation works in controllers too...
There is my files :
config.yml:
framework:
#esi: ~
translator: { fallback: %locale% } # = en
# ...
default_locale: %locale% # = en
Controller :
public function indexAction()
{
$this->get('session')->set('_locale', 'fr');
$this->getRequest()->setLocale($lang);
exit($this->getRequest()->getLocale()); // = fr
exit($this->get('translator')->trans('Symfony2 is great')); // = Symfony2 est génial
return $this->render('TestBundle:Controller:test.html.twig');
View :
{% block content %}
<p>lang : {{ app.request.locale }}</p> {#} = "fr", OK{#}
<p>{{ 'Symfony2 is great'|trans }}</p> {#} = "Symfony2 is great", WAIT WHAT?{#}
I must resign myself to force the locale at the beginning of the method controller to have the requested locale (stored in session) like that :
Controller:
if($this->get('session')->get('_locale')){
$lang = $this->get('session')->get('_locale');
$this->getRequest()->setLocale($lang);
}
In other words, I do have a problem with the registration of the request object... Because the last code works well in the controller, and shows well the locale in twig page with app.request.locale, but not the translations... (sorry for my bad english and thanks for helping)
I had the same issue due to the low priority of my event listener. The locale would be overridden by the Translator's TranslatorListener. Increasing the priority of my event listener did the trick for me:
services:
app.locale_listener:
class: AppBundle\EventListener\LocaleListener
tags:
- { name: kernel.event_listener, priority: 11, ... }
Source: https://github.com/symfony/symfony/issues/12878#issuecomment-68628808
Parameter _locale in routing holds your locale value.
Look here on this page
Symfony - Book - Translation - Local and the URL
From Symfony 2.1 they have this kind of logic:
Since you can store the locale of the user in the session, it may be tempting to use the same URL to display a resource in many different languages based on the user's locale. For example, http://www.example.com/contact could show content in English for one user and French for another user. Unfortunately, this violates a fundamental rule of the Web: that a particular URL returns the same resource regardless of the user. To further muddy the problem, which version of the content would be indexed by search engines?
A better policy is to include the locale in the URL. This is fully-supported by the routing system using the special _locale parameter:
Now when you want to sel local, this doesn't work any more
$this->get('session')->set('_locale', 'fr');
You can use request insted of session now but you can not have session-logic with _local from Symfony 2.0 unless you simulate it with event listener on kernel request.

Resources