I am using angular dynamic locale (tmhDynamicLocale), but want all my locales to be pre loaded into the cache (tmhDynamicLocaleCache) when the app starts. The motivation behind this is to have all my resources bundled into 1 minified js file rather than the standard lazy loading. Is there a full example for this anywhere?
Solved it.
What i did was to put each locale into a module like so:
angular.module("myapp.locale.en", [
'tmh.dynamicLocale'
])
.run(['tmhDynamicLocaleCache', function (tmhDynamicLocaleCache) {
var PLURAL_CATEGORY = {ZERO: "zero", ONE: "one", TWO: "two", FEW: "few", MANY: "many", OTHER: "other"};
function getDecimals(n) {
n = n + '';
var i = n.indexOf('.');
return (i == -1) ? 0 : n.length - i - 1;
};
function getVF(n, opt_precision) {
var v = opt_precision;
if (undefined === v) {
v = Math.min(getDecimals(n), 3);
}
var base = Math.pow(10, v);
var f = ((n * base) | 0) % base;
return {v: v, f: f};
};
tmhDynamicLocaleCache.put('en', {
"DATETIME_FORMATS": {
"AMPMS": [
"AM",
"PM"
],
"DAY": [
"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday"
],
"FIRSTDAYOFWEEK": 6,
"MONTH": [
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December"
],
"SHORTDAY": [
"Sun",
"Mon",
"Tue",
"Wed",
"Thu",
"Fri",
"Sat"
],
"SHORTMONTH": [
"Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun",
"Jul",
"Aug",
"Sep",
"Oct",
"Nov",
"Dec"
],
"WEEKENDRANGE": [
5,
6
],
"fullDate": "EEEE, MMMM d, y",
"longDate": "MMMM d, y",
"medium": "MMM d, y h:mm:ss a",
"mediumDate": "MMM d, y",
"mediumTime": "h:mm:ss a",
"short": "M/d/yy h:mm a",
"shortDate": "M/d/yy",
"shortTime": "h:mm a"
},
"NUMBER_FORMATS": {
"CURRENCY_SYM": "$",
"DECIMAL_SEP": ".",
"GROUP_SEP": ",",
"PATTERNS": [
{
"gSize": 3,
"lgSize": 3,
"maxFrac": 3,
"minFrac": 0,
"minInt": 1,
"negPre": "-",
"negSuf": "",
"posPre": "",
"posSuf": ""
},
{
"gSize": 3,
"lgSize": 3,
"maxFrac": 2,
"minFrac": 2,
"minInt": 1,
"negPre": "\u00a4-",
"negSuf": "",
"posPre": "\u00a4",
"posSuf": ""
}
]
},
"id": "en",
"pluralCat": function (n, opt_precision) {
var i = n | 0;
var vf = getVF(n, opt_precision);
if (i == 1 && vf.v === 0) {
return PLURAL_CATEGORY.ONE;
}
return PLURAL_CATEGORY.OTHER;
}
});
}])
;
Create a module like this for each locale you use, just change "en" to "pt" or "he" etc (3 places in the code above).
Next load these modules into your app.js...
angular.module('myapp', [
'myapp.locale.en',
'myapp.locale.pt',
'myapp.locale.de',
'myapp.locale.he'
])
This way when .run() is called on each of these modules it will put the locale object into the tmhDynamicLocaleCache. Then when you later use tmhDynamicLocale.set('en') it will find the locale object in the cache and use it.
Related
This is the data in json file. the _id i am passing is month number.
Const incomes = [
{
"_id": 6,
"total": 3000
},
{
"_id": 7,
"total": 3500
}
]
I am trying to convert the month number to month name by using this function. but its not working.
const monthNames = ["January", "Feburary", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
const data = useMemo(
() => incomes.map((income, monthNames) => ({ ...income, name: monthNames[income._id.getMonth()] }
)
), [incomes, monthNames]);
Try this!
I think when you passed the monthNames in the .map it declared it as a new variable and that's why the result was undefined.
P.S. Added -1 to income._id so the months would match
Hope it helps :)
const incomes = [
{
"_id": 6,
"total": 3000
},
{
"_id": 7,
"total": 3500
}
]
const monthNames = ["January", "Feburary", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
const data = useMemo(
() => incomes.map((income) => ({ ...income, name: monthNames[income._id -1]}
)
), [incomes, monthNames]);
I have an array of such structures:
typedef struct month {
char cName[20];
char cAbbr[20];
int iDays;
int iNumber;
} MONTH;
which is nested in another structure:
typedef struct input{
MONTH * pMonths;
DATE sDate;
CALCS sCalcs;
} INPUT;
I initialize this array of structs in stack like that:
MONTH * init_months(bool bLeap) {
MONTH months[NUM_MONTHS] = {
{ .cName = "January", .cAbbr = "Jan", .iDays = 31, .iNumber = 1 },
{ "February", "Feb", 28, 2 },
{ "March", "Mar", 31, 3 },
{ "April", "Apr", 30, 4 },
{ "May", "May", 31, 5 },
{ "June", "Jun", 30, 6 },
{ "July", "Jul", 31, 7 },
{ "August", "Aug", 31, 8 },
{ "September", "Sep", 30, 9 },
{ "October", "Oct", 31, 10 },
{ "November", "Nov", 30, 11 },
{ "December", "Dec", 31, 12 }
};
if(bLeap){
months[1].iDays++;
}
return months;
}
The question is, if i form a INPUT structure inside some function:
INPUT GetUserInput(void){
// init months
MONTH * pMonths;
pMonths = init_months(isLeap(sDate.iYear));
// some code here
...
INPUT sInput = { pMonths, sDate, sCalcs };
// return
return sInput;
}
then how to properly initialize array of MONTH and/or how to properly initialize INPUT so it would contain valid data when returned from GetUserInput() ?
You have 3 options:
Make it static (but all returned references will reference the same array).
static MONTH months[NUM_MONTHS] = {
Use malloc and compound literal
MONTH *months = malloc(sizeof(*months) * NUM_MONTHS);
/* check if memory was allocated */
memcpy(months, (MONTH[]){
{ .cName = "January", .cAbbr = "Jan", .iDays = 31, .iNumber = 1 },
{ "February", "Feb", 28, 2 },
{ "March", "Mar", 31, 3 },
{ "April", "Apr", 30, 4 },
{ "May", "May", 31, 5 },
{ "June", "Jun", 30, 6 },
{ "July", "Jul", 31, 7 },
{ "August", "Aug", 31, 8 },
{ "September", "Sep", 30, 9 },
{ "October", "Oct", 31, 10 },
{ "November", "Nov", 30, 11 },
{ "December", "Dec", 31, 12 }}, sizeof(*months) * NUM_MONTHS);
Wrap the table into another structure and return by value
typedef struct
{
MONTH months[NUM_MONTHS];
}YEAR;
YEAR init_months(bool bLeap) {
YEAR year = {{
{ .cName = "January", .cAbbr = "Jan", .iDays = 31, .iNumber = 1 },
{ "February", "Feb", 28, 2 },
{ "March", "Mar", 31, 3 },
{ "April", "Apr", 30, 4 },
{ "May", "May", 31, 5 },
{ "June", "Jun", 30, 6 },
{ "July", "Jul", 31, 7 },
{ "August", "Aug", 31, 8 },
{ "September", "Sep", 30, 9 },
{ "October", "Oct", 31, 10 },
{ "November", "Nov", 30, 11 },
{ "December", "Dec", 31, 12 }}};
if(bLeap){
year.months[1].iDays++;
}
return year;
}
I am working on Rails 6 API. This is what I get
"data": [
{
"invoice_details": {
"customer_name": "Dylan Sollfrank",
"invoice_number": "1060",
"invoice_status": "paid"
}
},
{
"transaction_number": "QB1589148496",
"customer_name": "Freeman Sporting Goods:55 Twin Lane",
"amount": {
"amount_to_pay": 86.4,
"payment_fee": 0.0
},
"created_time": "03:38 AM",
"created_date": "May 11, 2020",
"payment_method": "qb_payment",
"payment_status": "completed"
},
Following is my code
def get_payment_report_activity(invoice_transactions, timezone = Time.zone.name)
invoice_details = []
transaction_details = {}
amount = {}
invoice_transactions.group_by(&:paymentable_id).each do |key, transactions|
invoice = Invoice.find key
invoice_details.push(invoice_details:{
customer_name: invoice&.customer&.fully_qualified_name&.strip,
invoice_number: invoice&.doc_number,
invoice_status: invoice&.invoice_status
})
transactions.each do |transaction|
customer = transaction&.paymentable&.customer
amount[:amount_to_pay] = transaction&.amount_to_pay.to_f
amount[:payment_fee] = transaction&.payment_fee.to_f
transaction_details[:transaction_number] = transaction&.transaction_number
transaction_details[:customer_name] = customer&.fully_qualified_name&.strip
transaction_details[:amount] = amount
transaction_details[:created_time] = Customer.time_format(transaction.created_at.in_time_zone(timezone))
transaction_details[:created_date] = Customer.date_format(transaction.created_at.in_time_zone(timezone))
transaction_details[:payment_method] = transaction&.payment_method
transaction_details[:payment_status] = transaction&.payment_status
end
invoice_details << transaction_details
end
invoice_details
end
Now I need the hash transaction details inside the invoice_details hash label as transaction_details and there can be multiple transaction details inside the invoice_details
"data": [
{
"invoice_details": {
"customer_name": "Dylan Sollfrank",
"invoice_number": "1060",
"invoice_status": "paid",
"transaction_details: [{
"transaction_number": "QB1589148496",
"customer_name": "Freeman Sporting Goods:55 Twin Lane",
"amount": {
"amount_to_pay": 86.4,
"payment_fee": 0.0
},
"created_time": "03:38 AM",
"created_date": "May 11, 2020",
"payment_method": "qb_payment",
"payment_status": "completed"
},
{
"transaction_number": "QB1589148496",
"customer_name": "Freeman Sporting Goods:55 Twin Lane",
"amount": {
"amount_to_pay": 86.4,
"payment_fee": 0.0
},
"created_time": "03:38 AM",
"created_date": "May 11, 2020",
"payment_method": "qb_payment",
"payment_status": "completed"
}]
},
"invoice_details": {
"customer_name": "Dylan Sollfrank",
"invoice_number": "1060",
"invoice_status": "paid",
"transaction_details : {
"transaction_number": "QB1589148496",
"customer_name": "Freeman Sporting Goods:55 Twin Lane",
"amount": {
"amount_to_pay": 86.4,
"payment_fee": 0.0
},
"created_time": "03:38 AM",
"created_date": "May 11, 2020",
"payment_method": "qb_payment",
"payment_status": "completed"
}
},
}
you can try like this:
def get_payment_report_activity(invoice_transactions, timezone = Time.zone.name)
invoice_details = []
invoice_transactions.group_by(&:paymentable_id).each do |key, transactions|
invoice = Invoice.find key
transaction_details = []
transactions.each do |transaction|
transaction_hash = {}
amount_hash = {}
customer = transaction&.paymentable&.customer
amount_hash[:amount_to_pay] = transaction&.amount_to_pay.to_f
amount_hash[:payment_fee] = transaction&.payment_fee.to_f
transaction_hash[:transaction_number] = transaction&.transaction_number
transaction_hash[:customer_name] = customer&.fully_qualified_name&.strip
transaction_hash[:created_time] = Customer.time_format(transaction.created_at.in_time_zone(timezone))
transaction_hash[:created_date] = Customer.date_format(transaction.created_at.in_time_zone(timezone))
transaction_hash[:payment_method] = transaction&.payment_method
transaction_hash[:payment_status] = transaction&.payment_status
transaction_hash[:amount] = amount_hash
transaction_details << transaction_hash
end
invoice_details.push(invoice_details: {
customer_name: invoice&.customer&.fully_qualified_name&.strip,
invoice_number: invoice&.doc_number,
invoice_status: invoice&.invoice_status,
transaction_details: transaction_details
})
end
invoice_details
end
Below is the data i get from the rest API
{
"memberdetails":[
{
"id":46,
"customername":"Zack",
"phoneno":"1323223232",
"nickname":"Zack",
"regdate":"2017-12-27 18:38:36.185829",
"groupname":"Test group",
"regid":36,
"groupdesc":"Test Test test",
"groupicon":"new",
"tamount":"3100"
},
{
"id":46,
"customername":"Carol",
"phoneno":"254721493487",
"nickname":"Caro",
"regdate":"2017-12-28 23:47:22.317687",
"groupname":"Test",
"regid":36,
"groupdesc":"Test Test Test",
"groupicon":"new",
"tamount":"130"
}
]
}
From the above data i want to populate 2 arrays as i want to plot a graph in the below format. The lineChartData array should pick the tamount and the lineChartLabels Array should pick the customenames. Thank you.
public lineChartData:Array<any> = [{data: [15, 29, 24, 21, 26, 15, 10], label: 'Opening/Time'}];
public lineChartLabels:Array<any> = ["Dan","Were","Kibe","Zack","Evah","Chris","Jess"];
angular.module('testapp', [])
.controller('TestCtrl', function(){
this.inputData = {
"memberdetails": [
{
"id": 46,
"customername": "Zack ",
"phoneno": "1323223232",
"nickname": "Zack ",
"regdate": "2017-12-27 18:38:36.185829",
"groupname": "Test group ",
"regid": 36,
"groupdesc": "Test Test test ",
"groupicon": "new ",
"tamount": "3100"
},
{
"id": 46,
"customername": "Carol ",
"phoneno": "254721493487",
"nickname": "Caro ",
"regdate": "2017-12-28 23:47:22.317687",
"groupname": "Test ",
"regid": 36,
"groupdesc": "Test Test Test ",
"groupicon": "new ",
"tamount": "130"
}
]
};
this.expectedOutput = {
"lineChartData": [
{
"data": [
15,
29,
24,
21,
26,
15,
10
],
"label": "Opening\/Time"
}
],
"lineChartLabels": [
"Dan",
"Were",
"Kibe",
"Zack",
"Evah",
"Chris",
"Jess"
]
}
this.computedLineChartData = [{data:[],label:"Opening\/Time"}];
this.computedLineChartLabels = [];
for(var i=0;i<this.inputData.memberdetails.length; i++){
var curMember = this.inputData.memberdetails[i];
this.computedLineChartData[0].data.push(curMember.tamount);
this.computedLineChartLabels.push(curMember.customername);
}
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.7/angular.min.js"></script>
<div ng-app="testapp" ng-controller="TestCtrl as tc">
<pre>{{tc|json}}</pre>
</div>
In AngularJs we have function like angular.fromJson(data) which returns output in form of array . So you can easily convert json to array.
I'm using Angular Dynamic locale and Angular-Translate for internationalization and localization (i18n). And works well.
I like the idea of angular-translate that is possible to change the language without refresh the page.
Is possible to do the same with Angular Dynamic locale? If is possible, how can I get this?
All the words from angular-translate changed automatically, but not the words from angular_locale (datapicker, etc), that the users need refreshing the page.
Thanks!
Just in case you don't have absolute necessity to use Angular Dynamic locale,you can create your own LocaleFactory like this:
factory('LocaleFactory', function ( $locale, $translate) {
var locales = {
nl: {
"DATETIME_FORMATS": {
"AMPMS" : [
"AM",
"PM"
],
"DAY" : [
"zondag",
"maandag",
"dinsdag",
"woensdag",
"donderdag",
"vrijdag",
"zaterdag"
],
"MONTH" : [
"januari",
"februari",
"maart",
"april",
"mei",
"juni",
"juli",
"augustus",
"september",
"oktober",
"november",
"december"
],
"SHORTDAY" : [
"zo",
"ma",
"di",
"wo",
"do",
"vr",
"za"
],
"SHORTMONTH": [
"jan.",
"feb.",
"mrt.",
"apr.",
"mei",
"jun.",
"jul.",
"aug.",
"sep.",
"okt.",
"nov.",
"dec."
],
"fullDate" : "EEEE d MMMM y",
"longDate" : "d MMMM y",
"medium" : "d MMM y HH:mm:ss",
"mediumDate": "d MMM y",
"mediumTime": "HH:mm:ss",
"short" : "dd-MM-yyyy HH:mm",
"shortDate" : "dd-MM-yyyy",
"shortTime" : "HH:mm"
},
"NUMBER_FORMATS" : {
"CURRENCY_SYM": "\u20ac",
"DECIMAL_SEP" : ",",
"GROUP_SEP" : ".",
"PATTERNS" : [
{
"gSize" : 3,
"lgSize" : 3,
"macFrac": 0,
"maxFrac": 3,
"minFrac": 0,
"minInt" : 1,
"negPre" : "-",
"negSuf" : "",
"posPre" : "",
"posSuf" : ""
},
{
"gSize" : 3,
"lgSize" : 3,
"macFrac": 0,
"maxFrac": 2,
"minFrac": 2,
"minInt" : 1,
"negPre" : "\u00a4\u00a0",
"negSuf" : "-",
"posPre" : "\u00a4\u00a0",
"posSuf" : ""
}
]
}
}
};
return {
setLocale: function (key) {
$translate.use(key);
angular.copy(locales[key], $locale);
}
};
});
Similarly you can add other locals as well
Call setLocale to change the locale
run(function (LocaleFactory) {
LocaleFactory.setLocale('nl');
});
When ever your locale get changed you can call setLocale by providing the locale key as an argument. It will change your locale instantly