Symfony/Doctrine: Fallback to default culture for i18n content - symfony1

I am building a multilingual website with Symfon 1.4/Doctrine, having English language defined as primary.
Content translation in other languages are added gradually, and there will very often be situations when translation of the content requested will be unavailable in requested language. I want to display the requested content in English in those cases.
Is this achievable at global level, e.g. for all the i18n content?
As pointed by Grad van Horck, this works fine by default for interface translation.
What I need is the same functionality for content stored in DB (models having "actAs i18n" behavior).

The default way allows perfectly for this. Just make sure all your texts are in English by default, and translate them where needed. So just do <?php echo __('Hello'); ?>, and then translate it if you want. If a translation can't be found, it just falls back at it's 'original'.
To do the same in your database, you'd probably be best of copying the i18n behaviour (Template/Listener) and add your own bit of logic to fall back on English.

It's possible to set the default culture on sfDoctrineRecord (see http://trac.symfony-project.org/ticket/5458)
sfDoctrineRecord::setDefaultCulture('nl'); // default = 'en'
This changes the i18n fallback when the translation isn't available in the database.

Related

using 18n and I18n:Alchemy in rails in order to translate a text value according to the locale

In my application, there is a field credit_debit that has two string values: 'credit' and 'debit'.
These two terms should be shown in a localized form to the user, so that when the app is using an italian locale they should be translated into 'avere' and 'dare'.
I could write my own setter/getter method for the credit_debit field and handle this issue inside it, but maybe I could leverage the I18n architecture to do so in a way that is consistent with the rest of the localization. If it's possible, how can I implement this?
Can't see the problem if you're already using i18n localization.
Just use the field as a key for i18n (with a prefix if you like)
t("prefix_#{credit_debit}")
and just define the strings for both your locales.
So in your en locale:
prefix_credit: credit
prefix_debit: debit
And in your it locale:
prefix_credit: avere
prefix_debit: dare

Hiding locale parameter in rails 3.1?

i want to implement the following behaviour:
if the users browser-language is e.g. EN, he should be redirected to a the url http://foo.bar/hello, if the browser-language is DE then to http://foo.bar/hallo.
so how do i need to set my routes to redirect the user to the right language (e.g. when an english user requests the DE route and vice versa) and how can i set a hidden locale parameter, so i can load the right view in the controller?
i want to use the same controller for both languages (one method per page), but localized views (foo.en.html.erb etc.)
thanks in advance!
I don't think that what you want to try to get is a good idea, and I will explain that here. I don't understand why you would choose a different approach from the ones that are provided by Rails out of the box, and explained in details in the "Internationalization Guide, Sections 2.3 and further".
Here are the arguments:
Rails provides at least 3 different ways to change the locale:
Getting it from parameters: http://my.example.com/books?locale=de
Getting it from the sub-domain: http://de.example.com/books
Client supplied application, like the accept-header
All have the advantage that the controller and action will be the same, which is what you normally want to have.
There are helper methods if you want to change your behavior depending on the locate: locale, ...
However, you may localize views as a whole if you want to do: see localized views. This will perhaps lead to a duplication in view code.
Or you use the translation and localization API as in I18n.t 'store.title' or I18n.l Time.now.
A hidden locale parameter has the disadvantage that it is not obvious (for the user) which locale is used.
If you need to translate your routes in addition to set the locale, you may have a look to the translate_routes gem.
The README explains how you can set the locale from your translation hello/hallo.

struts2 localization by embedding the locale code in the action name rather than by using ...?request_locale=<locale_code>

hi all,
i want to make localization feature in a website written in struts 2. as far as i know, the standard way of doing so is using get in the following manner:
http://.../namespace/action_name?request_locale=<locale code>
however, my boss doesn't like such hairy url. instead, i'm required to write it in the following manner:
http://.../namespace/a_param/<locale code>/another_param...
i tried to change the action mapping in my struts.xml into something like
<action name="*/*..." ... >
<param name="locale">{2}</param>
...
</action>
it doesn't work
after i changed it into
<action name="*/*..." ...>
<param name="request_locale">{2}</param>
...
</action>
it doesn't work either T_T
by the way, i know there is trick of putting ActionContext.getContext().setLocale(new Locale(...)); in action which basically change the locale for that instance. however, it seems that the effect will only be transient (in contrast, i18n saves the chosen locale in session, which basically makes it quite persistent.
so, how to change the locale by embedding the locale code in the url?
Your help is highly appreciated =D
I have not done much with locals but i18n should automatically determine the correct local from the browser via the headers, there is no need for anything to be in the url. As long as there is a language bundle for the particular locale it will try to pull properties from that file.
This page shows an example of using basic i18n (only looked at it for a moment, personally I always start at http://struts.apache.org/2.x/ but the tutorial/guides are a bit dry.
Why do you need to refer to anything in the url at all concerning language? Personally if the user did want to override the default locale I would provide some form of control (menu) to do so. Then I would set a variable in session then I would create an interceptor which would call setLocale on the action using the local parameter on the session (if there is a value set of course). This way there would not be any need to embed parameters into individual pages and the local is out of the url all together.
There is a way to do what you want with the url... Something to do with conventions and slashes in allowing slashes in the action name I think. I'll post back if I remember. But I think the above is generally a better approach anyways.
Edit: Taking into consideration what you are trying to accomplish I can see two very different solutions.
1) You can use a proxy, the incoming URL www.example.com/en/ and www.example.com/fr/ can be mapped to different web applications or even the same web application but the url is re-written into a form that suites your application. Tools that can do this include: iptables, apache mod_rewrite, squid... and a multitude of others. This type of solution is more valuable if you handle multiple ip addressses/urls/applications on one server.
2) You can set the struts2 property struts.enable.SlashesInActionNames then using wildcards you can do something like:
<action name="*/*">
<result>/WEB-INF/content/{1}/{2}.jsp</result>
<action>
You can also pass parameters to actions each asterisk found in the action name becomes {1}, {2}, etc. It sounds like you might need this feature. If someone else knows it escapes me at the moment how you would capture parts of the url like this with struts2-conventions-plugin so the action can make use of them I would find that interesting.
#Quaternion
basically the intention is that, the website has several national "sub-website". based on user's ip address, he/she will be redirected to the national "sub-website". it's like when you open www.google.com, you may be redirected to www.google.com.country_domain.
each national "sub-website" has several languages, with 1 default language. it's just like when you open google israel website, by default you will see a website written in hebrew language, although you can override this default choice by choosing it to be in english.
in my planned website, following isreal website and hebrew language example, it is supposed to be like this:
the user is in israel
he is opening www.abcdef.com
the server is recognizing that the client is in israel. there are 3 languages can be chosen for the israel "sub-website": hebrew, arabian, english. the default one is hebrew, but client can override this choice
the user is then redirected to www.abcdef.com/il/he ("il" stands for israel country and "he" stands for hebrew language)
but the user is apparently a british tourist with no knowledge on hebrew or arabian language. so he/she chose english language
he/she will be redirected to www.abcdef.com/il/en ("en" stands for english language)
the next time that client opens www.abcdef.com in israel again (assuming the cookies & sessions are still around), he/she will be redirected to www.abcdef.com/il/en
thx fr your help =D
Definitely I would leave the responsibility to handle the Locale to an interceptor.
Here is a tutorial to Create an Interceptor.
This interceptor can be placed in a common stack shared by all (or most of) incoming requests and it will assign the locale
ActionContext.getContext().setLocale(locale);
with the proper logic that could take into account:
query-string parameters
stored user preferences
cookies
session
browser preferences (are in the request)

J2EE + Struts2: Internationalization + Text Alignment

I want to know following about struts2:
Does internationalization works for Arabic & English? Any special considerations?
How can I change the alignment of text when locale is changed? For English it will be left aligned and for Arabic it will be right aligned?
How can I intercept (during) the change of locale so, that I can reload the information on the page which is being fetched from the database?
We also need to adjust some styles (css) since text with non-english alphabet/characters tend to take up space differently. Can we also “internationalize” css files?
Any working example with this scenario.
Any help will be great.
BR
SC
1) Yes. Make sure all characters in the message bundle are unicode escaped and not the actual characters.
2) You can use the css or html: see http://www.i18nguy.com/markup/right-to-left.html. In out application, the Arabic is actually in the file in the proper direction so we did not have to change any styles.
3) In Struts 2 you can set the session WW_TRANS_I18N_LOCALE. See http://struts.apache.org/2.0.14/docs/localization.html and http://struts.apache.org/2.0.14/docs/i18n-interceptor.html
We had some trouble getting the language to store so we explicitly set that variable in the session
Locale locale = (Locale) getSession().get("WW_TRANS_I18N_LOCALE");
if (!(locale instanceof Locale) || !locale.getLanguage().equals(new Locale(languageCode, "", "").getLanguage())) {
getSession().put("WW_TRANS_I18N_LOCALE", LocaleUtility.langToLocale(languageCode));
}
4) Yes you can create mycss-ar.css, mycss-en.css and load the proper file based on the locale.
5) Sorry I don't have any code to supply.

How should I localize content in an ASP.NET MVC application?

This is a question about setting our website's Language and Culture settings with regards to the settings we read from the user visiting the site.
Let's assume our website supports 2 languages, English (en) and German (de). Let's also assume we want to disregard locale (region) (at least on the server side, so we only know that we support "en" and "de", so we have that specified either in application code, config file or somewhere elese). So we don't care if a user comes from US or UK.
What we are doing is matching "en" or "de" to possible matches in user's browser defined languages/cultures.
The issue I am having is that if I do this
/* Gets Languages from Browser */
IList<string> BrowserLanguages = filterContext.RequestContext
.HttpContext
.Request
.UserLanguages;
we get all sorts of results.
We might receive lists like
en, (for instance Firefox has this), - en-US, - en-UK.
en-US, - en-UK.
en, - de, - it-IT.
de, - en-US, - en.
What I would like to ask here is:
Is it ok to use compare strings here (checking whether "en" exists as a substring)? See sample list 2
Do we have to take the order into account or would you just disregard it?
Am I overcomplicating this? The problem is though that IE and Firefox (and others) have different strings for regional settings (for instance, "sl" in Firefox and "sl-SI" in IE8)
I just want to direct all visitors for which language does not exist to English and all others to their appropriate language (disregarding their location), you might think of it like if we support Portugese (pt) and our visitors come from Portugal and Brazil we will redirect them to Portugese version of the site even if the match is not 100% perfect (we would rather redirect them to Portugese version than English version).
Interesting question. Let me try to answer...
Is it ok to use compare strings here (checking whether "en" exists as a substring)?
You could something like this. Note, I am just providing a way that does not use strings, however, I think that in this case substring approach will also work since its simpler.
CultureInfo enCulture = new CultureInfo("en"); // use "de"
var langPref = Request.UserLanguages[0];
var userCulture = CultureInfo.GetCultureInfo(langPref);
var baseCulture = CultureInfo.GetCultureInfo(cult.TwoLetterISOLanguageName); // get the base culture
var isSame = baseCulture.Equals(enCulture);
What about using the Headers["Accept-Language"]. Section 14.4 Accept-Language of RFC 2616. There may be a bit more work involved using this, but off hand it seems that that it can hold more valuable information.
Do we have to take the order into account or would you just disregard it?
The UserLanguages array is sorted by preference (MSDN). Having said that, I would assume that each browser has its own specific way to create the Language String (I stand under correction, but I think that FF4 is considering removal of this part of the user-agent string). You could check each language and decide when the correct language is found using the approach described above.
Am I overcomplicating this? The problem is though that IE and Firefox (and others) have different strings for regional settings (for instance, "sl" in Firefox and "sl-SI" in IE8)
To me localisation is tricky. I would suggest having read through RFC 1766 and RFC 2616 (HTTP Protocol, Section 3.10.
I hope this helps.

Resources