mirror of
https://github.com/iSoron/uhabits.git
synced 2025-12-06 09:08:52 -06:00
Change language automatically
This commit is contained in:
@@ -25,6 +25,7 @@ import UIKit
|
||||
var backend = Backend(databaseName: "dev.db",
|
||||
databaseOpener: IosDatabaseOpener(withLog: StandardLog()),
|
||||
fileOpener: IosFileOpener(),
|
||||
localeHelper: IosLocaleHelper(NSLocale.preferredLanguages),
|
||||
log: StandardLog(),
|
||||
taskRunner: SequentialTaskRunner())
|
||||
|
||||
|
||||
@@ -86,11 +86,13 @@ class MainScreenCell : UITableViewCell {
|
||||
}
|
||||
|
||||
class MainScreenController: UITableViewController, MainScreenDataSourceListener {
|
||||
|
||||
var backend: Backend
|
||||
var dataSource: MainScreenDataSource
|
||||
var data: MainScreenDataSource.Data?
|
||||
var theme: Theme
|
||||
var nButtons = 3
|
||||
var strings: Strings
|
||||
|
||||
required init?(coder aDecoder: NSCoder) {
|
||||
fatalError()
|
||||
@@ -98,6 +100,7 @@ class MainScreenController: UITableViewController, MainScreenDataSourceListener
|
||||
|
||||
init(withBackend backend:Backend) {
|
||||
self.backend = backend
|
||||
self.strings = backend.strings
|
||||
self.dataSource = backend.mainScreenDataSource
|
||||
self.theme = backend.theme
|
||||
super.init(nibName: nil, bundle: nil)
|
||||
@@ -110,7 +113,7 @@ class MainScreenController: UITableViewController, MainScreenDataSourceListener
|
||||
}
|
||||
|
||||
override func viewDidLoad() {
|
||||
self.title = "Habits"
|
||||
self.title = strings.main_activity_title
|
||||
|
||||
self.navigationItem.rightBarButtonItems = [
|
||||
UIBarButtonItem(image: UIImage(named: "ic_more"),
|
||||
@@ -176,27 +179,27 @@ class MainScreenController: UITableViewController, MainScreenDataSourceListener
|
||||
|
||||
@objc func onMoreActionsClicked() {
|
||||
let alert = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
|
||||
alert.addAction(UIAlertAction(title: "Show archived", style: .default) {
|
||||
alert.addAction(UIAlertAction(title: strings.show_archived, style: .default) {
|
||||
(action: UIAlertAction) -> Void in
|
||||
// TODO
|
||||
})
|
||||
alert.addAction(UIAlertAction(title: "Hide completed", style: .default) {
|
||||
alert.addAction(UIAlertAction(title: strings.hide_completed, style: .default) {
|
||||
(action: UIAlertAction) -> Void in
|
||||
// TODO
|
||||
})
|
||||
alert.addAction(UIAlertAction(title: "Night mode", style: .default) {
|
||||
alert.addAction(UIAlertAction(title: strings.night_mode, style: .default) {
|
||||
(action: UIAlertAction) -> Void in
|
||||
// TODO
|
||||
})
|
||||
alert.addAction(UIAlertAction(title: "Help & FAQ", style: .default) {
|
||||
alert.addAction(UIAlertAction(title: strings.help, style: .default) {
|
||||
(action: UIAlertAction) -> Void in
|
||||
// TODO
|
||||
})
|
||||
alert.addAction(UIAlertAction(title: "About", style: .default) {
|
||||
alert.addAction(UIAlertAction(title: strings.about, style: .default) {
|
||||
(action: UIAlertAction) -> Void in
|
||||
// TODO
|
||||
})
|
||||
alert.addAction(UIAlertAction(title: "Cancel", style: .cancel) {
|
||||
alert.addAction(UIAlertAction(title: strings.cancel, style: .cancel) {
|
||||
(action: UIAlertAction) -> Void in
|
||||
// Do nothing
|
||||
})
|
||||
|
||||
@@ -44,6 +44,10 @@ extension Date {
|
||||
class IosLocalDateFormatter : NSObject, LocalDateFormatter {
|
||||
let fmt = DateFormatter()
|
||||
|
||||
override init() {
|
||||
fmt.locale = Locale(identifier: NSLocale.preferredLanguages.first!)
|
||||
}
|
||||
|
||||
func shortMonthName(date: LocalDate) -> String {
|
||||
fmt.dateFormat = "MMM"
|
||||
return fmt.string(from: date.iosDate)
|
||||
|
||||
64
ios/Application/Platform/IosLocale.swift
Normal file
64
ios/Application/Platform/IosLocale.swift
Normal file
@@ -0,0 +1,64 @@
|
||||
/*
|
||||
* Copyright (C) 2016-2019 Álinson Santos Xavier <isoron@gmail.com>
|
||||
*
|
||||
* This file is part of Loop Habit Tracker.
|
||||
*
|
||||
* Loop Habit Tracker is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by the
|
||||
* Free Software Foundation, either version 3 of the License, or (at your
|
||||
* option) any later version.
|
||||
*
|
||||
* Loop Habit Tracker is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||
* more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
import Foundation
|
||||
|
||||
class IosLocaleHelper : NSObject, LocaleHelper {
|
||||
var preferredLanguages: [String]
|
||||
|
||||
init(_ preferredLanguages: [String]) {
|
||||
self.preferredLanguages = preferredLanguages
|
||||
}
|
||||
|
||||
func getStringsForCurrentLocale() -> Strings {
|
||||
let lang = preferredLanguages.first ?? "en-US"
|
||||
StandardLog().debug(tag: "IosLocale", msg: lang)
|
||||
if lang.hasPrefix("ar") { return StringsArabic() }
|
||||
if lang.hasPrefix("fr") { return StringsFrench() }
|
||||
if lang.hasPrefix("es") { return StringsSpanish() }
|
||||
if lang.hasPrefix("pt-BR") { return StringsPortugueseBR() }
|
||||
if lang.hasPrefix("pt") { return StringsPortuguesePT() }
|
||||
if lang.hasPrefix("it") { return StringsItalian() }
|
||||
if lang.hasPrefix("de") { return StringsGerman() }
|
||||
if lang.hasPrefix("zh-Hans") { return StringsChineseCN() }
|
||||
if lang.hasPrefix("zh-Hant") { return StringsChineseTW() }
|
||||
if lang.hasPrefix("nl") { return StringsDutch() }
|
||||
if lang.hasPrefix("ja") { return StringsJapanese() }
|
||||
if lang.hasPrefix("ko") { return StringsKorean() }
|
||||
if lang.hasPrefix("vi") { return StringsVietnamese() }
|
||||
if lang.hasPrefix("ru") { return StringsRussian() }
|
||||
if lang.hasPrefix("sv") { return StringsSwedish() }
|
||||
if lang.hasPrefix("da") { return StringsDanish() }
|
||||
if lang.hasPrefix("fi") { return StringsFinnish() }
|
||||
if lang.hasPrefix("nb") { return StringsNorwegian() }
|
||||
if lang.hasPrefix("tr") { return StringsTurkish() }
|
||||
if lang.hasPrefix("el") { return StringsGreek() }
|
||||
if lang.hasPrefix("id") { return StringsIndonesian() }
|
||||
if lang.hasPrefix("hi") { return StringsHindi() }
|
||||
if lang.hasPrefix("hu") { return StringsHungarian() }
|
||||
if lang.hasPrefix("pl") { return StringsPolish() }
|
||||
if lang.hasPrefix("cs") { return StringsCzech() }
|
||||
if lang.hasPrefix("sk") { return StringsSlovak() }
|
||||
if lang.hasPrefix("ca") { return StringsCatalan() }
|
||||
if lang.hasPrefix("ro") { return StringsRomanian() }
|
||||
if lang.hasPrefix("hr") { return StringsCroatian() }
|
||||
if lang.hasPrefix("he") { return StringsHebrew() }
|
||||
return Strings()
|
||||
}
|
||||
}
|
||||
27
ios/Tests/Platform/IosLocaleTest.swift
Normal file
27
ios/Tests/Platform/IosLocaleTest.swift
Normal file
@@ -0,0 +1,27 @@
|
||||
/*
|
||||
* Copyright (C) 2016-2019 Álinson Santos Xavier <isoron@gmail.com>
|
||||
*
|
||||
* This file is part of Loop Habit Tracker.
|
||||
*
|
||||
* Loop Habit Tracker is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by the
|
||||
* Free Software Foundation, either version 3 of the License, or (at your
|
||||
* option) any later version.
|
||||
*
|
||||
* Loop Habit Tracker is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||
* more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
import XCTest
|
||||
@testable import uhabits
|
||||
|
||||
class IosLocaleTest: XCTestCase {
|
||||
func testGetStringForLocale() {
|
||||
//let helper = IosLocaleHelper()
|
||||
}
|
||||
}
|
||||
@@ -8,6 +8,8 @@
|
||||
|
||||
/* Begin PBXBuildFile section */
|
||||
0057EC2B224C4CDB00C49288 /* icons in Resources */ = {isa = PBXBuildFile; fileRef = 0057EC2A224C4CDB00C49288 /* icons */; };
|
||||
006EFE4C2252E9F3008464E0 /* IosLocaleTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 006EFE4A2252E9D3008464E0 /* IosLocaleTest.swift */; };
|
||||
006EFE4E2252EA2B008464E0 /* IosLocale.swift in Sources */ = {isa = PBXBuildFile; fileRef = 006EFE4D2252EA2B008464E0 /* IosLocale.swift */; };
|
||||
00A5B42822009F590024E00C /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 00A5B42722009F590024E00C /* AppDelegate.swift */; };
|
||||
00A5B42A22009F590024E00C /* MainScreenController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 00A5B42922009F590024E00C /* MainScreenController.swift */; };
|
||||
00A5B42F22009F5A0024E00C /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 00A5B42E22009F5A0024E00C /* Assets.xcassets */; };
|
||||
@@ -57,6 +59,8 @@
|
||||
|
||||
/* Begin PBXFileReference section */
|
||||
0057EC2A224C4CDB00C49288 /* icons */ = {isa = PBXFileReference; lastKnownFileType = folder; path = icons; sourceTree = "<group>"; };
|
||||
006EFE4A2252E9D3008464E0 /* IosLocaleTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = IosLocaleTest.swift; sourceTree = "<group>"; };
|
||||
006EFE4D2252EA2B008464E0 /* IosLocale.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = IosLocale.swift; sourceTree = "<group>"; };
|
||||
00A5B42422009F590024E00C /* uhabits.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = uhabits.app; sourceTree = BUILT_PRODUCTS_DIR; };
|
||||
00A5B42722009F590024E00C /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
|
||||
00A5B42922009F590024E00C /* MainScreenController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MainScreenController.swift; sourceTree = "<group>"; };
|
||||
@@ -184,6 +188,7 @@
|
||||
00C0C6DA2247E6B0003D8AF0 /* IosDates.swift */,
|
||||
00C0C6CD2246EFB3003D8AF0 /* IosExtensions.swift */,
|
||||
00C0C6A9224654F4003D8AF0 /* IosFiles.swift */,
|
||||
006EFE4D2252EA2B008464E0 /* IosLocale.swift */,
|
||||
);
|
||||
path = Platform;
|
||||
sourceTree = "<group>";
|
||||
@@ -195,6 +200,7 @@
|
||||
00C0C6A222465365003D8AF0 /* IosDatabaseTest.swift */,
|
||||
00C0C6DC2247E6C4003D8AF0 /* IosDatesTest.swift */,
|
||||
00C0C6A122465365003D8AF0 /* IosFilesTest.swift */,
|
||||
006EFE4A2252E9D3008464E0 /* IosLocaleTest.swift */,
|
||||
);
|
||||
path = Platform;
|
||||
sourceTree = "<group>";
|
||||
@@ -331,6 +337,7 @@
|
||||
00C0C6CE2246EFB3003D8AF0 /* IosExtensions.swift in Sources */,
|
||||
00C0C6E0224A3602003D8AF0 /* DetailScreenController.swift in Sources */,
|
||||
00C0C6A8224654A2003D8AF0 /* IosDatabase.swift in Sources */,
|
||||
006EFE4E2252EA2B008464E0 /* IosLocale.swift in Sources */,
|
||||
00C0C6DB2247E6B0003D8AF0 /* IosDates.swift in Sources */,
|
||||
00A5B42A22009F590024E00C /* MainScreenController.swift in Sources */,
|
||||
00A5B42822009F590024E00C /* AppDelegate.swift in Sources */,
|
||||
@@ -344,6 +351,7 @@
|
||||
files = (
|
||||
00C0C6DD2247E6C4003D8AF0 /* IosDatesTest.swift in Sources */,
|
||||
00C0C6A52246537A003D8AF0 /* IosFilesTest.swift in Sources */,
|
||||
006EFE4C2252E9F3008464E0 /* IosLocaleTest.swift in Sources */,
|
||||
00C0C6D92247DC13003D8AF0 /* IosCanvasTest.swift in Sources */,
|
||||
00C0C6A62246537E003D8AF0 /* IosDatabaseTest.swift in Sources */,
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user