globalize numbers and currency: en-IN/INR format not working as expected - javascript-globalize

I have two methods i'm trying to write using globalize. One to format Numbers and one to format Currency.
function currencyFormatter(value, locale, fieldDefinition) {
var formatter = Globalize('en').currencyFormatter( 'INR' )
return formatter( value )
}
function numberFormatter(value, locale, fieldDefinition){
var formatter = Globalize('en').numberFormatter()
return formatter( value )
}
When I use the 'en' locale with 'INR' currency code for currencyFormatter or 'en' for numberFormatter i'm expecting to see something like ₹12,34,567.89 however what i get in both cases is something like ₹1,234,567.89 (minus currency symbol for number). I looked inside my node_modules folder cldr-data/main/en-IN/numbers.json and found this:
"percentFormats-numberSystem-latn": {
"standard": "#,##,##0%"
},
"currencyFormats-numberSystem-latn": {
"currencySpacing": {
"beforeCurrency": {
"currencyMatch": "[:^S:]",
"surroundingMatch": "[:digit:]",
"insertBetween": " "
},
"afterCurrency": {
"currencyMatch": "[:^S:]",
"surroundingMatch": "[:digit:]",
"insertBetween": " "
}
},
"standard": "¤ #,##,##0.00",
"accounting": "¤#,##0.00;(¤#,##0.00)",
I also went to CLDR website and dug up what i believe is the format file (en-IN.xml) and found this:
<rulesetGrouping type="OrdinalRules">
<ruleset type="digits-ordinal">
<rbnfrule value="-x">−→→;</rbnfrule>
<rbnfrule value="0">=#,##0=$(ordinal,one{st}two{nd}few{rd}other{th})$;</rbnfrule>
</ruleset>
</rulesetGrouping>
Both of these support the results we are getting are correct and our assumptions on what we expect are wrong. However, if you look out on the internet there are many reputable sites that say our assumptions are correct and the results are incorrect.
This leaves me in a state of not knowing what to do. Who's right? Can you help us to answer this question?
Also, is there a way to "overwrite" the default format if for whatever reason we wanted to change it?

You should use en-IN for English as spoken in India, i.e., Globalize('en-IN').

Related

How to dynamically set binding type's "formatOptions" and "constraints" in XML with binding?

I have a list of elements (OData set) and use a binding to show this list.
One field is for a quantity value and this value could sometimes need some decimal places.
The requirement is: only show that amount of decimal numbers that is also available in the OData service.
Annotation techniques can't be used.
I 'hacked' something that is misusing a formatter to update the type of a binding. But this is 'a hack' and it is not possible to convert it to XML views. (The reason is a different handling of the scope the formatter will be called).
So I am searching for a working solution for XML views.
The following code would not work but shows the issue:
new sap.m.Input({ // looking for an XML solution with bindings
value: {
path: "Quantity",
type: new sap.ui.model.type.Float({
// formatOptions
maxFractionDigits: "{QuantityDecimals}",
// ...
}, {
// constraints
minimum: 0
}),
// ...
}
});
The maxFractionDigits : "{QuantityDecimals}" should be "dynamic" and not a constant value.
Setting formatOptions and constraints dynamically in XML (via bindings or a declared function) is unfortunately not (yet) supported. But IMHO this is a valid enhancement request that app developers would greatly benefit from, since it encourages declarative programming.
I already asked for the same feature some years ago but in a comment at https://github.com/SAP/openui5/issues/2449#issuecomment-474304965 (See my answer to Frank's question "If XMLViews would allow a way to specify the dynamic constraints as well (e.g. enhanced syntax), would that fix the problem?").
Please create a new issue via https://github.com/SAP/openui5/issues/new with a clear description of what kind of problems the feature would resolve and possibly other use cases (You can add a link to my comment). I'll add my 👍 to your GitHub issue, and hopefully others too.
I'll update this answer as soon as the feature is available.
Get your dynamic number from your model and store it in a JS variable.
var nQuantityDecimals = this.getModel().getProperty("/QuantityDecimals");
new sap.m.Input({
value : {
path : "Quantity",
type : new sap.ui.model.type.Float({
maxFractionDigits : nQuantityDecimals,
source : {
groupingSeparator: ",",
decimalSeparator: ".",
groupingEnabled: false
}
}, {
minimum:0
})
}
}),

Tackle Localization with Graphql

In a json file, I have some fields that have a list of dictionaries with the locale and the translated text:
description_localized: [
{
locale: "en-UK",
value: "Read along"
},
{
locale: "de-DE",
value: "Lesen und"
},
{
locale: "fr-FR",
value: "Lis avec"
},
]
There are other fields where there are two keys in the value key:
path_localized: [
{
locale: "de-DE",
value: "{"identifier"=>"", "value"=>"http://path_6987.path"}"
},
{
locale: "es-ES",
value: "{"identifier"=>"", "value"=>"http://path_4685.path"}"
},
I already created a schema that grabs these fields, but instead of pulling the entire dictionary, on Graphql, I will need to be able to make a query where I only specify the language-locale I want (e.g. fr-FR) and that would only fetch the translated value for that one language (fr-FR). I don't want to get an array of all localized languages as this will be very taxing and will take longer to load per query.
Does anyone have any ideas on how I could accomplish this? Maybe through a resolver where I pass in the language as a parameter? But where would the languages be defined? I may need to clean this but we have an assortment of locale definitions: en_UK, en-UK, UK, en_UK_CO... Also, if the queried language is not found, how could I define a fallback?
Any guidance would be appreciated.
Thanks!
What stack are you working on, Node.js? If so, you can find a localization example here: https://github.com/kriasoft/nodejs-api-starter ..it uses "i18next" module to detect user's language, fallback to default language etc. then you can pass "lng" string as a context variable and use that inside of your "resolve()" methods. See src/app.js, src/Context.js.

ios application Localization

How can I set ios application supported languages?
e.g I use NSDate to get current day. If the device language is other than my supported languages NSDateFormatter returns "day" in device's language but I want to get in English if I don't support that language.
I know there is a way to get day in specific language using NSLocal but I don't want to do that way because I need to convert other strings as well.
The Apple documentation covers this pretty clearly. I know all you need is the word "day", but the following will help you include any word for any language if you do as follows:
1) You need to place all of the words (Strings) in your application into a single .swift file. Each word should be returned in a function that converts this string into the localized string per the device's NSLocal set in the device settings:
struct Localization {
static let all: String = {
return getLocalized("All")
}()
static let allMedia: String = {
return getLocalized("All Media")
}()
static let back: String = {
return getLocalized("Back")
}()
// ...and do this for each string
}
2) This file should also contain a static function that will convert the string:
static func getLocalized(_ string: String) -> String {
return NSLocalizedString(string, comment: "")
}
Here, the NSLocalizedString( method will do all of the heavy lifting for you. If will look into the .XLIFF file (we will get to that) in your project and grab the correct string per the device NSLocale. This method also includes a "comment" to tell the language translator what to do with the "string" parameter you passed along with it.
3) Reviewing all of the strings that you placed in your .swift file, you need to include each of those into an .XLIFF file. This is the file that a language expert will need to go over and include the proper translated word per string in the .XLIFF. As I stated before, this is the file that once included inside your project, the NSLocalizedString( method will search this file and grab the correct translated string for you.
And that's it!

Check if entered text is valid in Xtext

lets say we have some grammar like this.
Model:
greeting+=Greeting*;
Greeting:
'Hello' name=ID '!';
I would like to check whether the text written text in name is a valid text.
All the valid words are saved in an array.
Also the array should be filled with words from a given file.
So is it possible to check this at runtime and maybe also use this words as suggestions.
Thanks
For this purpose you can use a validator.
A simple video tutorial about it can be found here
In your case the function in the validator could look like this:
public static val INVALID_NAME = "greeting_InvalidName"
#Check
def nameIsValid(Greeting grt) {
val name = grt.getName() //or just grt.Name
val validNames = NewArrayList
//add all valid names to this list
if (!validNames.contains(name)) {
val errorMsg = "Name is not valid"
error(errorMsg, GreetingsPackage.eINSTANCE.Greeting_name, INVALID_NAME)
}
}
You might have to replace the "GreetingsPackage" if your DSL isn't named Greetings.
The static String passed to the error-method serves for identification of the error. This gets important when you want to implement Quickfixes which is the second thing you have asked for as they provide the possibility to give the programmer a few ideas how to actually fix this particular problem.
Because I don't have any experience with implementing quickfixes myself I can just give you this as a reference.

Override Grails Error Messages to format Dates and Numbers

I have created a domain with a Double field. When the validation occurs it throws the error message with size value showing the number with commas. Following are the detials
Groovy Class
class Quote {
String content;
Double size;
static constraints = {
content(maxSize:1000, blank:false)
size(min: 0.00D, max:999.99D)
}
}
Value entered "11111", error obtained "Size 11,111 is exceeded the limit". I have added the property key/value pair in messages.properties.
Here, I would like to get the message back without the commas. My main aim is to take the key and format the message returned based on my requirements. I require this as I have other fields that need conversion. For example, a date is validated but when showing the error the Gregorian date needs to be converted to an Islamic date and shown to user.
Does anyone know if I can do something to make this work.
I have tried the solution provided in http://ishanf.tumblr.com/post/434379583/custom-property-editor-for-grails but this did not work.
I have also tried modifying the messages values, but this is not flexible in case of my date issue. Example, for a key value pair, instead of using {2} as a place holder I could use {2, date, mm/dd/yyyy}, but for Islamic dates I want to format and show things differently.
Also, please note I have created a separate key for default date formatting for my application.
Would appreciate the help.
In grails, the return of a constrain is an already translated string.
You can create a taglib to format that, or enhance the
Another option would be custom validators. A custom validator can return false or a key when failing.
For example in your domain class, to vaildate a field:
myDateField validator: {val, obj -> obj.myShinyDateValidatorMethod(val) }
private myShinyDateValidatorMethod() {
if (isNotValidDate(val) {
return [the_message_key, val.formatedAsYouWand]
}
}
and, in your properties file you have to have defined the key:
the_message_key=This date: {3} is not valid
The trick here is that in the return from the validator, first string is the key and the rest are parameters for that key, but grails already uses {0}, {1}, {2} placeholders for className, fieldName and value, and the first parameter that you pass will be used as {3} placeholder.
Hope this helps

Resources