I'm having a weird problem or maybe i failed to understand how Grails i18n mechanism works.
I inserted the following to my index.gsp file:
LocaleContextHolder.locale: '${org.springframework.context.i18n.LocaleContextHolder.locale}'
java.util.Locale.getDefault(): '${java.util.Locale.getDefault()}'
RequestContextUtils.getLocale(request): '${org.springframework.web.servlet.support.RequestContextUtils.getLocale(request)}'
session['SessionLocaleResolver.LOCALE']: '${session['org.springframework.web.servlet.i18n.SessionLocaleResolver.LOCALE']}'
This code snippet should print the Locale, and it does like so:
LocaleContextHolder.locale: 'iw'
java.util.Locale.getDefault(): 'en_US'
RequestContextUtils.getLocale(request): 'iw'
session['SessionLocaleResolver.LOCALE']: ''
The above is my default output when i run the given code snippet.
I tried to understand what exactly affects this variables so i did the next steps:
I changed Chrome's Locale
Settings --> Show advanced settings --> Language and input settings)
I changed my operating system's locale (Windows 7)
Control panel --> Region and language --> Format + Location + System locale
After those changes i rebooted my computer and expected the values of the locale variables to change, but they still remain the same.
I'm aware i can change the Locale with the ?lang parameter & with some code, but i'm interested in letting Grails to decide which Locale to pick.
What exactly affects those variables? how does Grails decide the machine's locale?
15.2 Changing Locales
By default the user locale is detected from the incoming Accept-Language header. However, you can provide users the capability to switch locales by simply passing a parameter called lang to Grails as a request parameter:
/book/list?lang=es
Grails will automatically switch the user's locale and store it in a cookie so subsequent requests will have the new header.
from here :
https://grails.github.io/grails-doc/2.4.3/guide/i18n.html
If you are deploying in tomcat you can set the locale in the catalina.sh script:
if [ -z "$LOGGING_MANAGER" ]; then
JAVA_OPTS="$JAVA_OPTS -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Duser.language=en -Duser.region=US"
else
JAVA_OPTS="$JAVA_OPTS $LOGGING_MANAGER -Duser.language=en -Duser.region=US"
fi
I think the internationalization works when you pass lang parameter in the URL. It's mentioned in the documentation
By default the user locale is detected from the incoming Accept-Language header.
However, you can provide users the capability to switch locales by simply passing
a parameter called lang to Grails as a request parameter:
/book/list?lang=es
http://grails.github.io/grails-doc/latest/guide/i18n.html
By default the user Locale is detected from the incoming Accept-Language HTTP header, this header is generated by the browser that is used by the user.
Each browser determines the system Locale differently:
Internet Explorer uses the O/S Locale settings.
Firefox / Chrome let the user to decide what Locale to use (In the language settings).
See: http://www.w3.org/International/questions/qa-lang-priorities.en.php
Related
I am developing a C# .NET 6 client-server product using VS2022 with multi-language support. I've set up locale-specific resource strings but at the last minute I realized a problem: while client workstations are set up in local language, the app-server is always in English - so back-end code is using the English localization!
Servers are in English for a good reason (and we can't change this) so is there a way to force a deployed application to use a specified locale? Perhaps in a config file somewhere?
I know I can do this in code by changing Thread.CurrentCulture or similar, but the whole point is I don't want to hard-code it, I want it to be config-driven in a way that overrides the default system setting.
I want it to be config-driven in a way that overrides the default system setting.
You can always add custom setting to your config file and then read it on start up and set needed culture.
For example something like this:
appsettings.json
{
"LocaleOverride" : "en-US",
// rest of settings
}
And somewhere at the start of app (depends on how it is started, if generic/minimal hosting is used, configuration can be read from there, otherwise - manually):
var locale = Configuration["LocaleOverride"];
if(!string.IsNullOrEmpty(locale))
{
var cultureInfo = new CultureInfo(locale);
CultureInfo.DefaultThreadCurrentCulture = cultureInfo;
CultureInfo.DefaultThreadCurrentUICulture = cultureInfo;
}
I'm trying to use i18next for localtization, it works well as it translates english text to arabic {t("title")} ==> مرحبا.
But when i try to change language to english with
const { t, i18n } = useTranslation()
i18n.changeLanguage('en')
it dosen't change.
How to fix this.
I got that error at server vscode terimanl react-i18next:: You will need to pass in an i18next instance by using initReactI18next
I found the solution at nextjs Docs.
It seems changing locales at nextjs not the same as reactjs, with invoking i18n.changeLanguage('en').
But instead use.
<Link href="/fr/another" locale="en">
<a>To /fr/another</a>
</Link>
Will do the job.
more information: https://nextjs.org/docs/advanced-features/i18n-routing
I have these language files:
locales/
en-US/
a.yaml
b.yaml
zh-CN/
a.yaml
b.yaml
DA/
a.yaml
b.yaml
set default language:
app.I18n.SetDefault("en-US")
How to set language dynamically according to l parameter:
www.sete.com/xx/xxx?l=en => set en-US
www.sete.com/xx/xxx?l=cn => set zh-CN
www.sete.com/xx/xxx?l=da => set DA
www.sete.com/xx/xxx?l=NotFound => default en-US
In addition, whenI set a language that doesn't exist:
www.sete.com/xx/xxx?l=NotFound
I get an error in response like this:
{
"user": "yaml%!(EXTRA string=Tom....)"
}
So , what should I do to better solve these problems? I tried my best, my English is not good, please help me.....
You can find Iris i18n examples at: https://github.com/kataras/iris/tree/master/_examples/i18n
If the language was not found, then the default language's key will be shown instead unless app.I18N.Strict is true. If the Strict field is false and the default language has no available key for the translation then the app.I18n.DefaultMessageFunc will be fired instead, you can take a look on how to configure what happens if a key does not exist at: https://github.com/kataras/iris/blob/7b6a8f1e26469ab3ae53cfe468d6e5202c75c2a8/_examples/i18n/basic/main.go#L38-L47
I want to add JVM option -Dfile.encoding=UTF-8 in my application.yml file
I tried using spring.mandatory-file-encoding=UTF-8 . But I'm getting the following error :
Type=ERROR,Category=org.springframework.boot.context.FileEncodingApplicationListener,Thread=main,MDC=,Text=System property 'file.encoding' is currently 'Cp1252'. It should be 'UTF-8' (as defined in 'spring.mandatoryFileEncoding')
Type=ERROR,Category=org.springframework.boot.context.FileEncodingApplicationListener,Thread=main,MDC=,Text=Environment variable LANG is 'en_US.UTF-8'. You could use a locale setting that matches encoding='UTF-8'
Type=ERROR,Category=org.springframework.boot.context.FileEncodingApplicationListener,Thread=main,MDC=,Text=Environment variable LC_ALL is 'null'. You could use a locale setting that matches encoding='UTF-8'
FileEncodingApplicationListener :
It halts application startup if the system file encoding does not match an expected value set in the environment. By default has no effect, but if you set {#code spring.mandatory_file_encoding} to the name of a character encoding (e.g. "UTF-8") then this initializer throws an exception when the System property does not equal it.
Click here to read more:
Deleting this property "spring.mandatory-file-encoding=UTF-8" from application.properties file helped me.
But first I've changed settings in IntelijIDEA: File -> Settings -> Editor -> File Encodings form "System default" to UTF-8. May be it affected somehow.
I have a running VoiceXML application that works ok in Nexmo. If I set any language other than en-US the calls want get answered. I just change en xml:lang as in:
<vxml application="/dialogue/root/50b9bab0-9ce8-4d7a-9389-09f06aa8f9ee" version="2.1" xml:lang="es-es">
I have tried in the vxml above and also in the prompt tag. Any language like es-es, es-ES... even en-UK will make my vxml stop working in Nexmo.
I am sure script is OK as I can change en-US female and male voice with en-us-male and en-us-female. That works.
Am I missing something?
(I don't think it makes a difference but I use the great Rivr java library to generated vxml)
for me "fr-ca" doesnt work but "fr-ca-female" does work