<commit_before>//
//  StringLocalization.swift
//  SelfTaping
//
//  Created by <NAME> on 2018-07-11.
//  Copyright © 2018 Casting Workbook Services Inc. All rights reserved.
//

import Foundation

extension String {
    // NSLocalizedString shorthand
    // show erros if localization is missing
    // show erros if localization is missing
    var localized: String {
        //TODO: record all unresolved keys
        var localizedString = NSLocalizedString(self, comment: "")
        if localizedString == self {
            localizedString = NSLocalizedStringFallback(self, comment: "")
            if LogOptions.localization {
                if localizedString == self {
                    print("missing localization: \(self)")
                }else{
                    print("missing localization for \(String(describing: Locale.current.languageCode)): \(self)")
                }
            }
        }
        if let (en, fr) = AllLocalizations(self){
            var _en = en.replacingOccurrences(of: "\n", with: "\\n", options: .literal, range: nil)
            var _fr = fr.replacingOccurrences(of: "\n", with: "\\n", options: .literal, range: nil)
            if _en.contains(","){
               _en = "\"\(_en)\""
            }
            if _fr.contains(","){
                _fr = "\"\(_fr)\""
            }
            let loc_area = self.split(separator: ".").first ?? ""
            LocalizationManager.shared.allLocalizationKeys[self] = "\(self),\(loc_area),\(_en),\(_fr)"
        }
        LocalizationManager.shared.addLocalization(key: self, string: localizedString)
        return localizedString
    }
    
    public func NSLocalizedStringFallback(_ key: String, tableName: String? = nil, bundle: Bundle = Bundle.main, value: String = "", comment: String) -> String {
        let fallbackLanguage = "Base"
        guard let fallbackBundlePath = Bundle.main.path(forResource: fallbackLanguage, ofType: "lproj") else { return key }
        guard let fallbackBundle = Bundle(path: fallbackBundlePath) else { return key }
        let fallbackString = fallbackBundle.localizedString(forKey: key, value: comment, table: nil)
        return Bundle.main.localizedString(forKey: key, value: fallbackString, table: nil)
    }
    
    public func AllLocalizations(_ key: String) -> (String, String)? {
        guard let baseBundlePath = Bundle.main.path(forResource: "Base", ofType: "lproj") else { return nil }
        guard let frBundlePath = Bundle.main.path(forResource: "fr", ofType: "lproj") else { return nil }
        
        guard let baseBundle = Bundle(path: baseBundlePath) else { return nil }
        guard let frBundle = Bundle(path: frBundlePath) else { return nil }

        let baseString = baseBundle.localizedString(forKey: key, value: nil, table: nil)
        let frString = frBundle.localizedString(forKey: baseString, value: baseString, table: nil)
        return (baseString, frString)
    }

}<commit_msg>no message<commit_after>//
//  StringLocalization.swift
//  SelfTaping
//
//  Created by <NAME> on 2018-07-11.
//  Copyright © 2018 Casting Workbook Services Inc. All rights reserved.
//

import Foundation

extension String {
    // NSLocalizedString shorthand
    // show erros if localization is missing
    // show erros if localization is missing
    
    func localized(value: String)-> String {
        //TODO: record all unresolved keys
        var localizedString = NSLocalizedString(self, comment: "")
        if localizedString == self {
            localizedString = NSLocalizedStringFallback(self, comment: "")
            if LogOptions.localization {
                if localizedString == self {
                    print("missing localization: \(self)")
                }else{
                    print("missing localization for \(String(describing: Locale.current.languageCode)): \(self)")
                    localizedString = value
                }
            }
        }
//        if let (en, fr) = AllLocalizations(self){
//            var _en = en.replacingOccurrences(of: "\n", with: "\\n", options: .literal, range: nil)
//            var _fr = fr.replacingOccurrences(of: "\n", with: "\\n", options: .literal, range: nil)
//            if _en.contains(","){
//                _en = "\"\(_en)\""
//            }
//            if _fr.contains(","){
//                _fr = "\"\(_fr)\""
//            }
//            let loc_area = self.split(separator: ".").first ?? ""
//            LocalizationManager.shared.allLocalizationKeys[self] = "\(self),\(loc_area),\(_en),\(_fr)"
//        }
//        LocalizationManager.shared.addLocalization(key: self, string: localizedString)
        return localizedString
    }
    
    var localized: String {
        //TODO: record all unresolved keys
        var localizedString = NSLocalizedString(self, comment: "")
        if localizedString == self {
            localizedString = NSLocalizedStringFallback(self, comment: "")
            if LogOptions.localization {
                if localizedString == self {
                    print("missing localization: \(self)")
                }else{
                    print("missing localization for \(String(describing: Locale.current.languageCode)): \(self)")
                }
            }
        }
//        if let (en, fr) = AllLocalizations(self){
//            var _en = en.replacingOccurrences(of: "\n", with: "\\n", options: .literal, range: nil)
//            var _fr = fr.replacingOccurrences(of: "\n", with: "\\n", options: .literal, range: nil)
//            if _en.contains(","){
//               _en = "\"\(_en)\""
//            }
//            if _fr.contains(","){
//                _fr = "\"\(_fr)\""
//            }
//            let loc_area = self.split(separator: ".").first ?? ""
//            LocalizationManager.shared.allLocalizationKeys[self] = "\(self),\(loc_area),\(_en),\(_fr)"
//        }
//        LocalizationManager.shared.addLocalization(key: self, string: localizedString)
        return localizedString
    }
    
    public func NSLocalizedStringFallback(_ key: String, tableName: String? = nil, bundle: Bundle = Bundle.main, value: String = "", comment: String) -> String {
        let fallbackLanguage = "Base"
        guard let fallbackBundlePath = Bundle.main.path(forResource: fallbackLanguage, ofType: "lproj") else { return key }
        guard let fallbackBundle = Bundle(path: fallbackBundlePath) else { return key }
        let fallbackString = fallbackBundle.localizedString(forKey: key, value: comment, table: nil)
        return Bundle.main.localizedString(forKey: key, value: fallbackString, table: nil)
    }
    
    public func AllLocalizations(_ key: String) -> (String, String)? {
        guard let baseBundlePath = Bundle.main.path(forResource: "Base", ofType: "lproj") else { return nil }
        guard let frBundlePath = Bundle.main.path(forResource: "fr", ofType: "lproj") else { return nil }
        
        guard let baseBundle = Bundle(path: baseBundlePath) else { return nil }
        guard let frBundle = Bundle(path: frBundlePath) else { return nil }

        let baseString = baseBundle.localizedString(forKey: key, value: nil, table: nil)
        let frString = frBundle.localizedString(forKey: baseString, value: baseString, table: nil)
        return (baseString, frString)
    }

}
{'fraction_non_alphanumeric': 0.12761306189370134, 'fraction_numerical': 0.003279136494056565, 'mean_word_length': 2.2562277580071175, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '204140', 'n_tokens_mistral': 2112, 'n_tokens_neox': 1976, 'n_words': 538}