Dynamic setting language in Golang web framework Iris - localization

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

Related

Grails - computer/browser locale doesn't affects i18n mechanism

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

Nexmo VoiceXML not working in language other than en-US

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

Translations in sourceLanguage does not work in Yii2 application

I'm using a translations based on keywords in my Yii2 application (I know, that this isn't best option, but I don't have other). I've prepared #app/messages/pl/app.php and #app/messages/en/app.php files with translation strings using keywords, instead of full-featured sentences or words:
<?php
return [
'name_english'=>'Name in English',
'keywords_english'=>'Keywords in English'
];
?>
I have set my application to use Polish language as default:
'language' => 'pl',
'sourceLanguage' => 'en',
And I'm invoking translation:
Yii::t('app', 'keywords_english');
Everything works fine, when language is actually set to base, Polish (pl):
But, when I change it to English (en; either by setting Yii::$app->language during runtime or by changing application configuration), translation is not performed and I'm getting keywords_english:
I have put die() in the beginning of #app/messages/pl/app.php and #app/messages/en/app.php files and I can clearly see, that when language is set to English, second file is not being included by Yii2 (application run follows), while, when language is Polish, first file is included and application flow breaks on that die().
What am I missing? Why Yii2 is not using translations from #app/messages/en/app.php file, if language is set to English (en)?
EDIT: By default, I was not altering default i18n component configuration in my application's configuration as I found no need for that. Translation files are stored in default position (#app/messages/<language>/) and are using default class (yii\i18n\PhpMessageSource). And this is working for all languages except sourceLanguage. At some point, I tried to alter configuration:
'i18n' => [
'translations' => [
'*' => [
'class' => 'yii\i18n\PhpMessageSource',
'sourceLanguage' => 'en',
'basePath' => '#app/messages'
],
],
],
But, it brought no change (why should it -- it still uses default settings).
According to samdark at Yii Forum, this is by design. Translations are not performed, if language = sourceLangage.
To workaround this, and force translations in this case, one must set forceTranslation to true.
Therefore, one must add / modify i18n component in components section of application's config in the way similar to this:
'i18n' => [
'translations' => [
'app' => [
'class' => 'yii\i18n\PhpMessageSource',
'forceTranslation' => true
],
],
],
Solution:
'translations' => [
'app*' => [
'class' => 'yii\i18n\PhpMessageSource',
'basePath' => '#app/messages',
'sourceLanguage' => 'en_US',
'fileMap' => [
'app' => 'app.php',
],
],
Answers for your comment:
1) 'sourceLanguage' => 'en_US' - You must use full locale. Because English locale may be en_US, en_UK and e.t.c. The format for the language/locale is ll-CC where ll is a two- or three-letter lowercase code for a language according to ISO-639 and CC is the country code according to ISO-3166. from [doc][1]
2) In key use category. And category set in Yii::t('category'...)

Rails Gem Geocoder does not obey per request language setting?

I am trying to get a different language version of an address from Geocoder using Google API. I have tried to geocode the result using the following:
Geocoder.search("Effel Tower", language: :fr)
or
Geocoder.search("Effel Tower", {language: :fr})
but nothing works. It would work if I change the configuration:
Geocoder::Configuration.locale = :fr
Geocoder.search("Effel Tower")
I have been searching for an answer... even to look into the code. https://github.com/alexreisner/geocoder/blob/master/lib/geocoder/lookups/google.rb. In the google.rb language parameter is handled so it should work: :language.
Any idea? Thank you :)
I've found the answer. The language setting should be sent as string, not symbol. As a result:
Geocoder.search("Effel Tower", language: "fr")
will return French result. Thank you.

Can I use a regular expression to extract the domain from a URL?

Suppose I want to turn this :
http://en.wikipedia.org/wiki/Anarchy
into this :
en.wikipedia.org
or even better, this :
wikipedia.org
Is this even possible in regex?
Why use a regex when Ruby has a library for it? The URI library:
ruby-1.9.1-p378 > require 'uri'
=> true
ruby-1.9.1-p378 > uri = URI.parse("http://en.wikipedia.org/wiki/Anarchy")
=> #<URI::HTTP:0x000001010a2270 URL:http://en.wikipedia.org/wiki/Anarchy>
ruby-1.9.1-p378 > uri.host
=> "en.wikipedia.org"
ruby-1.9.1-p378 > uri.host.split('.')
=> ["en", "wikipedia", "org"]
Splitting the host is one way to separate the domains, but I'm not aware of a reliable way to get the base domain -- you can't just count, in the event of a URL like "http://somedomain.otherdomain.school.ac.uk" vs "www.google.com".
/http:\/\/([^\/]*).*/ will produce en.wikipedia.org from the string you provided.
/http:\/\/.{0,3}\.([^\/]*).*/ will produce wikipedia.org.
yes
Now I know you haven't asked for how, and you haven't specified a language, but I'll answer anyway... (note, this works for all language subsites, not just en.wikipedia...)
perl:
$url =~ s,http://[a-z]{2}\.(wikipedia\.org)/.*,$1,;
ruby:
url = url.sub(/http:\/\/[a-z]{2}\.(wikipedia\.org)\/.*/, '\1')
php:
$url = preg_replace('|http://[a-z]{2}.(wikipedia.org)/.*|, '$1', $url);
Of course, for this particular example, you don't even need a regex, just this will do:
url = 'wikipedia.org'
but I jest...
you probably want to handle any URL and pull out the domain part, and it should also work for domains in different countries, eg: foo.co.uk.
In which case, I'd use Mark Rushakoff's solution to get the hostname and then a regex to pull out the domain:
domain = host.sub(/^.*\.([^.]+\.[^.]+(\.[a-z]{2})?)$/, '\1')
Hope this helps
Also, if you want to learn more, I have a regex tute online: http://tech.bluesmoon.info/2006/04/beginning-regular-expressions.html
Sure all you would have to do is search on http://(.*)/wiki/Anarchy
In Perl (Sorry I don't know Ruby, but I expect it's similar)
$string_to_search =~ s/http:////(.)//. should give you wikipedia.org
to get rid of the en, you can simply search on http:////en(.)//......
That should do it.
Update: In case you're not familiar with Regex, I would recommend picking up a Regex book, this one really rocks and I like it: REGEX BOOK,Mastering Regular Expressions, I saw it on half.com the other day for 14.99 used, but to clarify what i suggested above, is to look for the string http://en, then for anything until you find a / this is all captured in $1 (in perl, not sure if it's the same in ruby), a simple print $1 will print the string.
Update: #2 sorry the star in the regex is not showing up for some reason, so where you see the . in the () and after the // just imagine a *, oh and I forgot for the en part add a /. at the end that way you don't end up with .wikipedia.org

Resources