I need to switch Locate in my app between Arabic and English.
I have the following code to switch locale:
if (Locale.getDefault() == Locale.get(Locale.LOCALE_ar, null)) {
Locale.setDefault(Locale.get(Locale.LOCALE_en, null));
} else {
Locale.setDefault(Locale.get(Locale.LOCALE_ar, null));
}
And in my app I have the following resource files:
appName.rrh
appName.rrc
appName_ar.rrc
appName_en.rrc
And I have a button which uses a localized string as follows:
subscribeButton = new ButtonField(res.getString(LANG), ButtonField.CONSUME_CLICK);
My problem is when the locale is changed to Arabic, the UI flips (Arabic is right to left), and switching it again to English flips it again, but all without the text in the button changing. Please guide me on what I'm doing wrong.
Its because, you have created the button field with the text which was relevant for that locale. Once the locale changes, you will have to re set the buttonField text as
subscribeButton.setLabel(res.getString(LANG));
Related
I've built an application and now it's time to translate it to different languages. But some labels that I think I have localised don't get displayed in different languages with the different values.
Image one: So first, I've added the Swedish language inside
"Project -> Localizations"
Than I went inside the "LaunchScreen.storyboard" and edited the file.
This one:
Edited to this:
After that I made sure my launch screen supports the language like this:
And for the last step.. I edited the application language to Swedish like this:
But when I run it on the simulator, the text is not getting translated. It still remains the same text, and I've no idea how to solve it. Do you? Do not hesitate to help me out, I would really appreciate it!
So, I'm not sure if I've to add some code or whatever to make this work, but I followed a guide, and they don't. That's one of the reasons why I'm so confused. And thanks in advance!
BEFORE DISLIKE: At least give me a reason for it, so I can improve it in the future!
Since storyboards elements ids changes and they are not managable to be used as "keys" in your localized "key-value" pair one better solution when localizing storyboards (UI) elements could be to provide a custom User Define Attribute defined as a Locale Key to be used for that UIView.
Then you can define in your Storyboard Attribute Inspector for a specific UI View an input field that will be filled with the localized key defined in your Localizable.strings files (one for English one will be for Swedish, both should have the same keys but with different values - in the English one will have english translations in values, and in Swedish one the opposite.
1) For example since you want to localize a UI View UILabel than you can have this in a swift file Localizable.swift for example (the code makes possible:
import UIKit
// MARK: Localizable
public protocol Localizable {
var localized: String { get }
}
extension String: Localizable {
public var localized: String {
return NSLocalizedString(self, comment: "")
}
}
// MARK: XIBLocalizable
public protocol XIBLocalizable {
var localeKey: String? { get set }
}
extension UILabel: XIBLocalizable {
#IBInspectable public var localeKey: String? {
get { return nil }
set(key) {
text = key?.localized
}
}
}
2) You can uncheck your storyboard translations so you will endup having 1 storyboard without duplicating the storyboard into multiple translated version.
3) You can create now a Localizable.strings file containing your keys and translations and localize this file in order to have your translations (as you did with the storyboard, but instead of localizing the storyboard into multiple translated version, you will localize the Localizable.strings file (so you will see two files after you localize that in both languages, one will be called Localizable.strings (English) and the other Localizable.strings (Swedish).
Your Localizable.strings (English) could look like this for example:
"main_page_title_label" = "Main Page Title in English";
Your Localizable.strings (Swedish) could look like this for example:
"main_page_title_label" = "Huvudsida Titel på engelska";
4) Now go to your LaunchScreen.storyboard which you should have un-localized so you only have one version of it and not two like in your example pictures (English, Swedish). Look for the UILabel you want to localize and under the Attribute Inspector you will see a new input field called Locale Key here you can put as a value main_page_title_label. And now you have just localized a UILabel in your storyboard.
5) To test it from the simulator you have to change the language in Edit Scheme > Run > Options > Application Language and after you save it you can now run the app in the simulator and it will simulate that your simulator OS system will be set to Swedish language, so the label set with that key will show the Swedish value for that key.
Supporting more UI Views (UIButton, UITextField, UISearchBar..)
If you want to be able to localize more UI View, and not just UI View of type UILabel than you can add more support to the Localizable.swift file.
If you want to be able to localize also a UI View of type Button you can add this to Localizable.swift:
extension UIButton: XIBLocalizable {
#IBInspectable public var localeKey: String? {
get { return nil }
set(key) {
setTitle(key?.localized, for: .normal)
}
}
}
To support localization in storyboards for UITextField and UISearchBar placeholder text add this to your Localizable.swift:
// MARK: Special protocol to localizaze UI's placeholder
public protocol UIPlaceholderXIBLocalizable {
var localePlaceholderKey: String? { get set }
}
extension UITextField: UIPlaceholderXIBLocalizable {
#IBInspectable public var localePlaceholderKey: String? {
get { return nil }
set(key) {
placeholder = key?.localized
}
}
}
extension UISearchBar: UIPlaceholderXIBLocalizable {
#IBInspectable public var localePlaceholderKey: String? {
get { return nil }
set(key) {
placeholder = key?.localized
}
}
}
I like denis_lor's solution, but I thought that I first want to get working what should be working already (translation of the storyboard with a simple strings file) without having to re-implement that functionality in a similar way.
My problem was similar to what is described in the question. Translations were already working to a point, but manually added and some changed translations didn't get translated at all.
The solution I found was recreating the strings file using ibtool:
ibtool AppName/Base.lproj/Main.storyboard --generate-strings-file tmp.strings
After backing up my current (just partly working) Main.strings file (AppName/de.lproj/Main.strings) I overwrote it with the newly generated tmp.strings
mv tmp.strings AppName/de.lproj/Main.strings
And then I manually changed the values of that file to the backed up values.
Now everything gets translated correctly again. I think the problem could have had something to do with encoding problems, because that strings files are UTF16 encoded.
According to Apple docs, the LaunchScreen.storyboard is static and shouldn't have texts because these texts will not be localized.
Avoid including text on your launch screen. Because launch screens are static, any displayed text won’t be localized.
source: https://developer.apple.com/design/human-interface-guidelines/ios/icons-and-images/launch-screen/
I'm supper new here, either Javascript and JXA, so pardon me if I make some stupid questions. But I'm trying to figure out a way to get the string from the highlighted text using JXA - JavaScript for Automation, for Javascript can be recognized in Automator since Yosemite, I thought I can make something work with these:
window.getSelection in:
function getSelectedText() {
if (window.getSelection) {
txt = window.getSelection();
} else if (window.document.getSelection) {
txt =window.document.getSelection();
} else if (window.document.selection) {
txt = window.document.selection.createRange().text;
}
return txt;
}
This code is not mine, somebody posted this. But I've found out that I can't use window or document here in Automator to make change to Mac OS, so can someone show me how to convert this Javascript code into JXA which Automator can understand?
Thanks a lot!
In general, you can use the System Events app to copy and paste with any app.
'use strict';
//--- GET A REF TO CURRENT APP WITH STD ADDITONS ---
var app = Application.currentApplication()
app.includeStandardAdditions = true
var seApp = Application('System Events')
//--- Set the Clipboard so we can test for no selection ---
app.setTheClipboardTo("[NONE]")
//--- Activate the App to COPY the Selection ---
var safariApp = Application("Safari")
safariApp.activate()
delay(0.2) // adjust the delay as needed
//--- Issue the COPY Command ---
seApp.keystroke('c', { using: 'command down' }) // Press ⌘C
delay(0.2) // adjust the delay as needed
//--- Get the Text on the Clipboard ---
var clipStr = app.theClipboard()
console.log(clipStr)
//--- Display Alert if NO Selection was Made ---
if (clipStr === "[NONE]") {
var msgStr = "NO Selection was made"
console.log(msgStr)
app.activate()
app.displayAlert(msgStr)
}
For more info see:
Sending Keystrokes in JXA
JXA Resources
You need to mix JXA and Safari’s javaScript…
var Safari = Application("Safari") // get Safari
selection = Safari.doJavaScript("document.getSelection().toString()",{
in: Safari.windows[0].tabs[0] // assume frontmost window and tab
})
The script is in JXA, but the document.getSelection().toString() is Safari’s javaScript.
Of course you will need to enable apple events in Safari… http://osxdaily.com/2011/11/03/enable-the-develop-menu-in-safari/
If you want the selected text from another application, the code might be very different.
Don't do that, it's only applicable to JavaScript embedded inside a web browser. JXA is a standalone JS interpreter that has absolutely no understanding of web pages or DOM (and frankly doesn't have much clue about Mac application scripting either, btw).
Instead, use Automator to create an OS X Service as services can manipulate selected text in almost any OS X app; no application scripting required.
i'm working in Multilingual grails application (English and arbaic) , i want when the user chooses Arabic language the view's labels will be on the right side of the page and in English on the left side , how this can be achieved ?
thanks
You can use internationalization in grails through messages.properties file, you can define message signature in files and and they can be accessed through ?lang=es on the URL, you may need to have two files one for english and another for Arabic.
for example define in the messages.properties:
vendor.link.dashboardLink = Vendor Dashboard
and on the GSP page you can access it like:
<g:message code="vendor.link.dashboardLink" />
you can find more about internalization at grails doc have a look at http://grails.org/doc/2.2.1/guide/i18n.html
If the views have differences beyond simple string substitution, I would recommend using a different set of views based on locale:
Example controller code:
import org.springframework.web.servlet.support.RequestContextUtils as RCU
class ExampleController {
final static String englishLanguageCode = new Locale('en').getLanguage()
final static String arabicLanguageCode = new Locale('ar').getLanguage()
def differentViews() {
def currentLocale = RCU.getLocale(request)
switch(currentLocale.language) {
case englishLanguageCode:
render view: 'englishView'
break
case arabicLanguageCode:
render view: 'arabicView'
break
default:
// pick a default view or error page, etc.
}
}
}
I have a web application that uses localization to show either English or French for our French Canadian Customers.
It is working just fine based on the users regional settings.
We have a need however to allow the user to switch back to English if their regional settings are set to French.
Is it possible to override the users regional setting if he so desires? if so...how would I code this? (for example having a link on the layout page that says English, clicking this would then change it back to English or back to French)
Also, I am using resource files to save the text strings and using the same set of views.
Somewhere in your code after they click a button to select a language:
Session["customLocalization"] = "de-DE"; //Or whatever language
In your Global.asax
protected void Application_BeginRequest(object sender, EventArgs e)
{
String sessionOverrideLocale;
if (HttpContext.Current != null && HttpContext.Current.Session != null)
{
sessionOverrideLocal = (String) HttpContext.Current.Session["customLocalization"];
}
if (sessionOverrideLocale != null)
{
Thread.CurrentThread.CurrentUICulture = new CultureInfo(sessionOverrideLocale);
Thread.CurrentThread.CurrentCulture = new CultureInfo(sessionOverrideLocale);
}
}
Yes, it is possible but I do not currently have access to the code I most recently worked on where we allowed the user to change their current rendered region.
Here is a blog post that goes into great detail regarding doing this. He has an older post that I believe uses MVC3 and then this newer one is written from the perspective of MVC4, so this should have you covered.
I hope it helps:
http://geekswithblogs.net/shaunxu/archive/2012/09/04/localization-in-asp.net-mvc-ndash-upgraded.aspx
I'm using AIR 2.0 (soon will be updating to 3.3 with Flash CS6) to create an iPad app. We have textfields (Classic, dynamic) which sometimes contain one or multiple htmlText links which need to be clickable. In the desktop version of the program, all text is selectable and the links are easily accessed. My problem is that it takes me mashing the link like 20 times on the iPad before it will recognize that there's a link and navigate to it in Safari. The other strange thing is that none of the text appears to be selectable - I can't get the iPad cursor, copy/paste menu, etc. to show up.
I think, from reading other threads, that the hit area for the URL is only the stroke on the text itself... if that's true, what can I do to increase the hit area? Or make text selectable? It was suggested elsewhere to put movieclips behind the URLs but that's not really possible as this is all dynamic text from XML files.
I've read about StageText but I gather this is only used for input fields, which is not the case here.
I'm reasonably advanced in AS3 but I'd prefer an easy solution over re-writing large chunks of code. At the moment the only thing I can think to do is get the URL and make it so that as soon as you touch anywhere on the textfield, it navigates to the link. But this would break down if there were more than 1 URL in a given textfield.
Any ideas?
I had this exact same issue, and it's had me flummoxed for a while.
Here's what I did to get the desired behaviour:
1) Instead of using a listener for TextEvent.LINK, listen for MouseEvent.CLICK (or TouchEvent.TAP) on the TextField.
eg.
var tf:TextField = new TextField();
tf.addEventListener(MouseEvent.CLICK, linkClicked);
2) In the linkClicked() handler, you use getCharIndexAtPoint() to determine the index of the character that was clicked, and then from that determine the URL from the TextFormat of the character. This is adapted from a post by Colin Holgate on the Adobe Forums (http://forums.adobe.com/thread/231754)
public function linkClicked(e:MouseEvent):void {
var idx:int = e.target.getCharIndexAtPoint(e.localX, e.localY);
trace("Tapped:",idx);
var tf:TextFormat = e.target.getTextFormat(idx);
if(tf.url != "" && tf.url != null) {
var linkURL:String = tf.url;
trace(linkURL);
// Hyperlink processing code here
dispatchEvent(new UIEvent(UIEvent.LINK_TAPPED,tf.url));
}
}
3) The last line (dispatchEvent()) is sending a custom event to another function to process the link, but you could easily inline your code here.
I've tested on an iPad 3 running iOS6.1, building with AIR3.5. Links are much more responsive, and I don't find myself mashing the screen trying to hit the stroke of the text!