How to implement Runtime Locales in GWT - gwt

I am trying to implement Runtime Locales in GWT, But I could not able to get it working. There is very limited documentation available on net. So could you please help me in implementing Runtime Locales in GWT. It will be helpful if someone give an example on how to implement Runtime Locales, because I have already spent lot of time on implementing Runtime Locales. So if somebody explain how to implement it using an example that would be great.
The reason I need Runtime Locale is, I want to get date format, month names and weekdays names based on locale string (Ex : en_US, es_MX etc), I mean I want to obtain Locale object from locale string as it is possible in Java. As of my knowledge we cannot get information about any other locale than the default locale that GWT has loaded. and i don't want to use Compile time Locales because of the compile time overhead and increase on static footprint.
This is the documentation for implementing Locale in GWT which I am referring - http://www.gwtproject.org/doc/latest/DevGuideI18nLocale.html
Thanks,
Madhusudhan.

I think you miss one important thing about runtime locales:
[...] all locales that GWT knows about that inherit from your compile-time locale will be automatically included [...]
As an example, you might have one set of translations for all of Spanish as spoken in Latin America (es_419), yet allow users to choose a country-specific locale such as Argentinian Spanish (es_AR).
Note that DateTimeFormatInfoImpl_es_AR extends DateTimeFormatInfoImpl_es_419.
But there is a trick that allow you to get date format in any locale by getting DateTimeFormatInfo implementation directly for given locale:
DateTimeFormatInfo format = new DateTimeFormatInfoImpl_es_MX();
format.dateFormatLong(); // d 'de' MMMM 'de' y

Related

get Current Date in Scala without java class

Is there any way to get the current date in Scala without using Java class import java.text.SimpleDateFormat and import java.util.Date?
java.time
You should not be importing java.text.SimpleDateFormat or java.util.Date. Those troublesome old classes are now legacy, supplanted by the java.time classes.
While I do not know Scala syntax, here is Java syntax.
java.time.Instant.now() // Current moment in UTC, with a resolution of nanoseconds.
…and…
java.time.ZonedDateTime.now( ZoneId.of( "Africa/Tunis" ) ) // Current moment adjusted to the wall-clock time as used by the people in a particular region (time zone).
…and…
java.time.LocalDate.now( ZoneId.of( "Pacific/Auckland" ) ) // Current date (date-only, no time-of-day nor zone) of the people in a particular region (time zone).
http://pavkin.ru/cross-platform-polymorphic-datetime-values-in-scala-with-type-classes/
The Goal
There’s no solution without a goal. Precise goal will also provide correct context for reasonings in this article. So let me state it.
My primary goal is to be able to write cross-platform code that operates on date/time values with full time zone support.
This also means that I will need implementation(s) that behave consistently across JVM and browser. We’re Scala programmers, so let’s choose JVM behaviour semantics as our second goal.
Options explored in the article are
https://github.com/scala-js/scala-js-java-time
This library is the future of cross-platform date/time code. It’s effectively Java 8 time, written from scratch for ScalaJS.
At the time of writing this post, scala-js-java-time already provides LocalTime, LocalDate, Duration , and a handful of other really useful java.time.* classes (full list here).
https://github.com/soc/scala-java-time
Scala Java-Time is a fork of ThreeTen backport project. So it’s main purpose is to provide java.time.* -like functionality on Java 6 & 7.
It is also compiled to ScalaJS, which means we can write cross-platform code with it. And we can even use (to some extent) LocalDateTime!
https://github.com/mdedetrich/soda-time
Soda time is a port of Joda to ScalaJS.
It’s in early development stages and also doesn’t have time zones in ScalaJS, but I still added it to the list, because developers took an interesting approach: they are converting original Joda code with ScalaGen.
The author then goes to recommend that at the time of writing the article, (Published 11.11.2016)
There’s no cross-platform library with full time zone support. And for JavaScript runtime there’s only MomentJS, that really fits our requirements.
So unless one of those have evolved, or you only need a subset of datetime functions, or another solution has raised it's head, you are probably stuck implementing some sort of interface over the behavior needed, and swapping it out based on whether you are on java or scalajs.
Well, it's unclear - beside the hint about newer classes mentioned by Basil - why you would do this, need this.
On unix-like systems you can use the System date, probably on Windows systems too, but with the same syntax? You probably loose system independency:
scala> import sys.process._
import sys.process._
scala> "date".!
Mi 31. Jan 07:09:04 CET 2018
res150: Int = 0
If you have a database, these often provide the date/time too.
Then you could try to get the date via TCP/IP.
All this solutions are in allmost all cases inferior to using the newer Java classes and even the older ones.

String to Time, but without the limitations of strtotime()

In my (non-public) applications, I like for my users to be able to enter the date/time in whatever format and then use strtotime() along with date() to translate it to YYYY-MM-DD for DB storage. I know this has a couple of drawbacks (such as how 1/1/13 vs. 1-1-13 might be handled based on locale and such), but over the years these have never been an issue given my user base.
However, there is one issue that I may have to resolve, and that's with handling dates prior to 1970 in one particular application. For obvious reasons, strtotime() doesn't work so well. I know I could use a date-picker, but was wonder if there is a more proper way of handling this that would allow me to do what I've been doing but to handle a wider range of dates.
After posting this, I did some additional testing, and it seems on the system I'm working on right now (Ubuntu 12.04 64-bit), there are no issues with early.. perhaps this is a 64-bit thing?
print date( "Y-m-d", strtotime( "May 5, 1820" ));
Prints 1820-05-05 as I wanted but not as I expected. I'm not sure if this is related to the version of Linux I'm running, the version of PHP I'm running, or what else. But since it hasn't worked this way for me in the past, I don't necessarily feel comfortable dealing with dates like this.
I did find the DateTime object, so I think I'm going to use that approach, especially since it is supposed to handle a much wider range of dates:
$date = new DateTime( "May 5, 1820" );
print $oop->format( "Y-m-d" );
I'll leave my answer unaccepted for a bit in case someone else has a better or more correct solution to this.

Where can I find a list of language + region codes?

I have googled (well, DuckDuckGo'ed, actually) till I'm blue in the face, but cannot find a list of language codes of the type en-GB or fr-CA anywhere.
There are excellent resources about the components, in particular the W3C I18n page, but I was hoping for a simple alphabetical listing, fairly canonical if possible (something like this one). Cannot find.
Can anyone point me in the right direction? Many thanks!
There are several language code systems and several region code systems, as well as their combinations. As you refer to a W3C page, I presume that you are referring to the system defined in BCP 47. That system is orthogonal in the sense that codes like en-GB and fr-CA simply combine a language code and a region code. This means a very large number of possible combinations, most of which make little sense, like ab-AX, which means Abkhaz as spoken in Åland (I don’t think anyone, still less any community, speaks Abkhaz there, though it is theoretically possible of course).
So any list of language-region combinations would be just a pragmatic list of combinations that are important in some sense, or supported by some software in some special sense.
The specifications that you have found define the general principles and also the authoritative sources on different “subtags” (like primary language code and region code). For the most important parts, the official registration authority maintains the three- and two-letter ISO 639 codes for languages, and the ISO site contains the two-letter ISO 3166 codes for regions. The lists are quite readable, and I see no reason to consider using other than these primary resources, especially regarding possible changes.
There are 2 components in play here :
The language tag which is generally defined by ISO 639-1 alpha-2
The region tag which is generally defined by ISO 3166-1 alpha-2
You can mix and match languages and regions in whichever combination makes sense to you so there is no list of all possibilities.
BTW, you're effectively using a BCP47 tag, which defines the standards for each locale segment.
Unicode maintains such a list :
http://unicode.org/repos/cldr-tmp/trunk/diff/supplemental/index.html
Even better, you can have it in an XML format (ideal to parse the list) and with also the usual writing systems used by each language :
http://unicode.org/repos/cldr/trunk/common/supplemental/supplementalData.xml
(look in /LanguageData)
One solution would be to parse this list, it would give you all of the keys needed to create the list you are looking for.
http://www.iana.org/assignments/language-subtag-registry/language-subtag-registry
I think you can take it from here http://www.unicode.org/cldr/charts/latest/supplemental/territory_language_information.html
This can be found at Unicode's Common Locale Data Repository. Specifically, a JSON file of this information is available in their cldr-json repo
We have a working list that we work off of for language code/language name referencing for Localizejs. Hope that helps
List of Language Codes in YAML or JSON?
List of primary language subtags, with common region subtags for each language (based on population of language speakers in each region):
https://www.unicode.org/cldr/charts/latest/supplemental/language_territory_information.html
For example, for English:
en-US (320,000,000)
en-IN (250,000,000)
en-NG (110,000,000)
en-PK (100,000,000)
en-PH (68,000,000)
en-GB (64,000,000)
(Jukka K. Korpela and tigrish give good explanations for why any combination of language + region code is valid, but it might be helpful to have a list of codes most likely to be in actual use. s-f's link has such useful information sorted by region, so it might also be helpful to have this information sorted by language.)

Is it bad practice to append localization codes to images?

I'm considering assembling image name strings based on device type, orientation, and localization.
For example:
#define kLocalCode NSLocalizedString(#"en", #"localization code")
...would result in "background_iphone_portrait_en.png" (or _es, _de, etc.)
It's faster in my workflow to do it this way instead of placing images with the same names in separate localized folders.
Are there any downsides with this method of image localization?
First:
I found this a really creative solution. And it makes me think.
This is not really an answer but:
You can load the 'en' with the following call:
[NSLocale currentLocale]
This would result that you do not have to translate en to fr, es or de, but can take it runtime.
If there is a language you do not support, load the english version.

How to convert an NSTimeInterval into an date with time?

I have an NSTimeInterval value, or more precisely an NSTimeInterval "since reference date". I think that's a value in seconds from 1970 or something like so. Pretty standard in most programming languages, I think.
So now I have that ugly value which the user doesn't understand, and I'd like to display a date + time. Is there a useful method or function that would do that, maybe by specifying formats or a locale as well? Maybe the iphone also has built-in support for this kind of stuff so that the date+time is displayed automatically like the user likes it in his/her settings?
You want to create an NSDate with +[NSDate dateWithTimeIntervalSince1970:]. You can then get the date's -descriptionWithLocale: or use the Date and Time Programming Guide from the iPhone library to find out more options for displaying the date.