How do apps display stored notifications as localised strings? - ios

I'm trying to localise my app, and have noticed that apps like Twitter and Facebook immediately switch the language used in previous notifications stored on the server to match the devices language. How are they doing that?
For example is someone comments on a post so that the notifications is:
John commented "hi" on your post.
It's later translated into French if my device language is set to French.
Would they store a localised string such as:
%# commented %# on your post.
And then somehow reference that string in the notification along with the variables?

There are many localisation related tools out there, one is like Phraseapp. The way these tools work are fairly simply, and the localisation is mostly front end driven, not backend.
You define a key for some content, and the key could accept parameters as well. So in your example, it could look the following.
English: user_commented_message = %# commented %# on your post.
German: user_commented_message = %# Kommentierte %# auf Ihrem Beitrag.
Than you save this into your Localizable.strings, what is being shipped with your app binary.
Once the localisation is done, all you need to do is to create the right payload for your notification. In your example, it would be looking something like the following:
{
aps = {
alert = {
"loc-args" = (
"John",
"hi"
);
"loc-key" = "user_commented_message";
};
badge = 1;
sound = default;
};
}
As you can see, you pass 2 arguments in the loc-args sections, where you pretty much define, what should be display instead of %# in your key. The order of the arguments is important, they will replace the %# in the same order, as they are in loc-args sections.
In the loc-key section, you just pretty much define what key should be used from your Localizable.strings. From this on, the Notification Center will just display the notification based on the device language settings reading the content the localisation dictionary, the Twitter or Facebook server has nothing to do with it.
For further reading, i would recommend to take a look at the Apple notification programming guide.
EDIT
This post says you are not able to use body and loc-args at the same time. It is an exclusive EITHER ONE or the OTHER relationship.
If it is in your app, than simply just use localisation. Do not send hard coded language content or store hard coded language text on the server, always use every text content with the localisation keys. It is still front end driven, the server should only keep the localised keys, not the content itself.

Related

How to find content type of qr code in ios in swift?

I am developing an iOS application where I am scanning QRCode and showing result to user. I am scanning QRCode by native libraries provided by Apple. I am able to get string/content from QRCode but I am unable to distinguish and parse string/content. I want to get the type of data (distinguish data type) present in QRCode as it may have different types of data, for example url, text, mcard, email data. I also want to parse content i.e. getting fields of data, for example in case of email data, I want to get email, cc, subject and body field. This question How are different content types within QR-Codes distinguished? did not helped because it does not have accepted answer which can help me.
The .stringValue obtained from a AVMetadataObject scan is just that - a "string value." It is up to you to parse the content.
There are a number of predefined data types, such as
a URL, where the string starts with "https://";
an email link, e.g. "mailto:somebody#example.com?subject=Mail%20from%20Our%20Site"
There is also a "VCard" spec, which may be formatted with "field" values:
BEGIN:VCARD
VERSION:3.0
N:Owen;Sean;;;
FN:Sean Owen
TITLE:Software Engineer
EMAIL;TYPE=INTERNET;TYPE=WORK;TYPE=PREF:srowen#google.com
URL;TYPE=Homepage:https://example.com
END:VCARD
for example.
So, you check the start of the string. If it begins with "https://" your app might launch Safari. If it begins with "mailto:" you could launch the mail app. If it begins with "BEGIN:VCARD" then you'd parse the predefined fields and use the data in your app.
There is a good summary of predefined types here (although, it may be a bit out-of-date): https://github.com/zxing/zxing/wiki/Barcode-Contents
Of course, you can create your own types for your own purposes. For example, you can create a QR-Code that contains:
myTypeA:Robert
or:
myTypeB:Driver
then your app would get the string, take one action if it begins with myTypeA: and a different action if it begins with myTypeB:.

Take Action based on QR code Information of VCARD, iPhone

What is the best way to handle QR code's Information, As QR code can have any information, For now, I want to handle only
1 URL and redirect to safari browser--> its fine
2 vCard and open contact book with contact values, But I'm seeing that VCARDS keys name are not unique(not sure if I'm Having improper QR codes). And also, QR value is a string to how to detect which value is for which key of address book?
e.g:
"BEGIN:VCARD
FN:Ashwin kanjariya
TEL:+999-999-9999
EMAIL:you#we.com
URL:http://www.youandme.com
N:kanjariya;ashwin
ADR:any address
ROLE:software developer
VERSION:3.0
END:VCARD"
So, I'm Not sure for VCARD all keys are universal or not? What is the best way to handle it?
I appreciate your any kind of suggest that can help me to figure out VCARD parsing.
Is CFDataCreate with ABPersonCreatePeopleInSourceWithVCardRepresentation best way to go with?(I have support for below IOS 9 as well)
like
let vCardnsdata = CFDataCreate(nil, UnsafePointer<UInt8>(vCard.bytes), vCard.length)
let addressbookDefaultSrc = ABAddressBookCopyDefaultSource(addressBook)
let vCardPeople = ABPersonCreatePeopleInSourceWithVCardRepresentation(defaultSource.takeUnretainedValue(), vCardData).takeRetainedValue() as NSArray
VCARD has several versions with slightly different implementations, keys don't have to be unique, a person can have multiple home or work phone numbers for example, but you should be able to tell what is a phone number and just accept as many as your customer believes is reasonable for their use case.
An extensive list of what you may find in VCARD's is here: https://en.wikipedia.org/wiki/VCard
If you want to make sure that all the data is stored, then you may have to implement lists, or in database terms, store items in different tables so that one to many relationships can be maintained for several items.
When designing a system to store information about people, you may also want to observe some of the Falsehoods Programmers Believe About Names

How do I localize static UIApplicationShortcutItems?

With the iPhone 6s, Apple has introduced a new feature called "3D Touch". App developers are able to use this technology by using it within their apps or provide so-called UIApplicationShortcutItems on the home screen which appear when you 3D Touch the corresponding app icon. I've seen quite a few people out there who wanted to know how you would be able to localize those. Here's how.
What you have to do is, if you haven't already, create a new strings file called InfoPlist.strings, then you localize this strings file to the languages you wish via the File Inspector on the right.
Now, you write down a key (for example: ADD_ITEM_SHORTCUT_TITLE or ADD_ITEM_SHORTCUT_DESCRIPTION) and the correct translation for each localized file. For example:
English file:
ADD_ITEM_SHORTCUT_TITLE = "Add";
ADD_ITEM_SHORTCUT_DESCRIPTION = "a new item";
German file:
ADD_ITEM_SHORTCUT_TITLE = "Füge hinzu";
ADD_ITEM_SHORTCUT_DESCRIPTION = "ein neues Item";
Then, go to your Info.plist and enter your key to the corresponding field. For example:
That way, you get localized UIApplicationShortcutItems. Now, they look like this:
Phone language English:
Phone language German:

Suitable data structure to save localized settings

Here's the scenario. I have a set of settings in an app. For example consider my app as a video player. So there are settings like allow full screen, display subtitles etc. All these settings have boolean values since you either turn on or off them.
These settings should display inside the app in a table view. And if any of them are activated or when the user taps on them to activate/deactivate, you show it by setting the checkmark accessory view of that cell.
Since I need the settings to be displayed this way and only within the app, I cannot simply use Settings bundles. There's also another catch. I need these settings to be localized.
What I initially thought was to have separate plists for the languages I support.
Settings_en.plist (English)
Settings_sv.plist (Swedish)
Then fetch the plist name depending on the system language and display its values.
let filePath = NSBundle.mainBundle().bundlePath.stringByAppendingPathComponent(NSLocalizedString("SETTINGS_PLIST", comment: ""))
But this is not ideal because say I'm running in Swedish and I change the Subtitles setting to on. Now i have to update this in both plists. This will quickly become even messier if I add more languages in the future.
Is there a better way to store settings which is easier to save and fetch and also supports localization?
I was able to find an answer elsewhere. Here are the steps taken to resolve this issue.
Instead of multiple plists, create one plist and have the keys in English language.
Then have the localized strings in your string files with the same English keys.
Localizable.strings (English)
FULL_SCREEN = "Full Screen";
SUBTITLES = "Subtitles";
Localizable.strings (Swedish)
FULL_SCREEN = "Helskärm";
SUBTITLES = "Undertexter";
In the code when you're displaying the values in a table view, refer to them by that key.
let setting = settings[indexPath.row] as [String: Bool]
let title = setting.keys.first
cell.textLabel?.text = NSLocalizedString(title!, comment: "")

how to send rich push to APNS

I can send a regular push just fine but cannot figure out to send a "rich" push. I've read Apples "Local and Push Notification Programming Guide" 5 times and it makes no mention of sending anything other than an alert, badge and sound. I cannot find any documentation for rich push in the developer center.
I must be looking for the wrong thing, maybe someone else has been down this road already?
You won't send a whole HTML content directly in your notification, but you can send "rich push" in the sense that you can send additional data in your Push Notifications, and not just alert, badge and sound: you can send any other key you want/need. (just put them outside of the aps entry of your JSON, like at the root of the JSON object).
Only limitation is that you are limited to 255 characters, so that's not designed to provide sthg like a large HTML file for example. Just to provide some additional keys of your choice, like for example an URL, some fields to describe internal ids of objects you want to display, etc.
Everything is described here in the Local and Push Notifications Programming Guide: look at the paragraph called "Examples of JSON Payloads", where they add dummy parameters like "acme1" and "acme2".
So applied to your case your could have a payload like that for your Push notification:
{
"aps" : { "alert" : "Message received from Bob" },
"rich-text-url" : "http://yourserver/message/1234"
"id-of-object-to-show" : 456
}
The only thing to keep in mind is the 255 characters limit.
(Note: obviously the descriptive keys in my example above are only for explanation but one generally choose shorter key names like url and id instead to shorten the overall payload)

Resources