JQuery/Globalize: Method for finding best matching locale? - javascript-globalize

As an example I have loaded following locales:
en
de
de-CH
As code: Globalize.load( require('cldr-data').entireMainFor('en', 'de', 'de-CH'));
When I call const globalize = Globalize(locale); without having set locale loaded I get e.g. Error: Cannot find module './de-AT'
This means that I have to choose one of the existing locales:
de-CH -> choose de-CH
de-AT -> choose de
en-Latn-US -> choose en
it -> choose en
Since everybody needs this piece of code I expected this to exist in the library. Could you point me to a proper implementation?

Related

Java API which return language code from language display name

I have a service which gives the language display name as part of response. I want to retrieve the language code so that I can apply the internationalization for output file.
Expected input : English - United States
Output : en_US
You might use something like that:
Optional<Locale> locale = Arrays.stream(Locale.getAvailableLocales())
.filter(l ->
l.getDisplayLanguage().equals("English") &&
l.getDisplayCountry().equals("United States")
).findAny();
locale.ifPresent(System.out::println);

how to set iOS default localizable string for a key?

I have create a Localizable.strings file with 2 language, en & jp.
en: "key"="english";
jp: "key"="日本";
It works good when using en & jp.
_title.text = NSLocalizedString(#"key",nil);
But when I using other language,like German,the label will display key in the view.I want to show the key of en strings value default.
How to set to load en when the user using other language without en & jp?
you can create a Localizable.strings for "de", and then you can copy/paste from the "en" (if "en" was your main language you don't need to copy/paste, it will be created with the "en" strings)
But it's strange, I have my apps for "en" and "es" and the default is "en". I didn't do anaything.

How to translate a dynamic title attribute

I have this kind of translation to make :
<a tal:attributes="href troncon/url;
title string:Cette etape fait partie du troncon ${troncon/nom}"
tal:content="troncon/nom">Canal du centre</a>
You see that I have a dynamic title attribute that I want to be translatable.
I've tried like this :
<a tal:attributes="href troncon/url;
title string:Cette etape fait partie du troncon ${troncon/nom}"
tal:content="troncon/nom"
i18n:attributes="title">Canal du centre</a>
And like this :
<a tal:attributes="href troncon/url;
title string:Cette etape fait partie du troncon ${troncon/nom}"
tal:content="troncon/nom"
i18n:attributes="title"
title="Cette etape fait partie du troncon ${nom}">Canal du centre</a>
But this doesn't work (of course).
Any ideas ?
The result of the tal:attributes call is passed literally to the translation machinery. i18n:attributes matches it's keys against what tal:attributes generates, and if there is a match, the original attribute on the element is ignored (see this I18N article on the Zope 3 wiki.
This means that the result of "Cette etape fait partie du troncon ${troncon/nom}" will be looked up for translation, requiring you to provide translations for each variation of the sentence that can be made with all possible values of troncon/nom.
To get support for proper placeholders in this string you are better off creating a message id in the code that generates the troncon structure and translate it there, presumably in your view. You need:
A message id with the placeholder
Attach the nom value to the message id
Translate this message to the currently selected language
Include the result in your troncon structure
I generally do this is one step:
from zope.i18n import translate
from zope.i18nmessageid import MessageFactory
_ = MessageFactory('yourdomain')
troncon = dict(
...
nom=nom,
nomtitre=translate(
_(u'troncon_nomtitre', default=u'Cette etape fait partie du troncon ${nom}',
mapping=dict(nom=nom)),
context=self.request)
)
Do note that you need a request for the translation function to pick the correct language.
You can always force the translation on context.translate():
tal:attributes="foobar python:context.translate(string, domain='translationdomain')"
http://collective-docs.readthedocs.org/en/latest/i18n/internationalisation.html#manually-translated-message-ids
However, this might be against all best practices.

Liferay: Default Language by Site

I have one portal and several communities (LR 6.1 - sites). The communities allocate several regions with different languages, hence I would like set different default languages for each community.
Is it possible to set default language by site?
I need it for CMS requirements: every site has a own default language for content. For example site in Germany must have germany as default language, and site in Spain have a spanish as default language and so on.
I'm not aware that configuration setting for default language per site (if you mean community/organization...) exists.
For entire portal you have "Default Language" option under
Control panel -> Portal settings -> Display settings
But if you don't have your language there, or you want to remove/add some languages you can configure that in portal-ext.properties. For example
locales=hr_HR,en_US
EDIT (more info about bug mentioned in comment)
If you take a look at com.liferay.portal.service.impl.CompanyLocalServiceImpl
public void updatePreferences(long companyId, UnicodeProperties properties)
throws PortalException, SystemException {
PortletPreferences preferences = PrefsPropsUtil.getPreferences(
companyId);
try {
String newLocales = properties.getProperty(PropsKeys.LOCALES);
if (newLocales != null) {
String oldLocales = preferences.getValue(
PropsKeys.LOCALES, StringPool.BLANK);
if (!Validator.equals(oldLocales, newLocales)) {
validateLocales(newLocales);
LanguageUtil.resetAvailableLocales(companyId);
}
}
...
}
protected void validateLocales(String locales) throws PortalException {
String[] localesArray = StringUtil.split(locales, StringPool.COMMA);
for (String locale : localesArray) {
if (!ArrayUtil.contains(PropsValues.LOCALES, locale)) {
throw new LocaleException();
}
}
}
you will see that "newLocales" are validate against
if (!ArrayUtil.contains(PropsValues.LOCALES, locale)) {
throw new LocaleException();
}
so if you add new locale key, in Display page of Portal settings, which is not in portal-ext.properties or portal.properties you'll get LocaleException.
Pay attention that oldLocales are read from preferences (database) String oldLocales = preferences.getValue(PropsKeys.LOCALES, StringPool.BLANK);
and validated against portal.properties/portal-ext.properties if (!ArrayUtil.contains(PropsValues.LOCALES, locale)) { ...
PropsValues.LOCALES => public static String[] LOCALES = PropsUtil.getArray(PropsKeys.LOCALES);
As stated in comment this behavior will happen if (and only if) you add new locale (in GUI) that is not in portal.properties
locales=ar_SA,eu_ES,bg_BG,ca_AD,ca_ES,zh_CN,zh_TW,hr_HR,cs_CZ,da_DK,nl_NL,nl_BE,en_US,en_GB,et_EE,fi_FI,fr_FR,gl_ES,de_DE,el_GR,iw_IL,hi_IN,hu_HU,in_ID,it_IT,ja_JP,ko_KR,lo_LA,nb_NO,fa_IR,pl_PL,pt_BR,pt_PT,ro_RO,ru_RU,sr_RS,sr_RS_latin,sl_SI,sk_SK,es_ES,sv_SE,tr_TR,uk_UA,vi_VN
or if you modified locales in portal-ext.properties than if not in portal-ext.properties.
For example if you have in portal-ext.properties
locales=en_GB
you will not be able to add any other locale from GUI unles you first add new locale to portal-ext.properties AND then go to Display page of Portal settings and add it there again.
there might be a way based on how you organize your editors: The default language for web content is based on the current language (e.g. the default language) of the user. Typically german users might have their language set to german, in order to read the german content version, no?
Alternatively you'd have to customize Liferay (e.g. the webcontent editor) and manually set the default language based on some property in the current site - this might be stored as custom field aka expando. This way it will truly be based on the site that content is being created in, but this might conflict with the current user's expectation: What do they set their default language for when the editor doesn't honor this?
Still - in your case the expectation might even be what you describe, so it's a valid option.
Is this level of detail enough?

What's options are there for localisation in WebWorks?

I'm building a WebWorks version of an Android app that's localised into 39 languages.
At the moment all the localisations are in xml files of key-value pairs, one file per language .
Each language file has about 400 lines (roughly 40k per file).
Users can change the language used in app.
What options are there in WebWorks to solve this kind of situation?
I'd be more than happy to convert the resource files into any other kind of format to make working with it a better experience on the platform.
You could store each language set in a JavaScript file that you include/load as needed. (I've converted the XML data to a "Map" since it is just key/value pairs)
e.g. (just ignore my translations... I just Googled this, I'm by no means fluent in Spanish)
//Spanish File "lang_spanish.js"
var translations = {
"lose_a_turn": "pierde un turno",
"do_not_pass_go": "huele como un camello",
"take_a_card": "tener una tarjeta de",
"you_win_the_game":"sin motocicletas en la biblioteca",
"you_can_not_move":"desbordamiento de la pila puede ser un lugar divertido"
};
In your <head> you can then have a generic script tag, that you just change the source of as needed.
e.g.
<script id="langFile" src="js/lang_english.js"></script>
When you want a different language, just remove this script from the DOM and add your new one. e.g.
function switchLang(langName){
var headTag = document.getElementsByTagName('head')[0];
var scriptFile = document.getElementById('langFile');
headTag.removeChild(scriptFile);
var newScript = document.createElement('script');
newScript.id = 'langFile';
newScript.src = 'js/lang_' + langName + '.js';
headTag.appendChild(newScript);
}
//call with:
switchLang('spanish');
The alternative would be to load all 39 languages by default... but that seems like overkill considering most will only ever want 1 or 2.

Resources